Professional Documents
Culture Documents
“More than the act of testing, the act of designing tests is one of the
best bug preventers known. The thinking that must be done to create a
useful test can discover and eliminate bugs before they are coded –
indeed, test-design thinking can discover and eliminate bugs at every
stage in the creation of software, from conception to specification, to
design, coding and the rest.” – Boris Beizer
If the code handles most of the special condition give it the point.
if (a + b <= c) // Check a c b
{
System.out.println("Not a triangle");
} else { System.out.println("Triangle");}
}
}
Result: Not a triangle
Independently
Testable
Feature
Representative
Model
Values
Test Test
Case Cases
Specifications
a b c d x
a b c d x
a b c d x
a b c d x
a b
x
x1
a b
Single defect assumption
April 18, 2017 SE 433: Lecture 4 69 of 101
Example: nextDate() – Test Cases: Boundary Values
a b
x
x1
a b
April 18, 2017 SE 433: Lecture 4 72 of 101
Example: nextDate() – Test Cases: Boundary Values
x2
x1
a b
x2
x1
a b
@After @Test
public void deleteOutputFile() { public void test2WithFile() {
output.close();
// code for test case
output.delete();
} …
}
}
Run the method once only for the entire test class
before any of the tests, and
before any @Before method(s)
@BeforeClass
public static void anyName() {
// class setup code here
}
April 18, 2017 SE 433: Lecture 4 91 of 101
Once-Only Tear-Down
@AfterClass annotation on a static method
one method only
Run the method once only for the entire test
class
after any of the tests
after any @After method(s)
Useful for stopping servers, closing connections,
etc.
@AfterClass
public static void anyName() {
// class clean up code here
}
April 18, 2017 SE 433: Lecture 4 92 of 101
Timed Tests
Useful for simple performance test
Network communication
Complex computation
The timeout parameter of @Test annotation
in milliseconds
@Test(timeout=5000)
public void testLengthyOperation() {
...
}
The test fails
if timeout occurs before the test method completes
See Junit2.zip
@Parameters
public static Collection<Integer[]> data() {
return Arrays.asList(new Integer[][] {
{ 1, 0 }, // expected, value
{ 1, 1 },
{ 2, 2 },
{ 24, 4 },
{ 5040, 7 }, });
}
….
@Test
public void factorialTest() {
Calculator calc = new Calculator();
assertEquals(expected, calc.factorial(value));
}