08 Unit Test
08 Unit Test
Content
1. Testing overview
ITSS SOFTWARE DEVELOPMENT 2. Unit Test
08. UNIT TEST
3. Integration Test
1 2
3 4
3 4
1
5 6
5 6
7 8
Specification
Precondition Postcondition
Implementation = soict?
7 8
2
9 10
9 10
11 12
11 12
3
14
13 14
15 16
0 20 40 60 70 80 100
15 16
4
17
17 18
19 20
19 20
5
2.2. Blackbox Testing Techniques
2.2.3. Decision Table
Example: Decision table
• Relations between the conditions for and the contents of • The conditions for creating reports from employee files
the processing are expressed in the form of a table
• A decision table is a tabular form tool used when complex
conditions are combined Under age 30 Y Y N N
Male Y N Y N
Married N Y Y N
Output Report 1 - X - -
Output Report 2 - - - X
Output Report 3 X - - -
Output Report 4 - - X -
21 22
Decision Table for “Examination Judgement”??? Decision Table for “Examination Judgement”
60 ---------------------------------------------------------------------------------------------------------------------------------------
Average (4) 77 80 Passed
Score “Passed” Yes Yes Yes --- Yes --- N/A --
is 80. (5) 85 92 Passed “Failed” --- --- --- Yes --- Yes N/A Yes
40
(7) (6) (6) 79 58 Failed
(7) 52 58 Failed
20 (8) 15 120 Invalid
Score of (9) 68 -66 Invalid
Math.
(10) 118 85 Invalid
0 20 40 60 70 80 100
(9)
23 24
6
26
If both of mathematics score and physics score are invalid, two messages are
expected to be output. Is it correct specifications? Please confirm it?
25 26
Test cases for “Log in” Creating test cases from use cases
• “Thành công” • Identify all of the scenarios for the given use case
• Mã PIN đúng
• Alternative scenarios should be drawn in a graph fo
• “Thất bại”
each action
• Mã PIN sai và số lần sai < 3
• “Khoá tài khoản” • Create scenarios for
• Mã PIN sai và số lần sai = 3 • a basic flow,
• one scenario covering each alternative flow,
Mã PIN đúng Y Y N N
• and some reasonable combinations of alternative
Số lần sai < 3 Y N Y N
“Thành công” x N/A - -
flows
“Thất bại” - N/A x - • Create infinite loops
“Khoá tài khoản” - N/A - x
• Phân tích vùng biên? Số lần sai = 2, 4 (?)
27 28
7
E.g. Control flow test of “Examination Judgment
2.3. White Box Testing Techniques Program”
• Test cases should cover all processing structure in Start
29 30
E.g. Control flow test of “Examination Judgment E.g. Control flow test of “Examination Judgment
Program” – 100% C0 coverage Program” – 100% C1 coverage
TC1int: Math score: 88, Physics score: 75 → “Passed” TC1int: Math score: 88, Physics score: 75 → “Passed”
Start Start TC2int: Math score: 68, Physics score: 90 → “Failed”
TC2int TC2int: Math score: 68, Physics score: 90 → “Failed”
TC1int TC1int TC2int TC3int: Math score: 98, Physics score: 68 → “Passed”
Get a score Get a score
All statements are executed. TC3int All branches are executed.
False ALL False ONLY 3 TEST
Mathematics Mathematics
=>70 BRANCHES =>70 CASES???
? Compared to 7 TCs?
True True
False False False
Physics False M+P/2 Physics M+P/2
=>70 =>80 =>70 =>80
True True True True
End End
31 32
8
E.g. Control flow test of “Examination Judgment
Decision Table for “Examination Judgement”
Program” – 100% C1 coverage
Condition1: Mathematics score=>70 TC2 is covered by TC2int and TC3int?
Start
Condition2: Physics score=>70 TC2intTC2 TC6 is covered by TC3int and TC2int?
TC1int
Condition3: Average of Mathematics, and Physics =>80 Get TC6
a score
TC3int BLACK
----------------------TC5---------TC4---------TC3---------TC6----------TC2--------TC1----------TCNG--------TC7
Condition1 True True True True False False False False
False
Condition2 True True False False True True False False
Mathematics BOX!!!
=>70
Condition3 True False True False True False True(none) False
--------------------------------------------------------------------------------------------------------------------------------------- True
“Passed” Yes Yes Yes --- Yes --- N/A -- False False
“Failed” --- --- --- Yes --- Yes N/A Yes Physics M+P/2
=>70 =>80
• One TCxint can cover plural TCs, based on the correct control flow structure True True
• TC1int covers TC5 and TC4
• TC2int covers TC1 and TC7
• TC3int covers to TC3. “Passed” “Failed”
• TC2 and TC6 are left in no execution.
End
33 34
“Passed” “Failed”
End
35 36
9
38
How to test a loop structure program Examples for “Examination Judgment Program”
• For the control flow testing in the software including a Input two subjects scores, Mathematics and Physics,
loop, the following criteria are usually adopted instead for each member of one class.
of C0/C1 coverage measures. The input form is “tabular form”.
• Skip the loop. Class members can be allowed only 0 (zero) through 50.
• Only one pass through the loop. Output/Print out the “Examination result report for a
• Typical times m passes through the loop class”.
• n, n-1, n+1 passes through the loop The output form is also “tabular form” that has the columns
such as student name, scores (Math., Physics), passed or
• n is maximum number, m is typical number (m<n)
failed.
• Example: 6 cases based on boundary-value analysis:
37 38
Examples for “Examination Judgment Program” Examples for “Examination Judgment Program”
Start
Exercise: Loop test cases of the module are; n = 50.
Get “number of students” Create the test cases using the
criteria two pages before based “number of students” = 0,
counter = zero
on the following assumptions. “number of students” = 1,
1. “Examination “number of students” = 20,
counter < False Judgment
number of students +1 program” are “number of students” = 49,
True already tested. “number of students” = 50,
2. Input data of this
Function: “Examination Print out “number of students” = 51 → Invalid.
module are
Judgment program” “examination result
already checked,
report for a class”.
and valid.
counter = counter 0 1 typical times: m=20 n-1 n n+1
+1
End
39 40
10
How to test for nested loops structure 2.4. Combination of Black/White Box test
• Advantage of Black box
At first, first loop’s control • Encompassing test based on external specification
number is determined at • Very powerful and fundamental to develop high-quality software
typical number, and second • Advantage of White box
loop is tested as a simple loop. • If any paths/flows don’t appear in the written specifications, the
paths/flows might be missed in the encompassing tests => White
Next, second loop’s control box test
number is determined at • for data of more than two years before => alternative paths
typical number, and first loop • “0 =< score =< 100” => code: “if 0 =< score ” and “if score =< 100”
41 42
44
43 44
11
Architectural overview
Why create a test suite?
• JUnit test framework is a
• Obviously you have to test your code—right?
package of classes that lets
• You can do ad hoc testing (running whatever tests occur to you at you write tests for each
the moment), or method, then easily run
• You can build a test suite (a thorough set of tests that can be run those tests
at any time)
• TestRunner runs tests and
• Disadvantages of a test suite
reports TestResults
• It’s a lot of extra programming
• You test your class by
• True, but use of a good test framework can help quite a bit
extending abstract class
• You don’t have time to do all that extra work TestCase
• False! Experiments repeatedly show that test suites reduce
debugging time more than the amount spent building the test suite • To write test cases, you
• Advantages of a test suite
need to know and
understand the
• Reduces total number of bugs in delivered code
Assert class
• Makes code much more maintainable and refactorable
45 46
47 48
12
Assert methods Assert methods
• assertTrue(String message, Boolean test)
• Assert methods dealing with floEach assert • assertFalse(String message, Boolean test)
method has parameters like these: • assertNull(String message, Object object)
message, expected-value, actual-value • assertNotNull(String message, Object object)
• assertEquals(String message, Object expected, Object actual)
• ating point numbers get an additional argument, (uses equals method)
a tolerance • assertSame(String message, Object expected, Object actual)
• Each assert method has an equivalent version (uses == operator)
• assertNotSame(String message, Object expected, Object
that does not take a message – however, this use actual)
is not recommended because:
• messages helps documents the tests
• messages provide additional information when
reading failure logs
49 50
51 52
13
TestSuites JUnit in Eclipse
• TestSuites collect a selection of tests to run them as a unit
• To create a test class,
• Collections automatically use TestSuites, however to specify the order
in which tests are run, write your own: select File→ New→
public static Test suite() {
suite.addTest(new TestBowl(“testBowl”));
Other... → Java, JUnit,
suite.addTest(new TestBowl(“testAdding”)); TestCase and enter the
return suite;
}
name of the class you
• Should seldom have to write your own TestSuites as each method in will test
your TestCase should be independent of all others
• Can create TestSuites that test a whole package:
public static Test suite() { Fill this in
TestSuite suite = new TestSuite();
suite.addTestSuite(TestBowl.class);
suite.addTestSuite(TestFruit.class);
return suite; This will be filled in
}
automatically
53 54
55 56
14
58
57 58
59 60
59 60
15
61 62
63 64
63 64
16
65 66
65 66
67 68
67 68
17
69 70
• Can add a failure message: assertNull(“Ptr isn’t null", value) High-level concept: test behaviors in combination
• expected is the oracle – remember this is the first (leftmost) Maybe add works when called once, but not when call twice
param Maybe add works by itself, but fails (or causes a failure) after calling remove
• The table above only describes when to fail – what happens if an
assertion succeeds? Does the test pass?
69 70
71 72
71 72
18
73 74
73 74
75 76
75 76
19
77 78
77 78
79 80
79 80
20
81
21