Wednesday, October 7, 2009

Play around with Junit~~

After applied some automated QA tools (CheckStyle, PMD, FindBugs) to my robocode project, it is also important to write some tests manually to assure my robot works correctly. This time, I created total six tests for my project, including two acceptance tests, two behavioral tests, and two unit tests. Here is a list of my tests.

Acceptance tests:
1. check if my robot can beat SittingDuck 10 times out of 10 battles.
2. check if my robot can beat Walls more than 5 times out of 10 battles.

Behavioral tests:
1. check if my robot ever visited the four corners in the battlefield during 5 battles.
2. check if my robot ever fire at SittingDuck and hit SittingDuck during 5 battles.

Unit tests:
1. check if the calculation for robot heading given the start position and target position is correct.
2. check if the calculation for normalizing angles is correct.

In my case, acceptance test is the easiest test to create. It only checks the "strength" of the robot to make sure it fits in some requirements. Unit test is a little bit harder to create, because it depends on the complexity of the unit that need to be tested. If the unit is relatively big, it is nice to break it down to pieces and write unit test for each pieces. I think the behavioral test is the most challenging test to create. For example, I tried to write a detail test for my robot's firing strategy. The firing strategy is determined by the robot's sensor. However, in a battle, we cannot tell when, where, and how robots see another robot unless we set the initial positions for all the robots. Therefore, it is very hard to test a specific behavior at a certain point of time. The most we can do is track if the robot ever made certain movement during a period of time.
After creating the test, I used Emma to check the coverage of my tests. 59% of lines of code are tested which is a acceptable number since there is only 6 tests. However, I think, to be a solid test, more than 90% of lines of code should be tested. I think test case is an important way to ensure the quality of the project, especially for a large system. Test is like a documentation for the system. It helps developers to mentain the system in a more efficient way. Also, developers can even develop the system by writing tests.

Here is my latest code with all the tests:

No comments:

Post a Comment