fu agile#2 unit_testing
TRANSCRIPT
![Page 1: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/1.jpg)
Nguyen Ngoc [email protected] Hoa Lac, 3- 2013
![Page 2: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/2.jpg)
Objectives
oWhy testing?
oBasic principles
oHow to choosing test case
oTest-First Programming
o Introduction to Junit
oLet’s do together!!!!
FUAgile: Unit Testing Basic 2
![Page 3: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/3.jpg)
FUAgile: Unit Testing Basic 3
CRISIS
Numbers:4.4 million cars recalledBillions of $ loss.Damage the brand
![Page 4: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/4.jpg)
Desire vs. Reality
FUAgile: Unit Testing Basic 4
Requirement\Design
Desired
Implementation
Reality
testing
gap
![Page 5: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/5.jpg)
Why testing?
oEnsuring QUALITY
oManaging RISKS
oOptimizing Return-On-Investment (ROI)
o Professionalism
FUAgile: Unit Testing Basic 5
![Page 6: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/6.jpg)
Typical Production model
Requirement
Design
Implement
Test
FUAgile: Unit Testing Basic 6
![Page 7: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/7.jpg)
Bug cost
FUAgile: Unit Testing Basic 7
![Page 8: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/8.jpg)
Testing level
oUnit testing
o On individual unit of source code (function, class, etc.)
o Smallest testable part
o Developer testing
o Integration testing
o On groups of modules
o System testing
o On complete integrated system
FUAgile: Unit Testing Basic 8
![Page 9: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/9.jpg)
Basic Principles
oBe systematic
o Haphazard testing & exhaustive testing are impossible.
o test cases must be chosen carefully and systematically
oDo it early and often
o Don’t leave testing until the end debugging longer and
more painful
o test-first programming
FUAgile: Unit Testing Basic 9
![Page 10: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/10.jpg)
Basic Principles
o Automate it
o Nothing makes tests easier to run, and more likely to be
run, than complete automation.
o For regression test
FUAgile: Unit Testing Basic 10
![Page 11: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/11.jpg)
Why Testing is Hard
o We want to
o know when product is stable enough to launch
o deliver product with known failure rate (preferably low)
o offer warranty?
FUAgile: Unit Testing Basic 11
![Page 12: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/12.jpg)
Why Testing is Hard
oBut
o it’s very hard to measure or ensure quality in software
o residual defect rate after shipping:
• 1 - 10 defects/kloc (typical)
• 0.1 - 1 defects/kloc (high quality: Java libraries?)
• 0.01 - 0.1 defects/kloc (very best: Praxis, NASA)
o exhaustive testing is infeasible
o statistical testing doesn’t work for software
FUAgile: Unit Testing Basic 12
![Page 13: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/13.jpg)
FUAgile: Unit Testing Basic 13
Find bugs as cheaply and quickly as possible!
![Page 14: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/14.jpg)
Choosing Test Cases
oKey Idea #1: Partition the Input Space
o input space is very large, but program is small so
behavior must be the “same” for whole sets of inputs
o ideal test suite
o identify sets of inputs with the same behavior
o try one input from each set
FUAgile: Unit Testing Basic 14
![Page 15: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/15.jpg)
Choosing Test Cases
Ex: multiply : BigInteger x BigInteger BigInteger
partition BigInteger into:
BigNeg, SmallNeg, -1, 0, 1, SmallPos, BigPos
pick a value from each class
-265, -9 -1, 0, 1, 9, 265
test the 7 × 7 = 49 combinations
FUAgile: Unit Testing Basic 15
![Page 16: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/16.jpg)
Choosing Test Cases
Ex2: max : int vs int int
partition into:
a < b, a = b, a > b
pick value from each class
(1, 2), (1, 1), (2, 1)
FUAgile: Unit Testing Basic 16
![Page 17: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/17.jpg)
Choosing Test Cases
0
FUAgile: Unit Testing Basic 17
![Page 18: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/18.jpg)
Choosing Test Cases
oKey idea #2: Boundary testing
o include classes at boundaries of the input space
zero, min/max values, empty set, empty string, null
o why? because bugs often occur at boundaries
o off-by-one errors
o forget to handle empty container
o overflow errors in arithmetic
FUAgile: Unit Testing Basic 18
![Page 19: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/19.jpg)
Choosing Test Cases
EX: Find max value in a list of nonnegative integers. Assuming
that list is not empty.
public static int max(List<Integer> l) { ... }
list length: length 0, length 1, length 2+
max position: start, middle, end of list
value of max: MIN_INT, negative, 0, positive, MAX_INT
FUAgile: Unit Testing Basic 19
![Page 20: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/20.jpg)
Coverage
o all-statements: is every statement run by some test case?
(common goal)
o all-branches: if every direction of an if or while statement (true
or false) taken by some test case?
o all-paths: is every possible combination of branches – every
path through the program – taken by some test case?
FUAgile: Unit Testing Basic 20
![Page 21: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/21.jpg)
Test-first programming
2 approaches:
Code Tests Fix bugs (painful)
Write tests Code
FUAgile: Unit Testing Basic 21
Test-first programming
What is your choice?
![Page 22: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/22.jpg)
Test-first programming
write tests before coding
specifically, for every method or class:
1) write specification
2) write test cases that cover the spec
3) implement the method or class
4) once the tests pass (and code coverage is sufficient), you’re done
FUAgile: Unit Testing Basic 22
![Page 23: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/23.jpg)
JUnit
oUnit testing automation framework for Java
oCan do integration test
o Integrated in IDEs (Eclipse, NetBeans, IntelliJ, etc.)
oMust have tool for Java developers
FUAgile: Unit Testing Basic 23
http://www.junit.org/
![Page 24: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/24.jpg)
JUnit
oA JUnit test case start with @Test
oTest case name start with testXXX()
oUsing assertEquals, assertTrue, assertFalse…
@Test
public void testMaxAtStart(){
ArrayList<Integer> list = new ArrayList<>();
//add 5,2,3 to list
assertEquals(MyClass.Max(list), 5);
}FUAgile: Unit Testing Basic 24
![Page 25: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/25.jpg)
JUnit
o If you expect an exception using
@Test(expected = SomeTypeOfException.class)
@Test(expected = IllegalArgumentException.class)
public void testMaxAtStart(){
ArrayList<Integer> list = new ArrayList<>();
MyClass.Max(list)
}
FUAgile: Unit Testing Basic 25
![Page 26: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/26.jpg)
Let’s do togetherUsing Test-First programming
Ex1: Write a function with parameter is an integer n, if n<0 throw
IllegalArgumentException; otherwise
• if n%3==0 return “Fizz”;
• If n%5==0 return “Buzz”;
• If n%3==0 and n%5==0 return “FizzBuzz”;
• Else return the number itself
public static String fizzBuzz(int n) { ... }
FUAgile: Unit Testing Basic 26
![Page 27: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/27.jpg)
Let’s do together
Using Test-First programming
Ex2: Find max value in a list of nonnegative integers. Assuming
that list is not empty.
public static int max(List<Integer> list) { ... }
FUAgile: Unit Testing Basic 27
![Page 28: Fu agile#2 unit_testing](https://reader031.vdocument.in/reader031/viewer/2022032113/55a42c8f1a28ab595c8b46e9/html5/thumbnails/28.jpg)
Let’s do together
Using Test-First programming
Ex3: Find the union of 2 arrays of integers.
public static int[] union(int[] arr1, int[] arr2) { ... }
FUAgile: Unit Testing Basic 28