Fu agile#2 unit_testing


Published on

Published in: Health & Medicine
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • I don’t want to frustrate you by bunch of testing terms. Just focus on thinking, process and how-tos.
  • Global: 4.4 million cars recalled (http://wsws.org/articles/2010/feb2010/toyo-f12.shtml )In the first quarter of 2009, the company posted a $7.7 billion loss. The estimated loss for Toyota in the current financial year, calculated before the expansion of the recall process in January, is $5.5 billion.http://parttimembadegree.com/business-school-cases/toyota-recalls-pr-management-crisis/http://en.wikipedia.org/wiki/2009%E2%80%932011_Toyota_vehicle_recallsVN: “mua toy chonólành” – lost
  • For ROI optimization, see the analysis of Rex Black on Managing the Testing Process: Practical Tools and Techniques for Managing Hardware and Software Testing
  • In conclusion: Test a.s.a.pTesting is responsibilities of all including developers.
  • Fu agile#2 unit_testing

    1. 1. Nguyen Ngoc Anhanhnn3@fpt.com.vnFu Hoa Lac, 3- 2013
    2. 2. Objectiveso Why testing?o Basic principleso How to choosing test caseo Test-First Programmingo Introduction to Junito Let’s do together!!!! FUAgile: Unit Testing Basic 2
    3. 3. CRISISNumbers:4.4 million cars recalledBillions of $ loss.Damage the brand FUAgile: Unit Testing Basic 3
    4. 4. Desire vs. Reality Desired RealityRequirement gap Implementation Design testing FUAgile: Unit Testing Basic 4
    5. 5. Why testing?o Ensuring QUALITYo Managing RISKSo Optimizing Return-On-Investment (ROI)o Professionalism FUAgile: Unit Testing Basic 5
    6. 6. Typical Production model Requirement Design Implement Test FUAgile: Unit Testing Basic 6
    7. 7. Bug costFUAgile: Unit Testing Basic 7
    8. 8. Testing levelo Unit testing o On individual unit of source code (function, class, etc.) o Smallest testable part o Developer testingo Integration testing o On groups of moduleso System testing o On complete integrated system FUAgile: Unit Testing Basic 8
    9. 9. Basic Principleso Be systematic o Haphazard testing & exhaustive testing are impossible. o test cases must be chosen carefully and systematicallyo Do 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
    10. 10. Basic Principleso 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
    11. 11. Why Testing is Hardo 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
    12. 12. Why Testing is Hardo But 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
    13. 13. Find bugs as cheaply and quickly as possible! FUAgile: Unit Testing Basic 13
    14. 14. Choosing Test Caseso Key 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
    15. 15. Choosing Test CasesEx: multiply : BigInteger x BigInteger  BigIntegerpartition BigInteger into: BigNeg, SmallNeg, -1, 0, 1, SmallPos, BigPospick a value from each class -265, -9 -1, 0, 1, 9, 265test the 7 × 7 = 49 combinations FUAgile: Unit Testing Basic 15
    16. 16. Choosing Test CasesEx2: max : int vs int  intpartition into: a < b, a = b, a > bpick value from each class (1, 2), (1, 1), (2, 1) FUAgile: Unit Testing Basic 16
    17. 17. Choosing Test Cases0 FUAgile: Unit Testing Basic 17
    18. 18. Choosing Test Caseso Key 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
    19. 19. Choosing Test CasesEX: Find max value in a list of nonnegative integers. Assumingthat 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
    20. 20. Coverageo 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
    21. 21. Test-first programming2 approaches:Code  Tests  Fix bugs (painful)Write tests  Code Test-first programming What is your choice? FUAgile: Unit Testing Basic 21
    22. 22. Test-first programmingwrite tests before codingspecifically, 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
    23. 23. JUnito Unit testing automation framework for Javao Can do integration testo Integrated in IDEs (Eclipse, NetBeans, IntelliJ, etc.)o Must have tool for Java developers http://www.junit.org/ FUAgile: Unit Testing Basic 23
    24. 24. JUnito A JUnit test case start with @Testo Test case name start with testXXX()o Using assertEquals, assertTrue, assertFalse…@Testpublic void testMaxAtStart(){ ArrayList<Integer> list = new ArrayList<>(); //add 5,2,3 to list assertEquals(MyClass.Max(list), 5);} FUAgile: Unit Testing Basic 24
    25. 25. JUnito 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
    26. 26. Let’s do togetherUsing Test-First programmingEx1: Write a function with parameter is an integer n, if n<0 throwIllegalArgumentException; 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 itselfpublic static String fizzBuzz(int n) { ... } FUAgile: Unit Testing Basic 26
    27. 27. Let’s do togetherUsing Test-First programmingEx2: Find max value in a list of nonnegative integers. Assumingthat list is not empty.public static int max(List<Integer> list) { ... } FUAgile: Unit Testing Basic 27
    28. 28. Let’s do togetherUsing Test-First programmingEx3: Find the union of 2 arrays of integers.public static int[] union(int[] arr1, int[] arr2) { ... } FUAgile: Unit Testing Basic 28