COMP3053.SQA
¡ Some of the materials we use may come directly from previous teachers of this module, and other sources …
¡ Thank you to (amongst others):
Copyright By PowCoder代写 加微信 powcoder
▪ , , , , , Julie Greensmith, , T.Y. Chen,
COMP3053.SQA.08: Unit Testing
¡ More Intro Testing Stuff
¡ White Box Testing
§ (Some) Strategies
§ Basis Path Testing
§ Cyclomatic Complexity
¡ Unit Testing
COMP3053.SQA.08: Unit Testing
¡ A good test is one that finds an error
¡ Testing cannot prove the absence of errors
¡ Complete test coverage is impossible § Concentrate on problem areas?
¡ It costs a lot less to remove bugs early
¡ Code Testing ≠ Code Walkthrough
COMP3053.SQA.08: Unit Testing
¡ Unit Testing
§ Modules of code
¡ IntegrationTesting § Design
¡ Validation Testing § Requirements
¡ System Testing
§ System Engineering
COMP3053.SQA.08: Unit Testing
COMP3053.SQA.08: Unit Testing
COMP3053.SQA.08: Unit Testing
COMP3053.SQA.08: Unit Testing
¡ Function coverage
§ Has each function in the program been executed?
¡ Statement/Line coverage
§ Has each line of the source code been executed?
¡ Branch coverage
§ Has each branch of each control structure been executed?
¡ Condition coverage
§ Has each evaluation point (such as a true/false decision) been executed?
¡ Path coverage
§ Has every possible route through a given part of the code been executed?
COMP3053.SQA.08: Unit Testing
int example1 (int value, boolean cond1, boolean cond2)
if ( cond1 )
if ( cond2 )
return value;
¡ Total Statement Coverage with one case – T T
¡ Total Path Coverage with four paths – TT TF FT FF § But, total path coverage is usually impractical, so Basis
Path Testing is usually better
COMP3053.SQA.08: Unit Testing
¡ Draw a Flow Graph
¡ Determine the Cyclomatic Complexity (CC) § CC = number of regions
§CC = E – N + 2
¡ Max (necessary) number of tests = CC
¡ Derive a basis set of independent paths
¡ Generate data to drive each path
COMP3053.SQA.08: Unit Testing
COMP3053.SQA.08: Unit Testing
3: 3: 4: 4:
6: 6: 7: 8: 8:
WHILE NOT EOF LOOP Read Record;
IF field1 equals 0 THEN
Add field1 to Total
Increment Counter ELSE
IF field2 equals 0 THEN
Print Total, Counter
Reset Counter ELSE
Subtract field 2 from Total END IF
Print “End Record”
END LOOP Print Counter
COMP3053.SQA.08: Unit Testing
int example1 (int value, boolean cond1, boolean cond2)
if ( cond1 )
if ( cond2 )
return value;
Cyclomatic Complexity (CC) = 3
¡ Test Data:
false false true false true true
¡ Basis Paths:
1 3 5 1235 12345
COMP3053.SQA.08: Unit Testing
¡ JUnit is a framework for writing tests
§ written by (of Design Patterns fame, GoF) and (creator of XP methodology)
¡ JUnit uses Java’s reflection capabilities
§ (Java programs can examine their own code)
¡ Uses annotations as control
¡ JUnit helps the programmer:
§ define and execute tests and test suites
§ formalize requirements and clarify architecture
§ write and debug code
§ integrate code and always be ready to release a working version
COMP3053.SQA.08: Unit Testing
int max(int a, int b) {
if (a > b) {
return b; }
void testMax() {
assertEquals(7, max(3, 7));
assertEquals(3, max(3, -7)); }
void testMax() {
int x = max(3, 7);
if (x != 7) {
} System.out.println(“max(3, 7) gives ” + x);
x = max(3, -7);
if (x != 3) {
} } System.out.println(“max(3, -7) gives ” + x);
public static void main(String[] args) {
new MyClass().testMax(); }
COMP3053.SQA.08: Unit Testing
¡ A unit test tests methods in a single class
¡ A test case tests (if possible) a single method
§ can have multiple test cases for a single method
¡ A test suite combines unit tests
¡ The test fixture provides software support
for all this
¡ The test runner runs unit tests or an entire
test suite
¡ Integration testing (testing that it all works
together) is not well supported by JUnit
COMP3053.SQA.08: Unit Testing
¡ Start by importing these JUnit classes:
import org.junit.*;
import static org.junit.Assert.*; // note static import
¡ Declare your test class in the usual way public class MyProgramTest {
¡ Declare an instance of the class being tested
§ Can declare other variables, but don’t give them initial values here public class MyProgramTest {
MyProgram program;
int someVariable;
COMP3053.SQA.08: Unit Testing
¡ Define method(s) to be executed before each test
§ Initalize variables here: each test starts with fresh values @Before
public void setUp() {
program = new MyProgram();
someVariable = 1000;
¡ Can also define method(s) to be executed after each test
§ Typically such methods release resources, such as files
§ Usually there is no need to bother with this method @After
public void tearDown() {
COMP3053.SQA.08: Unit Testing
¡ SupposeyouhaveaclassArithmeticwiththemethods: § int multiply(int x, int y),
§ boolean isPositive(int x)
import org.junit.*;
import static org.junit.Assert.*;
public class ArithmeticTest {
public void testMultiply() {
assertEquals(4, Arithmetic.multiply(2, 2));
assertEquals(-15, Arithmetic.multiply(3, -5));
public void testIsPositive() {
assertTrue(Arithmetic.isPositive(5));
assertFalse(Arithmetic.isPositive(-5));
assertFalse(Arithmetic.isPositive(0));
} COMP3053.SQA.08: Unit Testing
¡ Within a test:
§ Call the method being tested and get the actual result
§ Assert what the correct result should be with an assert method § These steps can be repeated as many times as necessary
¡ An assert method is a JUnit method that performs a test, and throws an AssertionError if the test fails
§ JUnit catches these Errors and shows you the result
COMP3053.SQA.08: Unit Testing
static void assertTrue(boolean test)
static void assertTrue(String message, boolean test)
§ Throws an AssertionError if the test fails § The optional message is included in the Error
static void assertFalse(boolean test)
static void assertFalse(String message, boolean test)
§ Throws an AssertionError if the test fails
COMP3053.SQA.08: Unit Testing
assertEquals(expected, actual)
assertEquals(String message, expected, actual)
§ expected and actual must be both objects or same primitive type § For objects, uses your equals method
▪ (ifyouhavedefineditproperly) assertSame(Object expected, Object actual)
assertSame(String message, Object expected, Object actual)
§ Asserts that two arguments refer to the same object assertNotSame(Object expected, Object actual)
assertNotSame(String message, Object expected, Object actual)
§ Asserts that two objects do not refer to the same object COMP3053.SQA.08: Unit Testing
¡ These are much less common: assertNull(Object object) assertNull(String message, Object object)
§ Asserts that the object is null (undefined) assertNotNull(Object object)
assertNotNull(String message, Object object)
§ Asserts that the object is not null fail()
fail(String message)
§ Causes the test to fail and throw an AssertionFailedError § Useful as a result of a complex test, when the other assert
methods aren’t quite what you want
COMP3053.SQA.08: Unit Testing
¡ Difficult to add JUnit tests to an existing program
§ The program probably wasn’t written with testing in mind
¡ Better to write the tests before writing the code
§ This seems backward, but it really does work better
▪ Whentestsarewrittenfirst,youhaveaclearerideawhattodowhenyou
write the methods
▪ Becausethetestsarewrittenfirst,themethodsarewrittentobetestable
▪ Writingtestsfirstencouragessimpler,single-purposemethods
▪ Becausethemethodswillbecalledfrommorethanoneenvironment(the
“real” one, plus your test class), they tend to be more independent of the environment
COMP3053.SQA.08: Unit Testing
¡ In order to run our tests, the methods we are testing have to exist, but they don’t have to be right
¡ Instead of starting with “real” code, we start with stubs
§ Minimal methods that always return the same values
▪ A stub that returns void can be written with an empty body
▪ A stub that returns a number can return 0 or -1 or 666
▪ or whatever number is most likely to be wrong
▪ A stub that returns a boolean value should usually return false
▪ A stub that returns an object of any kind (including a String or an array) should
return null
¡ When we run our test methods with these stubs, we want the test
methods to
§ helps test the tests
§ help make sure that an incorrect method does not pass the tests
COMP3053.SQA.08: Unit Testing
¡ You can define a suite of tests
@RunWith(value=Suite.class)
@SuiteClasses(value={
MyProgramTest.class,
AnotherTest.class,
YetAnotherTest.class
public class AllTests { }
COMP3053.SQA.08: Unit Testing
¡ If you write your method stubs first (as on the previous slide), Eclipse will generate test method stubs for you
¡ To add JUnit to your project:
1. Select a class in Eclipse
2. Go to File -> New… -> JUnit Test Case
3. Make sure Unit test is selected
4. Click where it says “Click here to add JUnit … ”
5. Close the window that appears
COMP3053.SQA.08: Unit Testing
¡ If you write your method stubs first (as on the previous slide), Eclipse will generate test method stubs for you
¡ To create a JUnit test class:
Do steps 1 and 2 from previous slide (if you haven’t already)
Click Next >
Use checkboxes to decide which methods to have test cases for ▪ Don’tselectObjectoranythingunderit
▪ Maybe check “create tasks” : up to you…
Click Finish
¡ To run the tests:
§ Choose Run -> Run As -> JUnit Test COMP3053.SQA.08: Unit Testing
COMP3053.SQA.08: Unit Testing
1. Write a test for some method you intend to write
§ If the method is fairly complex, test only the simplest case
2. Write a stub for the method
3. Run the test and make sure it fails
4. Replace the stub with code
§ Write just enough code to pass the tests
5. Run the test
§ If it fails, debug the method (or the test?) § Repeat until the test passes
6. If the method needs to do more, or handle more complexity, add the tests first, then go back to step 3
COMP3053.SQA.08: Unit Testing
If you don’t unit test then you aren’t a software engineer, you are a typist who understands a programming language.
1. Never underestimate the power of one little test.
2. There is no such thing as a dumb test.
3. Your tests can often find problems where you’re not expecting them.
4. Test that everything you say happens actually does happen.
5. If it’s worth documenting, it’s worth testing.
COMP3053.SQA.08: Unit Testing
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com