SourceForge Logo

Unit Tests Generator

Unit Tests Generator team:   Artur Hefczyc  
API documentation:   Here
WTTools site:   More WTTools

What for?

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.

  1. Searching in given directory for all source files. You can put as a parameter file mask which, can improve finding source files.
  2. Parsing sources to structures which, can be processed later.
  3. Generates test files for each given source.

What now?

At the moment I have ready to use source parser and code generator for Java language and JUnit package. There are many extensions I can see in this version but at the moment it is ready to use for simple test generating.
But it is very modular application and it very easy to use code parsers and generator for other languages and unit test systems. It is necessary only to create class which, performs proper actions and returns results. Using different parsers and generators is very easy only by passing class name as a parameter to command line arguments.
If you are interested in other languages support please let me know. If you are interested in creating pasrsers or generators for other languages and unit tests please let me know or join my project.

How can I use it?

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
If you run application with no arguments it display short description of all available parameters:

[user] $ java -jar unittestsgen.jar
The most common options are: Run 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:
On unix like systems:
[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.

I have some test classes and what?

If you have ready to use test classes (classes which, can be compiled with no problems) you can start unit testing. It can be done in many ways, however preferred are:

And again improve your work with ANT

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"
Where, of course, 'some_dir' is directory where 'unittestsgen.jar' file exists.
Since we already defined new task we can use it at any time we need it. Of course it can be used in many ways depends of particular case. I recomend very simple but effective use. Look below to see almost complete very simple 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">
                <pathelement location="${libs}/junit.jar"/>
    <target name="jar" depends="compile">
        <jar jarfile="jar/${jarfile}.jar" manifest="MANIFEST.MF" basedir="${build}">
            <exclude name="** /*TestCase*.*"/>
            <exclude name="TestAll.*"/>
        <unitgen update="true" classpath="${build};${libs}/junit.jar"
                 imput="${src}" output="${test-src}"/>
After excecuting command 'ant jar' you should see similar output:
    [javac] Compiling 36 source files to /home/kobit/projects/unittestsgen/build
      [jar] Building jar: /home/kobit/projects/unittestsgen/jar/unittestsgen.jar
  [unitgen] Finished work: files 21, sources 14, new classes 3, new methods 7

Every 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.
In above sample 'unitteg' task found 21 files in given directory and 14 files with source code which can be tested. So it generated 3 new test classes and added 7 new methods to existing test classes.

More info you can find in UnitTestsGen class documentation.

Need more info?

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

This is part of Web Test Tools project.