1 Software Development ToolsCOMP220 Seb Coope Ant, Testing and JUnit Capturing test results These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran. Manning Publications, 2003
2 Capturing test resultsThe
3 Capturing test resultsAnt
4 Capturing test resultsBy default,
5 Capturing test resultsH:\Antbook\ch04>ant -f mybuild.xml test-brief Buildfile: C:\Antbook\ch04\mybuild.xml [echo] Building Testing Examples
6 Capturing test results[junit] Testcase: testRead(org.eclipseguide.persistence. FilePersistenceServicesTest): FAILED [junit] expected:<[One, Two, Three]> but was:
7 Capturing test resultsNow we’re getting somewhere: tests run as part of our regular build, test failures cause our build to fail: BUILD FAILED we get enough information to see what is going on. By default, formatters write their output to files either in the base directory of the build file, or in the directories specified in the
8 Capturing test resultsAlso, we turned off the printsummary option as it duplicates and interferes with the output in the console from the brief or plain formatter. In case of usefile="true", it makes sense to turn on the printsummary (to the console). xml formatter is better to use with usefile="true". It generates a huge XML output by listing all Ant’s properties The
9 Add this as a NEW target in mybuild.xmlXML formatter Saving the results to XML files lets you process them in a number of ways (e.g. transforming to HTML). Our testing task and target now evolves to this:
10 C:\Antbook\ch04>ant -f mybuild.xml clean test-xmlXML formatter (cont.) The effect of the above is to create an XML reports in the ${test.data.dir} (i.e., build/data) directory for each test case run. In our example, we get in build/data one (it could be more) XML file named like (actually, one line) TEST-org.eclipseguide.persistence. FilePersistenceServicesTest.xml. Note that for this to work we should use
11 Running multiple tests under
12 Running multiple tests under
13 Running multiple tests under
14 Running multiple tests under
15 Notes on Terminology Unfortunately the terminology of various textbooks on Ant on of our lectures differs from that used in the in Ant’s console output: Our Test Cases are called Testsuites in Ant’s console output. Our Test Methods are called Tests in Ant’s console output. 4.7
16 Generating (HTML) test result reportsWith test results written to XML files, it is straightforward to generate HTML reports (by using XSLT). The task
17 Generating (HTML) test result reportsGenerating HTML report consists of: placing the
18 Generating (HTML) test result reportsWe aggregate all generated "TEST-*.xml" files since that is the default naming convention used by the XML formatter of
19 Generating (HTML) test result reportsAdd the above
20 But this does not allow to createGenerating all test reports and enforcing the build to fail in case of failures We know that haltonfailure="yes" forces build to fail and, actually, to halt if any of the tests fails. But this does not allow to create all TEST-*.xml files, the aggregated TESTS-TestSuites.xml file, and the HTML report. As a solution, turn off haltonfailure in order for the XML and HTML reports to be generated as above before the build halts, and additionally, enforce build failure, after generating XML and HTML reports, by setting a specified property test.failed upon a test failure or error: use the failureProperty and errorProperty attributes of the
21 (
22 Generating all test reports and enforcing the build to fail in case of failures
23 Generating (HTML) test result reportsOpen C:\Antbook\ch04\build\test\reports\index.html Test methods Test cases the default frames This is the generated main page, index.html, by
24 Generating (HTML) test result reportsNavigating to a specific test case FilePersistenceServicesTest displays: A specific test case results: Test methods and details of corresponding assertion that failed are clearly shown. Clicking the Properties » shows all of Ant’s properties at the time the tests were run.
25 Generating (HTML) test result reportsSelf-study Clicking the Properties » above shows all of Ant’s properties at the time the tests were run. These can be handy for troubleshooting failures caused by environmental or configuration issues. NOTE. There are some issues with
26 Running a single test case from the command-lineSelf-study While a project can have many test cases, you may need to isolate a single test case to run when ironing out a particular issue. This can be accomplished using the if/unless attributes on
27 Running a single test case from the command-lineSelf-study
28 Running a single test case from the command-lineSelf-study By default, testcase property will not be defined and, therefore, the
29 About testing again Now, having Ant and JUnit tools, we can summarise how test-driven programming can be done: Writing and automated running test cases may actually improve the design of our production code. In particular, if you cannot write a test case for a class, you have a serious problem, as it means you have written untestable code. Hope is not lost if you are attempting to add testing to a large system on later stages . Do not attempt to incorporate test cases for the existing code in one big go. (that was built without unit tests in place)
30 About testing again Before adding new code, write teststo validate the current behaviour to describe (specify) the expected behaviour on new code to be added, and to verify that the new code does not break the current behaviour and demonstrates the correct new behaviour. When a bug is found, write a test case or a test method to identify it clearly, then fix the bug and watch the test passes. While some testing is better than no testing, a critical mass of tests needs to be in place to truly realize such XP benefits as fearless and confident refactoring. Keep at it and the tests will accumulate little-by-little allowing the project to realize these and other benefits.
31 Self-study Extensions to JUnit Partly discussed earlier.It is easy to build extensions on top of JUnit. There are many freely available extensions and companions for JUnit. This table shows a few: Name Description HttpUnit A test framework that could be embedded in JUnit tests to perform automated web site testing. JUnitPerf JUnit test decorators perform scalability and performance testing. Mock Objects Allows testing of code that accesses resources such as database connections and servlet containers without the need of the actual resources. Cactus In-container unit testing. Covered in detail in chapter 12 of Ant book. DBUnit Sets up databases in a known state for repeatable DB testing. Table 4.1 Because of its architecture, it is easy to build extensions on top of JUnit.
32 BRIEF SUMMARY to Ant+JUnitJUnit is Java’s de facto testing framework; it integrates tightly with Ant.
33 BRIEF SUMMARY to Ant+JUnitThere is a lot more on Ant and JUnit what we had no time to discuss. Read Ant book and other materials presented in the Web site of COMP220. 4.10 Unit testing makes the world a better place because it gives us the knowledge of a change’s impact and the confidence to refactor without fear of breaking code unknowingly. Here are some key points to keep in mind: Information can be passed from Ant to test cases via