Download presentation
Presentation is loading. Please wait.
1
© S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University
2
© S Ramakrishnan2 A day in the life of a Test Driven Development (TDD) Practitioner Check out a module from the project’s src repository run all the unit tests on the repository to see if they pass Then, write new tests for the new use case (TDD) (or write the code first & then the tests – non TDD) with new tests written first, compile/run will error as no such use case is supported in the apps yet. write the relevant code to implement the use case & show it is correct with a green bar in JUnit, then, you can check in the code & the tests. must continually run the unit tests as part of the dev. cycle for regression testing. Hence, tests should be able to be run automatically.
3
© S Ramakrishnan3 Automating JUnit and Running Tests from Ant Consider problems that can arise during compilation in a large project with mutiple classes with just tools such as a standard javac compiler: no. of classes refer to each other, more classes need to be on the classpath so complier can find them. In a build where mods to only some classes, which classes to build? Which JUnit tests to run after each build? Developers use Ant, Maven & Eclipse for building applications and for automating JUnit tests. Running JUnit in command line mode is difficult for running continuous tests in a project with a no. of classes. With lots of test & production classes in a project, error prone to run entire set of regression tests by hand. Need an effective way to write & run tests.
4
© S Ramakrishnan4 Automating JUnit Ant is a defacto standard tool for building Java apps & for managing & automating JUnit tests. Ant is good for running JUnit regression tests. Ant is a framework for running tools. Can use Ant to configure & launch a Java compiler, for generating code, invoking JDBC queries and for running JUnit test suites. Maven extends Ant and provides a wider project mgmt support. Most Java IDEs work with build tools like Ant & Maven. Many developers use IDE (e.g. Eclipse) to create & test code, and then use Ant and Maven for distribution or deployment of the latest version.
5
© S Ramakrishnan5 Ant Apache’s Ant (http://ant.apache.org/) is an open-source Java based build tool that lets you easily compile & test appl.http://ant.apache.org/ Equivalent to unix “make”. Ant is based on XML scripts. Included in Eclipes & other Java IDEs. buildfile Ant is configured through a buildfile named build.xml by default. Can use Ant : to build the application to be tested; compile the JUnit test cases, run the JUnit test cases format the results
6
© S Ramakrishnan6 Ant buildfile Manage projects. Manage the build process with Ant by creating a buildfile for each of your projects. Project Project: a set of “targets” associated with a project – requires default target to be specified, which is run, if no target is specified by the user. Target Target: a specific function with a reference name, eg. compile, test may have dependencies e.g before running JUnit tests, appl. & related JUnit tests must have compiled successfully buildfile can have several targets or entry points, and encapsulates the various tasks required to create your apps & required resources. You can run one task or chain a number of tasks together.
7
© S Ramakrishnan7 Ant buildfile task Ant buildfile describes each task that you want to apply to a project. the task might be: compile src, generate Javadocs, query DB, transfer or copy files, or run tests. A number of tasks come bundled with Ant. You can also write your own tasks. Ant Tasks are: Compile: javac Execute: java, exec Archive: zip, unzip, jar, … Test: JUnit, JUnitReport File: copy, move, delete, mkdir, … Documentation: javadoc Other: property, antlr, xslt…
8
© S Ramakrishnan8 Ant buildfile property elements For reuse & easier configuration, you can define dynamic property elements in an Ant buildfile. task you can use property elements to set the parameters a tool needs and a task to run the tool. to refer to a property in a buildfile, place the property within a special notation: ${property}. project, property elements, targetstasks So, you have in a buildfile: project, property elements, targets & tasks
9
© S Ramakrishnan9 Ant buildfile : Compile The snippets of code used in these set of slides is from the textbook, JUnit in Action, V Massol, Mannings Publ. 2004 available in the library. Build.xml could have the Ant script shown below: compile all files under dir src with suffix *.java, and put the.class files into the dir called bin <javac fork=“yes” // set fork to yes to force Ant to use a separate JVM for each test // good practice as it avoids interferences between test cases srcdir=“${src.dir}” includes=“**/*.java” destdir=“${bin.dir}”>
10
© S Ramakrishnan10 Ant buildfile : Compile targets Compile targets that call the java compiler for a project XXX, both for the production and the test code. // this file contains Ant properties that // may be changed in the user’s system due to executing environment // eg. Location of redistributable jars. Many open source projects // provide a build.properties.sample you can copy & edit for use // define properties related // to the //source tree // define properties <property name=“target.classes.java.dir” // related location=“${target.dir}/classes/java”/> // to // output tree …
11
© S Ramakrishnan11 Compile targets that call the java compiler for a project XXX, both for the production and the test code. // declare the target to compile the java // production sources and name it compile.java // make sure dir exists for //.class // call java compiler & // pass it destination dir to use // tell java compiler which sources to compile // compile.test target has // a dependency on compile.java target //add a nested classpath element to add the production classes //you just // compiled to the classpath, as testclasses call prod.classes // create a compile // target that automatically calls the compile.java & compile.test targets
12
© S Ramakrishnan12 JUnit Task Compile src & run test cases against the compiled classes as part of the same build target: // if Ant is asked to run the test target, Ant will run // the compile target before running test <junit printsummary=“yes” haltonerror=“yes” haltonfailure=“yes” // printsummary attribute // says render 1 line summary at the end of the test // haltonfailure & haltonerror attributes say the build should stop if // tests return a failure (one of the asserts did not pass) or // an error (due to unexpected error) fork=“yes” > //set fork to yes to force Ant to use a separate JVM for each test // configure junit task formatter to use // plain text and output the test result to the console // provide class name // of the test you want to run //add a nested classpath element to add the production & test classes
13
© S Ramakrishnan13 Once the buildfile is ready, can run Ant from the command line like so: c:\cse4431\stud-id-assignment1>ant Buildfile: build.xml compile.java: [mkdir] created dir: c:\cse4431\stud-id-assignment1\target\classes\java [javac] compiling xx source files to c:\cse4431\stud-id-assignment1\target\classes\java compile.test: [mkdir] created dir: c:\cse4431\stud-id-assignment1\target\classes\test [javac] compiling yy source files to c:\cse4431\stud-id-assignment1\target\classes\test compile: test: [mkdir] created dir: c:\cse4431\stud-id-assignment1\target\classes\report [junit] Running the test … [junit] Tests run: 2. Failures: 0. Errors: 0. Time elapsed: xx.xx sec [junit] Testsuite: …class [junit] Tests run: 2. Failures: 0. Errors: 0. Time elapsed: xx.xx sec [junit] Testcase: testaa-1 took ab.cd sec [junit] Testcase: testaa-2 took ac.bd sec BUILD SUCCESSFUL Total time: 10 seconds C:\cse4431\stud-id-assignment1
14
© S Ramakrishnan14 JUnit and Ant junit task is one of the components bundled in Ant’s optional jar. optional jar file is for tasks that depend on another package, e.g JUnit Ant does not bundle a copy of JUnit. Make sure junit.jar is in your system classpath or in the ANT_HOME/lib dir remember to update junit.jar in ANT_HOME/lib when installing a new version of either Ant or JUnit.
15
© S Ramakrishnan15 Pretty printing with JUnitReport An optional Ant task, junitreport, is designed to output the results of the tests as XML. Junitreport renders the XML into HTML using XSL stylesheet to produce a pretty printed report. Changes shown in bold. […] <property name=“target.report.dir” location=“${target.dir}/report”/> […] target name=“test” depends “compile”> // if Ant is asked to run the test target, Ant will run // the compile target before running test // create the report dir <junit printsummary=“yes” haltonerror=“yes” haltonfailure=“yes” fork=“yes” > // modify junit task so its output test results are in XML todir=“${target.report.dir}”/> // tell the junit task to create the report in this dir //add a nested classpath element to add the production & test classes contd next slide
16
© S Ramakrishnan16 Pretty printing with JUnitReport // create a new report target to generate html report // create dir where html will be generated // call junitreport task to create the report //junitreport task works by scanning the list of // XML test results you specify as an Ant fileset // tell junitreport task where to produce // the HTML report
17
© S Ramakrishnan17 Batchtest element – Automatically finding the tests to run junit task has a batchtest element that lets you specify testcases using wildcards. Refer to JUnit in Action for more information
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.