You are on page 1of 17

10/8/21

CSCE 5430

Unit Testing

Testing Levels

n Unit testing: Does a single component work?


n Integration testing: Do multiple components work
together?
n System testing: Does my application work?
n Acceptance testing: Does the customer like my
application?

1
10/8/21

Testing Levels
n Unit testing
¨ Individual program units,
such as procedure,
methods in isolation
n Integration testing
¨ Modules are assembled to
construct larger
subsystem and tested
n System testing
¨ Includes wide spectrum of
testing such as
functionality, and load
Development and testing phases in
n Acceptance testing
¨ Customer’s expectations V model
from the system

What Is an Unit?

• Named software element


• Separately invokable
• Performs single function
• Examples

n Subprogram or script
n Java class method

2
10/8/21

Unit Testing

n Testing done to each module, in isolation, to verify its


behavior

n Typically the unit test will establish some sort of


artificial environment and then invoke routines in the
module being tested

n It then checks the results returned against either some


known value or against the results from previous runs
of the same test (regression testing)

Slide from

3
10/8/21

Unit testing
n Scaffolding needed to create the environment in
which the module should be tested
¨ stubs
n Modules used by the unit under test
n It is a dummy program
¨ drivers
n Program activating the unit under test
n It provides input data to unit under test and reports the
test result

Unit Testing

Unit Test Environment

4
10/8/21

Writing Unit Tests

n Unit test should be conveniently located


¨ For small projects you can imbed the unit test for a
module in the module itself
¨ For larger projects you should keep the tests in the
package directory or a test subdirectory of the
package
n By making the code accessible to developers you
provide them with:
¨ Examples of how to use all the functionality of your
module
¨ A means to build regression tests to validate any
future changes to the code
n In Java, you can use the main routine to run your unit
tests

Using Test Harnesses


n A test harness can handle common operations
such as
¨ Logging status
¨ Analyzing output for expected results
¨ Selecting and running the tests

n Harnesses can be:


¨ GUI driven
¨ Written in the same language as the rest of the
project
¨ May be implemented as a combination of make
files and scripts

10

5
10/8/21

Using Test Harnesses


n A test harness should include the following
capabilities:

¨ A standard way to specify setup and cleanup


¨ A method for selecting individual tests or all
available tests
¨ A means of analyzing output for expected (or
unexpected) results
¨ A standardized form of failure reporting

11

Unit Testing in eXtreme Programming

Test Driven development (TDD)


¨ Rather than thinking of testing as something that occurs after
implementation, we want to think of it as something that occurs BEFORE
and DURING implementation
¨ All production code is written to make failing test cases pass
¨ When developing software, we write a test case first, watch it fail, then
write the simplest code to make it pass; repeat

=> The result, increased confidence when performing other tasks


such as fixing bugs, refactoring, or reimplementing parts of your
software system

12

6
10/8/21

Unit Testing in eXtreme Programming

Pair programming
¨ In XP code is being developed by two programmers working side
by side
¨ One person develops the code tactically and the other one inspects
it methodically by keeping in mind the story they are
implementing

13

Unit Testing in eXtreme Programming


1. Pick a requirement, i.e., a story
2. Write a test case that will verify a small part
of the story
3. Write the code that implement particular
part of the story to pass the test
4. Execute all test
5. Rework on the code, and test the code
until all tests pass
6. Repeat step 2 to step 5 until the story is
fully implemented

Test-first process in XP

14

7
10/8/21

JUnit Testing Framework


n Test Driven Development, XP programming
n Test framework for writing unit tests in Java
n Other frameworks: NUnit (C#), CPPUnit (C++), fUnit (Fortran)
n JUnit is a simple, open source framework to write and run
repeatable tests

n References
• http://www.junit.org
• JUnit Cookbook
• Test Infected - Programmers Love Writing Tests
• JUnit – A cooks tour
• Java Extreme Programming Cookbook, chapter 4, Eric Burke and Brian
Coyner, O’REILLY

15

The Problem
n JUnit Test Infected: Programmers Love Writing Tests
“Every programmer knows they should write tests for their
code. Few do. The universal response to “Why not?” is
“I’m in too much of a hurry.” This quickly becomes a
vicious cycle – the more pressure you feel, the fewer tests
you write. The fewer tests you write, the less productive
you are and the less stable your code becomes. The less
productive and accurate you are, the more pressure you
feel.”

16

8
10/8/21

The Goals of JUnit


n JUnit creators: Erich Gamma and Kent Beck
n Write a framework: the developers will actually write
tests (easy to learn)
n Create tests that retain their value over time
n Leverage existing tests to create new ones

17

Why Use JUnit


n JUnit allows you to write code faster while increasing quality
n JUnit is elegantly simple
n JUnit tests check their own results and provide immediate
feedback
n JUnit tests can be composed into a hierarchy of test suites
n Writing JUnit tests is inexpensive
n JUnit tests increase the stability of software
n JUnit tests are developer tests
n JUnit tests are written in Java
n JUnit is free

18

9
10/8/21

Example: Money.java
class Money{
private int fAmount;
private String fCurrency;

public Money (int amount, String currency){


fAmount = amount;
fCurrency = currency;
}

public int amount() {


return fAmount;
}

public String currency(){


return fCurrency;
}

public Money add(Money m){


return new Money(amount()+m.amount(), currency());
}

19

Example: code a little, test a little

n Implement a test in a subclass of TestCase


¨ A test case contains a collection of unit tests

n Define MoneyTest as a subclass of TestCase

n Add a test method testSimpleAdd that exercises Money.add() in


Money.java
¨ Each unit test is a public, no-argument method beginning with
“test” (JUnit finds test method automatically with this naming
convention)

20

10
10/8/21

MoneyTest.java
import junit.framework.*;

public class MoneyTest extends TestCase {

public void testSimpleAdd(){


Money m12USD = new Money(12, “USD");
Money m14USD = new Money(14, “USD");
Money expected = new Money(26, “USD");
Money result = m12USD.add(m14USD);
assertEquals(expected.amount(), result.amount());
}
}

21

How To Run testSimpleAdd


n JUnit3.8.1
n Set the CLASSPATH of the JUnit jar file to run JUnit
¨ Setenv CLASSPATH .:$owndir/junit3.8.1/junit.jar

n Compile Money.java and MoneyTest.java


n Run
¨ % java junit.testui.TestRunner MoneyTest
n Result
.
Time: 0.007
OK (1 test)

22

11
10/8/21

Add equals method in Money.java

public boolean equals (Object anObject) {


if (an Object instanceof Money) {
Money aMoney = (Money) anObject;
return aMoney.currency().equals(currency())
&& amount() == aMoney.amount();
}
return false;
}

23

Add testEquals in MoneyTest.java

public void testEquals() {


Money m12USD = new Money(12, “USD");
Money m14USD = new Money(14, “USD");

assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}

24

12
10/8/21

Duplicated Code
public void testSimpleAdd(){
Money m12USD = new Money(12, “USD");
Money m14USD = new Money(14, “USD");
Money expected = new Money(26, “USD");
Money result = m12USD.add(m14UDS);

assertEquals(expected.amount(), result.amount());
}

public void testEquals() {


Money m12USD = new Money(12, “USD");
Money m14USD = new Money(14, “USD");

assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}

25

A setUp method
n A common fixture for running the tests

public class MoneyTest extends TestCase {


Money m12USD;
Money m14USD;
public void setUp() {
m12USD = new Money(12, “USD”);
m14USD = new Money(14, “USD”);
}
}

26

13
10/8/21

Rewrite the two test methods


public void testSimpleAdd(){

Money expected = new Money(26, “USD");


Money result = m12USD.add(m14UDS);

assertEquals(expected.amount(), result.amount());
}

public void testEquals() {

assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}

27

tearDown method
n Cleanup code
n Release any permanent resources you allocated in setUp
n Often ignore the tearDown() method
n Useful if your tests do things like open database
connections, show GUI frames, …
public void tearDown(){
this.game.shutdown();
}

28

14
10/8/21

A sequence of invoking tests


n JUnit follows the following steps for each test method
¨ Call the test case’s setUp() method
¨ Call the test method
¨ Call the test case’s tearDown() method

n On-time setUp and tearDown


¨ junit.extensions.TestSetUp supports this functionality

29

Test Case Organization


public class MoneyTest extends TestCase{

public void setUp { … }

public void testSimpleAdd() { … }

public void testEquals() { … }


:
public void testXXX() { … }
}

30

15
10/8/21

Organizing tests into a Test Suite


public class MoneyTest extends TestCase{

public void setUp { … }

public static Test suite() {


return new TestSuite (MoneyTest.class);
}

public void testSimpleAdd() { … }

public void testEquals() { … }


}

31

Organizing tests into a Test Suite


public class MoneyTest extends TestCase{

public void setUp { … }

public static Test suite() {


TestSuite suite = new TestSuite();
suite.addTest (new MoneyTest(“SimpleAdd”));
suite.addTest (new MoneyTest(“testEquals”));
return suite;
}

public MoneyTest(String test){


super(test);
}

public void SimpleAdd() { … }

public void testEquals() { … }


}

32

16
10/8/21

Organizing Test Suites into a Test Suite

….

public static Test suite() {


TestSuite suite = new TestSuite();
suite.addTest(MoneyTest.suite());
suite.addTest(ExchangeTest.suite());
suite.addTest(OtherTest.suite());
:
return suite;
}

33

JUnit Test Suites/Tests Organization

Test Suite


… …

test methods

34

17

You might also like