Unit Tests Generator team:
Artur Hefczyc
API documentation:
Here
WTTools site:
More WTTools
Lets assume you started developing your application some weeks ago or some months ago or even some years ago. I am sure that testing your code was one of major problems. You thought about automating such process. And than you heard about Unit Testing. So you would like to start use it. However how to introduce Unit Testing for large number of source files. There is too much of stupid code to write in all places again.
Here comes this package. His main goal is save you from wasting time for creating test classes for your all source files. It takes as an argument sources directory and than performs some actions which, result with test classes.
Although by default it doesn't overwriting existing files remember that it is test
version of application and always make backup of you project before use it.
unittestsgen.jar
ommits all files which, name beggining with 'Test'
string or which, are extending TestCase
class from JUnit
package.
All new classes are stored in files named TestSrcOriginalName.java.
If you run application with no arguments it display short description of all available
parameters:
[user] $ java -jar unittestsgen.jarThe most common options are:
java -jar jarfile.jar
and there is no way to pass CLASSPATH in standard way I provided option to add classpath
through programm interface. Here you should put path to junit.jar
your
classes compiled version or if you have also test classes you should put here jar file
which contain compiled versions of your test classes. It allow to update code in your
existing test classes.unittestsgen.jar
with no arguments and check defaults values. They are
designed to be useful for most cases, so the most commonly you should use application
in the following way:[user] $ java -jar jar/unittestsgen.jar -cp "yourjarfile.jar:libs.jar:junit.jar"In MS windows like systems:
D:\projects\yourproject> java -jar unittestsgen.jar -cp "yourjarfile.jar;libs.jar:junit.jar"Where
libs.jar
contains all necessary libraries used by your application.
Of course if you use many libraries from many jar files you should put all necessary jar
files in '-cp' class path parameter or you will be unable to generate test classes.
More info you can find in GenerateTests class documentation.
[user] $ java -cp "yourjarfile.jar:libs.jar:junit.jar" full.class.NameIn MS windows like systems:
D:\projects\yourproject> java -cp "yourjarfile.jar;libs.jar;junit.jar" full.class.NameFile 'yourjarfile.jar' must contain compiled both your project classes and of course all generated 'Test' classes. And 'libs.jar' must contain all necessary by your application libraries. Of course you can put there many jar files instead of single 'libs.jar'.
TestAll.class
which, runs all available tests:[user] $ java -cp "yourjarfile.jar:libs.jar:junit.jar" TestAllIn MS windows like systems:
java -cp "yourjarfile.jar;libs.jar;junit.jar" TestAllFile 'yourjarfile.jar' must contain compiled both your project classes and of course all generated 'Test' classes. And 'libs.jar' must contain all necessary by your application libraries. Of course you can put there many jar files instead of single 'libs.jar'.
build.xml
file to tell Ant how to test
your package. Sample code for Ant's build.xml
you can find in
created file: sample-junit-build.xml
. And remember to have
junit.jar
in CLASSPATH before you run Ant.xalan.jar
in your
'ANT_HOME/lib/'
directory.
Although running 'unittestsgen.jar' from command line is very simple and easy it is not very efficient way for hard developing process. In current version it is possible to use this tool as ANT task. If you prepare your 'build.xml' file correctly your test classes will be updated permanently every time you start build process. So you can have your test classes up to date all time during your development process.
In my projects I use to run unit tests generator every time I build JAR file. In particular after successfully building JAR file. Below you can find complete sample 'build.xml file for using unit tests generator during your development.
To use it in your developing process you must define new task in your
build.xml
. To do this put in the beggining of your build file such lines:
<taskdef name="unitgen" classpath="some_dir/unittestsgen.jar" classname="testsgen.taskant.UnitTestsGen"/>Where, of course,
'some_dir'
is directory where 'unittestsgen.jar'
file exists.build.xml
sample.
<project name="UnitTestsGen" default="dist" basedir="."> <taskdef name="unitgen" classname="testsgen.taskant.UnitTestsGen"/> <property name="src" value="src"/> <property name="test-src" value="test-src"/> <property name="build" value="build"/> <target name="compile" description="Compile sources in ${tmpsrc} directory"> <mkdir dir="${build}"/> <javac srcdir="${src};${test-src}" destdir="${build}" debug="on" deprecation="off"> <classpath> <pathelement location="${libs}/junit.jar"/> </classpath> </javac> </target> <target name="jar" depends="compile"> <jar jarfile="jar/${jarfile}.jar" manifest="MANIFEST.MF" basedir="${build}"> <exclude name="** /*TestCase*.*"/> <exclude name="TestAll.*"/> </jar> <unitgen update="true" classpath="${build};${libs}/junit.jar" imput="${src}" output="${test-src}"/> </target> </project>After excecuting command
'ant jar'
you should see similar output:
compile: [javac] Compiling 36 source files to /home/kobit/projects/unittestsgen/build jar: [jar] Building jar: /home/kobit/projects/unittestsgen/jar/unittestsgen.jar [unitgen] Finished work: files 21, sources 14, new classes 3, new methods 7 BUILD SUCCESSFULEvery time you will be building jar file all your source classes will be checked against changes made and test classes will be updated if necessary.
More info about using this package you can find in javadoc api documentation in particular I recomend documentations for these two classes: UnitTestsGen and GenerateTests.
If you have any problems with this application or you found any bugs or you have
any ideas about extending this package, want to join to project or you simply need
to contact me please send e-mail to addres:
Artur Hefczyc kobit@users.sf.net