Concepts of Software Engineering: Testing
Concepts of Software Engineering: Testing
ENGINEERING
Testing
Select Task
Quick Design
Write Test
Code Refactor
Integrate
Go Home
Testing
Continuous Testing: Before programmers add a
feature, they write a test for it. When the suite runs,
the job is done. Tests in XP come in two basic
flavors.
Unit Tests are automated tests written by the
developers to test functionality as they write it. Each
unit test typically tests only a single class, or a small
cluster of classes. Unit tests are typically written using
a unit testing framework, such as JUnit.
Testing
Acceptance Tests (also known as Functional Tests)
are specified by the customer to test that the overall
system is functioning as specified. Acceptance tests
typically test the entire system, or some large chunk of
it. When all the acceptance tests pass for a given user
story, that story is considered complete.
At the very least, an acceptance test could consist of a
script of user interface actions and expected results that
a human can run. Ideally acceptance tests should be
automated, either using the unit testing framework, or a
separate acceptance testing framework.
Iterative Software development
7
Write
and
execute
unit tests Execute
Write
acceptance
acceptance
tests
tests
increment
+ system
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import math.Add;
import org.junit.*;
import static org.junit.Assert.*;
@Test
public void testAdd() {
Add add=new Add();
int sum=add.sum(3, 2);
assertEquals(5, sum);
}
}
Another example (Junit 4.x)
public class Add {
30 public static int sum (int a, int b) {
return a- b;
}
}
import math.Add;
import org.junit.*;
import static org.junit.Assert.*;
@Test
public void testAdd() {
Add add=new Add();
int sum=add.sum(3, 2);
assertEquals(5, sum);
}
}
Another example (Junit 4.x)
Test case 2
31
import junit.framework.TestCase;
import math.Add;
}
Example: Old way vs. new way
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
void testMax() { @Test
int x = max(3, 7);
if (x != 7) { void testMax() {
System.out.println("max(3, 7) gives " + x);
} assertEquals(7,
x = max(3, -7);
if (x != 3) { max(3, 7));
System.out.println("max(3, -7) gives " + x);
} assertEquals(3,
}
public static void main(String[] args) {
max(3, -7));
}
new MyClass().testMax();
}
Test fixtures
A test fixture is the context in which a test case
runs.
Typically, test fixtures include:
Objects or resources that are available for use by any
test case.
Activities required to make these objects available
and/or resource allocation and de-allocation.
Test fixtures
Before
You can define one or more methods to be executed before
each test; typically such methods initialize values, so that
each test starts with a fresh set
@Before public void
createOutputFile()
{
output = new File(...);
}
Test fixtures
After
You can define one or more methods to be executed
after each test; typically such methods release
resources, such as files
@After public void
deleteOutputFile()
{
output.delete();
}
Test fixtures
BeforeClass
If you wish, you can declare one method to be executed just
once, when the class is first loaded
This is for expensive setup, such as connecting to a
database
@BeforeClass
public static void setUpClass() throws Exception
{
// one-time initialization code
}
Test fixtures
AfterClass
If you wish, you can declare one method to be
executed just once, to do cleanup after all the tests
have been completed
@AfterClass
public static void tearDownClass() throws
Exception {
// one-time cleanup code
}
Example
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
public class JunitTest1
{
private Collection collection;
private Collection collection;
@BeforeClass public static void oneTimeSetUp()
{ // one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
Example
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println(“("@AfterClass - oneTimeTearDown");
}
@Before public void setUp()
{ collection = new ArrayList();
System.out.println("@Before - setUp");
}
@After public void tearDown()
{ collection.clear();
System.out.println("@After - tearDown"); }
Example
@Test
public void testEmptyCollection()
{ assertTrue(collection.isEmpty());
System.out.println("@Test -testEmptyCollection"); }
@Test
public void testOneItemCollection() { collection.add("itemA");
assertEquals(1, collection.size()); System.out.println("@Test -
testOneItemCollection");
}}
Example
Results:
@BeforeClass - oneTimeSetUp
@Before – setUp
@Test - testEmptyCollection
@After – tearDown
@Before - setUp
@Test – testOneItemCollection
@After - tearDown
@AfterClass - oneTimeTearDown
Assertions
Assertions are defined in the JUnit class Assert
If an assertion is true, the method continues executing.
If any assertion is false, the method stops executing at
that point, and the result for the test case will be fail.
If any other exception is thrown during the method, the
result for the test case will be error.
If no assertions were violated for the entire method, the
test case will pass.
All assertion methods are static methods
Assertions
A test method is annotated with @Test, takes no
parameters, and returns no result
@Test
public void sum() {
assertEquals(15, program.sum(array));
assertTrue(program.min(array) > 0);
}
Assertion
Boolean conditions are true or false
assertTrue(condition)
assertFalse(condition)
Objects are null or non-null
assertNull(object)
assertNotNull(object)
Objects are identical (i.e. two references to the same
object), or not identical.
assertSame(expected, actual)
true if: expected == actual
assertNotSame(expected, actual)
Assertion methods (2)
“Equality” of objects:
assertEquals(expected, actual)
valid if: expected.equals( actual )
“Equality” of arrays:
assertArrayEquals(expected, actual)
arrays must have same length
for each valid value for i, check as appropriate:
assertEquals(expected[i],actual[i])
or
assertArrayEquals(expected[i],actual[i])
Floating point assertions
When comparing floating point types (double or
float), there is an additional required parameter delta.
The assertion evaluates
Math.abs( expected – actual ) <= delta
to avoid problems with round-off errors with floating point
comparisons.
Example:
assertEquals( aDouble, anotherDouble, 0.0001 )
?When is JUnit appropriate
As the name implies…
for unit testing of small amounts of code
On its own, it is not intended for complex testing, system
testing, etc.
White box
Confirmation
design… of expected
elements behavior
White Box Testing