This was my first experience with writing JUnit tests so it was a fun learning experience, but also a difficult one. We were assigned to write six tests for our existing robocode project: two acceptance tests and four behavioral or unit tests. For my project I wrote two acceptances tests which tested my robots WallSpin's ability to beat two other robots; in my case the SittingDuck and RamFire Robots. I wrote three behavioral tests which tested that WallSpin visited each corner of the arena, visited the center of the arena, and spent at least fifty percent of its time in wall mode when facing the Crazy robot. The last test I did was a unit test, which tested that WallSpin's firePower method returned a bullet fire power relative to the enemies distance.
Tests
-
TestWallSpinVersusSittingDuck
Type: Acceptance Test
Overview: This tests that WallSpin can successfully beat SittingDuck.
Difficulty: This test was rather easy to create as it was based off of the example test provided for this assignment.
Test Quality: I feel that this test in particular, because it is only facing SittingDuck, does not really ensure the quality of my robot. All this test really does is make sure that my robot knows how to target and fire at a stationary robot. -
TestWallSpinVersusRamFire
Type: Acceptance Test
Overview: This tests that WallSpin can successfully beat RamFire.
Difficulty: Again, this test was also pretty easy to create; it was just a matter of changing the robot that WallSpin should face, from SittingDuck to RamFire, from the previous test case.
Test Quality: This test does ensure the quality of WallSpin, in the sense that it guarantees that WallSpin can beat another simple robot which can track and attack. It is important that WallSpin can beat certain robots, like RamFire, a hundred percent of the time. -
TestWallSpinMovement
Type: Behavioral Test
Overview: This tests that WallSpin visits each corner of the arena.
Difficulty: Like the first test mentioned, this was easy to write as I just used the example class file provided for this assignment. It just so happened that my Robot used wall movements, so it should be guaranteed to visit each corner of the arena.
Test Quality: I would say that this is a valid and important test because it makes sure that my robot is behaving the way it should. This test reassures that WallSpin will visit each corner of the arena when facing SittingDuck. -
TestWallSpinCenterMovement
Type: Behavioral Test
Overview: This tests that WallSpin can will move to the center of the arena when hit by a bullet.
Difficulty: This was a bit more difficult of a test to write, because I had to calculate the center of the arena and take into account the size of the robot. I basically had to test if the robot was within fifty units of the center in either the x and y coordinates.
Test Quality: This test does ensure the quality of WallSpin because it makes sure it is behaving the way it is supposed to: moving to the center of the arena when hit by a bullet. To guarantee that WallSpin is hit by a bullet, I make it fight the WallsRobot. If WallSpin were to not move to the center of the arena during its battle with Walls, it would mean that there is a flaw in WallSpin's code. -
TestWallSpinPercentageMovement
Type: Behavioral Test
Overview: This tests to see that WallSpin spends at least fifty percent of its time along the walls when facing the Crazy robot.
Difficulty: This test was a bit difficult to write as I had to calculate the range of where the walls lie in the coordinate system, then check during each turn if WallSpin was within those coordinates. At the end of the rounds I divide the number of times WallSpin was along the walls by the number of turns that WallSpin took, this provides me with the percentage of time WallSpin was riding along the walls. Thinking of how to conceptually write the test was more difficult than actually writing it.
Test Quality: I would say this is an important test of WallSpin's behavior, because depending on the robot it faces, it should spend roughly fifty percent of its time in wall mode. If it's not then something is wrong with its code, or the robot it is facing has trapped it in a corner. -
TestWallSpinFirePower
Type: Unit Test
Overview: This tests to see if WallSpin's firePower method returns a firepower relative to the distance of the enemy.
Difficulty: This test was pretty difficult to write, because I had to modify the source code of WallSpin to allow this unit test. I had to make the firePower method take the maxDistance of the arena and the enemy's position as parameters to be able to unit test the method. In the unit test I set the maxDistance as five hundred and give firePower varying positions for the enemy's distance.
Test Quality: This test is probably one of the most valid and important tests, as it makes sure that WallSpin is firing bullets proportional to the enemies distance. If this test fails this means that WallSpin's code is flawed and that WallSpin is most likely either wasting too much energy or using too little energy.
Jacoco
Running Jacoco showed that my WallSpin class had a total of seventy-two percent coverage. Basically it showed me what I already suspected, that some of the code in methods like moveToCenter are not guaranteed to be reached all the time. In WallSpin's moveToCenter method there are some cases I check for that will rarely be reached, such as the special case where the robot is already in the center when moveToCenter is called. Even though it showed me things that I already knew, the Jacoco output was very interesting to look at. I've never seen a tool that showed the coverage of the code, and I definitely believe that this can be a useful tool for detecting errors in my future projects.
How could I redesign WallSpin to make it easier to test?
If I were to rewrite WallSpin I would definitely separate its methods more effectively and make them have return values as opposed to being void. This would make it much easier to write unit tests for WallSpin. I actually had to modify WallSpin's source code in order to write the firePower unit test. I would also give the robot more defined movement patterns, as it would be much easier to create behavioral tests for. With the existing source code I had, it was very difficult for me to come up with my behavioral and unit tests.
Download
To download my WallSpin robot distribution click here.
No comments:
Post a Comment