Wednesday, October 7, 2009

JUnit and Emma testing

For another class assignment, we were introduced into creating our own tests and running them on JUnit. We were also introduced into using a coverage tool called Emma. JUnit allows java developers to easily test their code over and over. This saves the programmer a lot of time testing their code every time small changes are made. JUnit is especially useful for running tests on code after small changes are made and can tell the programmer if a change to the code has messed up other parts of the code. Emma is a coverage tool that tells the tester how much of the code was actually covered by the JUnit tests. To get used to JUnit and Emma, I wrote tests for my robocode robot.

I have never utilized JUnit before so this my first experience writing my own tests for code. It felt like I was learning an entirely new language, but after a few hours of looking at the API and the RobotTestBed I got the hang of it. I found that writing tests that are not acceptance tests are very challenging. At first, I had a hard time figuring out what types of behavioral tests to test on my robot. I decided to test if my robot moved to the correct side of the map initially, if my robot moved up and down the left or right wall, if my robot had its gun facing the correct direction, and if my robot turned 90 degrees when it was either hit by a bullet or ran into another robot.

The most challenging of the four behavioral tests were testing to see if my robot moved paralled up and down either the right or left wall. Eventually, I figured out that I had to test if the robot was first close to a wall then check if the robot was also facing parallel with the wall. After figuring that out, the rest of my tests went by pretty smoothly. Just thinking about what tests to run took a majority of my time. Thinking of the most efficient tests to test the most amount of code was challenging.

Using Emma was also an interesting experince for me. I had a lot of problems working with emma at first because I didn't understand exactly what all the red, green, and yellow highlights on my code were. As it turns out, the green highlights are highlights of code that was run during the tests. The red highlights were code that was not seen by Emma during the tests. The yellow was codes that were seen, but not executed or used by Emma. While trying to run Emma I ran into problems with Emma not being able to see my CounterBot.java file. This scared me for about an hour until a friend told me that I had to change the emma.build.xml file. The build file was not looking for the correct java code in the correct place, so after changing the code, I Emma was able to read my java file. I found that Emma was able to read 100% of my java file and also read almost all of my JUnit tests. The only test that Emma didn't read 100% was my TestToSide test. I think it was because some of my code doesn't get called if the robot only starts on one side of the map for the entire test.

In order to make my robot easier to test, I think I would have to create more methods within my code. I was not able to run any unit tests on my code since I didn't have methods that were not events within my code. I had a lot of code within the events like onHitRobot, onScannedRobot, and all those other tests. Therefore, I would probably make a moveDirection() method and a fire() method. This would allow me to test is my robot is moving laterally or test how strong of a bullet my robot would fire at a given distance.

My distribution file can be found here.

0 comments:

Post a Comment