Wednesday, October 1, 2008

Testing Stack using Emma and Junit

Testing a program or a software probably is the most tedious thing that a programmer does. But without thouroughly testing a program you will never know the importance of effort and time that you dedicated to finish the program. When me and my partner [Vincent] was starting to invoke the ant -f emma.build.xml in the class we stumbled upon on some errors on the emma.tool. And I was asking my partner what might be the problem but he didn't know either because he didn't even install Emma on his computer yet. In addition, I had a very slow and old laptop to run all these programs so it's not really helping me at all either. Therefore, I installed and set the path all the programs that I need in my new laptop and everything is much better. So I run the ant -f emma.build.xml on my revised Stack program and here is my coverage summary. The method, block and line is pretty low so we have to improve the line coverage to 100%.

[concat] Emma Coverage summary
[concat] class: 100% (3/3)
[concat] method: 73% (8/11)
[concat] block: 69% (65/94)
[concat] line: 75% (15/20)

The first thing I did was add some testing to my TestClearStack.java.
____________________________________________________________________

/**
* Test the top of stack.
* Should use JUnit 4 exception annotation!
*
* @throws EmptyStackException if the stack is empty during the test process.
*/
@Test
public void testGetTop() throws EmptyStackException {
ClearStack stack = new ClearStack();
stack.push(two);
stack.push(three);
assertEquals("The top of stack is two", three, stack.getTop());
}

/**
* Test illegal empty of the stack.
* Should use JUnit 4 exception annotation!
*
* @throws EmptyStackException if the stack is empty during the process.
*/
@Test
public void testIllegalEmpty() throws EmptyStackException {
ClearStack stack = new ClearStack();
stack.push(one);
stack.isEmpty();
assertEquals("The stack is not empty", false, stack.isEmpty());
}

/**
* Test if the stack is empty.
* Should use JUnit 4 exception annotation!
*
*/
@Test
public void testLegalEmpty() {
ClearStack stack = new ClearStack();
stack.isEmpty();
assertEquals("The stack is empty", true, stack.isEmpty());
stack.push(two);
assertEquals("The stack is empty", false, stack.isEmpty());

}

************************************************
TestStack.java
************************************************
/**
* Test illegal getTop() of the stack.
*
* @exception EmptyStackException If errors during stack processing.
*/
@Test
public void testIllegalTop() throws EmptyStackException {
Stack stack = new Stack();
try {
stack.top();
fail("Top of empty stack did not generate exception.");
}
catch (EmptyStackException e) {
System.out.println(e);
}

}

/**
* Test legal get top of empty stack.
* Should use JUnit 4 exception annotation!
*
* @throws EmptyStackException if the stack is empty during the process.
*/
@Test
public void testLegalGetTop() throws EmptyStackException {
Stack stack = new Stack();
stack.push(two);
stack.push(three);
assertEquals("The top of stack is three", three, stack.top());
stack.pop();
assertEquals("The top of stack is two", two, stack.top());

}

/**
* Test legal pop of the stack.
* Should use JUnit 4 exception annotation!
*
* @throws EmptyStackException if the stack is empty during the process.
*/
@Test
public void testPop() throws EmptyStackException {
Stack stack = new Stack();
stack.push(two);
stack.push(three);
assertEquals("Testing pop of the top of the stack", three, stack.pop());
assertEquals("Testing pop of the top of the stack", two, stack.pop());
}

________________________________________________________________

When I added those test cases I obtained this coverage summary:

[concat] Emma Coverage summary
[concat] class: 100% (3/3)
[concat] method: 100% (11/11)
[concat] block: 100% (94/94)
[concat] line: 100% (20/20)

Here's the link for my final Stack build:
http://www2.hawaii.edu/~flestado/stack-flestado-final-build-6.0.1006.zip

Conclusion:

The most important thing that I've learned in implementing different
test cases to improve the Emma coverage summary is doing thoroughly testing
and implementing the correct result assertion. For some reason every time I run
my JUnit test it gives some kind of "RemoteRunnerError, etc.". Therefore, I have
to actually "Clean" it first for my test to be able to run correctly. Adding some sort
of test cases into my TestStack.java and TestClearStack.java helped to improve my
Emma coverage summary. The only main thing that the Emma is detecting is how
many test case methods you implement but you can actually put those test cases in
one method except that it doesn't really increase the coverage summary.





No comments: