1、,C O N F I D E N T I A L Apr-17-08,Unit Test Concept & Coverage & Tool,Presented by Tao April 2008,C O N F I D E N T I A L Aug-10-07,Sections,Introduction to Unit Testing Code Test Coverage with CoberTura Q & A Resources,I. Introduction to Unit Testing,Introduction to automated unit tests,C O N F I
2、D E N T I A L Aug-10-07,C O N F I D E N T I A L Aug-10-07,Introduction to automated unit tests,Testing is the less glamorous part of software development “Testing is the ugly step-child of software development” eXtreme Programming eXplained, Kent Beck, Addison-Wesley, 2000 The dilemma of developer t
3、esting: Everyone knows that testing is important. Everyone knows they dont do enough testing. Manual testing is: Tedious high level of detail and discipline required, Error-prone easy to forget to run a test or run it incorrectly not repeatable Time intensive often testing is done at the end of a pr
4、oject, close to delivery. When time gets tight, it is difficult to do enough testing Cost to the project “The earlier the defects or bugs are found, the lower the cost to the project” Code Complete, second edition, Steve McConnell, Microsoft Press, 2004 Automated Unit Testing addresses these issues,
5、C O N F I D E N T I A L Aug-10-07,Unit Test Who and when,Unit Tests are written by the developers The developer who creates an Object is the most familiar with that Object and is in the best position to know how to test it correctly. Unit Tests are best written for an Object Before methods are writt
6、en Writing tests first often helps to define a good interface. As methods are written Implementation could be complicated and require extra testing. After methods are written Its impossible to think of everything. Add new tests not previously thought of. When new bugs are found When a bug is discove
7、red, write a test to expose it and prove it is fixed.,C O N F I D E N T I A L Aug-10-07,Unit Test Who runs and when,Unit Tests are run by the developers and Release/SCM Developers run the unit tests often. Ensuring changes made work correctly and dont mysteriously break the code elsewhere. Single un
8、it tests or small suites of tests after making changes. All tests for a module as part of the module release. Release/SCM team runs the unit tests as part of the automated build process: Ensuring the system is in a stable, working state and is ready for feature testing.,C O N F I D E N T I A L Aug-1
9、0-07,II. Code Test Coverage with CoberTura,What is unit test coverage Line Coverage vs. Branch Coverage vs. Complexity Coverage vs. Effort vs. Failures Why CoberTura CoberTura Usage Example CoberTura Report,C O N F I D E N T I A L Aug-10-07,What is unit test coverage,Unit test coverage defines how t
10、horough your unit tests are All conditions are tested for (if/else, try/catch, case/switch, etc.) How many times branches of code are run through Establishes confidence High % coverage means less chance for regression Allows refactoring without fear Shows quality of unit tests High quality unit test
11、s have high coverage (Not true versus, high coverage does not mean high quality necessarily, need more practices to increase the code quality but high coverage is the starting point.) Shows how useful unit tests are for documenting the usage of code for peer training,C O N F I D E N T I A L Aug-10-0
12、7,Line Coverage vs. Branch Coverage vs. Complexity,Line Coverage Indicates if each executable statement is encountered in test cases Does not indicate if conditions (if/else, try/catch, loop termination) are covered. Branch Coverage Indicates whether conditions are executed in test cases Does not in
13、dicate if early terminating condition (e.g. OR statements) are properly covered. Code Complexity Also known as McCabes cyclomatic code complexity algorithm Determines how complex a section of code is The higher the complexity number the more unit test coverage is required Measured by counting the nu
14、mber of paths there are through a section of code and comparing against an ordinal number (1).,C O N F I D E N T I A L Aug-10-07,Coverage vs. Effort vs. Failures,Increasing test coverage reaches a point of diminishing returns 100% line/branch coverage is impossible 90% is very time consuming but red
15、uces failures 60-80% about where testing, failures and effort are all close to ideal,Figure taken from http:/,C O N F I D E N T I A L Aug-10-07,McCabes Code Complexity,How to interpret McCabes code complexityStarCite should try to keep code complexity below 15 Refactor existing code to get to this l
16、evel of complexity Code complexity above 15 is too hard to test MKV and MV has some code in the 20s all areas with high bug counts or instability McCabes code complexity directly corresponds with the bugginess, stability and testability of software Software refactored for testability = less buggy an
17、d more stable,C O N F I D E N T I A L Aug-10-07,Why CoberTura,The StarCite Architecture team has chosen CoberTura as its unit test coverage tool for the following reasons Open source hybrid GPL and ASL license Generates easy to read and transform reports in HTML and/or XML Can write custom report ha
18、ndlers to plug in to CoberTura Integrates well with ANT Easy to automate Identifies in actual source code which parts are lacking in test coverage Identifies line coverage, branch coverage and McCabes complexity to help developers determine which parts of code require more unit tests and/or refactor
19、ing Compatible with JDK5 and 6 Can be integrated with app. servers for Servlet/Web App coverage reports driven from HttpUnit tests,CoberTura Usage,Integrated/automated with G3.1 for over a year To run execute from G3.1 top directory:Default coverage report is HTML Output can be found in gen/test/cov
20、erage/index.html,cd unittest ant coverage-test,C O N F I D E N T I A L Aug-10-07,Example CoberTura Report,C O N F I D E N T I A L Aug-10-07,Example CoberTura Report (contd),C O N F I D E N T I A L Aug-10-07,Example CoberTura Report (contd),C O N F I D E N T I A L Aug-10-07,Real example from Kestrel,
21、http:/ O N F I D E N T I A L Aug-10-07,III. Q&A,C O N F I D E N T I A L Aug-10-07,IV. Resources,Useful Resources From StarCite BaseCamp: StarCite unit test best practices - https:/ Web pages JUnit site - http:/www.junit.org/ JUnit Primer - http:/ EasyMock site http:/www.easymock.org CoberTura site - http:/ Books Java Extreme Programming Cookbook, by Eric M. Burke, Brian M. Coyner Code Complete, Second Edition, Steve McConnell,