Download presentation
Presentation is loading. Please wait.
1
5/9/2018 Advanced Unit Testing David Rabinowitz
2
Object Oriented Design Course
5/9/2018 Test Coverage Tests are imperative for developing How to write tests How to know we have all important code covered? What are possible risks 5/9/2018 Object Oriented Design Course
3
Object Oriented Design Course
5/9/2018 What to test Correct results Cross checking Error conditions Boundaries Performance 5/9/2018 Object Oriented Design Course
4
Code coverage checking
5/9/2018 Code coverage checking How do we know that we have our critical code covered Statement coverage Branch coverage Path coverage How to make sure it will be done automatically 5/9/2018 Object Oriented Design Course
5
Object Oriented Design Course
5/9/2018 Example public int foo(int x, boolean b1, boolean b2) { if (b1) { x++; } if (b2) { x = -x; return x; 5/9/2018 Object Oriented Design Course
6
Object Oriented Design Course
5/9/2018 Tests Statement Coverage testTT() {int x=foo(0,true,true); ...} Branch Coverage testFF() {int x=foo(0,false,flase); ...} Path Coverage testTT(), testTF(), testFT(), testFF() 5/9/2018 Object Oriented Design Course
7
Object Oriented Design Course
5/9/2018 Test coverage tools Monitor the tests Create a report of executed lines Examples Cobertura Clover many many more... 5/9/2018 Object Oriented Design Course
8
Object Oriented Design Course
5/9/2018 How they work Placing special markers in the code A post processor changes the generated bytecode The instrumented bytecode is put to a different directory The tests are run on the instrumented code 5/9/2018 Object Oriented Design Course
9
Object Oriented Design Course
5/9/2018 Pitfalls Having high test coverage can lead to the (false) idea that our code is safe Tests may be covering 100% of the code, but not all the cases 5/9/2018 Object Oriented Design Course
10
Object Oriented Design Course
5/9/2018 Example 1 public class PathCoverage { public String pathExample(boolean condition){ String value = null; if(condition){ value = " " + condition + " "; } return value.trim(); Taken from 5/9/2018 Object Oriented Design Course
11
Object Oriented Design Course
5/9/2018 Example 1 Test public class PathCoverageTest extends TestCase { public final void testPathExample() { PathCoverage pt = new PathCoverage(); String value = pt.pathExample(true); assertEquals("should be true", "true", value); } 5/9/2018 Object Oriented Design Course
12
Example 1 Coverage Report
5/9/2018 Example 1 Coverage Report 5/9/2018 Object Oriented Design Course
13
Object Oriented Design Course
5/9/2018 Example 2 public class AnotherBranchCoverage { public void branchIt(int value){ if((value > 100) || (HiddenObject.doWork() == 0)){ this.dontDoIt(); }else{ this.doIt(); } private void dontDoIt(){ ... } private void doIt(){ ... } 5/9/2018 Object Oriented Design Course
14
Object Oriented Design Course
5/9/2018 Example 2 public class HiddenObject { public static int doWork(){ //return 1; throw new RuntimeException("surprise!"); } 5/9/2018 Object Oriented Design Course
15
Object Oriented Design Course
5/9/2018 Example 2 Test public class AnotherBranchCoverageTest extends TestCase { public final void testBranchIt() { AnotherBranchCoverage abc = new AnotherBranchCoverage(); abc.branchIt(101); } 5/9/2018 Object Oriented Design Course
16
Example 2 Coverage Report
5/9/2018 Example 2 Coverage Report 5/9/2018 Object Oriented Design Course
17
Object Oriented Design Course
5/9/2018 Summary Test coverage is a mean to check out the quality of our tests Line coverage Branch coverage Can be easily integrated to build scripts It cannot be taken for granted Results should be carefully examined 5/9/2018 Object Oriented Design Course
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.