Unit Test generation tool

13 February 2017


Automatic Unit test generation

There are some tools for generating unit tests based on source files. Some tools come handy with an error revealing test mechanism, so it basically generates error revealing tests on top of your existing tests.

There are a variety of tools for unit test generation, some paid and some free options. In this post I'm going to show how to run Randoop for java.


1. Installing

Download Randoop zip and unzip it to a folder in your filesystem. Set an environment variable RANDOOP_PATH to the path of the unzipped archive.

export RANDOOP_PATH
PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$ANDROID_HOME/tools
export PATH

2. Sample Unit test

This is a sample User class with only 1 property called name:

package sample.test.coverage;

public class User { private String name; public User(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isValid() { return (name != null && !name.equals("") && name.length() <= 10); }
@Override public String toString() { return name.toString(); }

}

3. Generating the Unit test

Now lets trigger the unit test generation:

go to target folder of project and type:

java -ea -classpath classes:${RANDOOP_PATH}/randoop-all-3.0.8.jar randoop.main.Main gentests --testclass=sample.test.coverage.User --timelimit=5

This will generate random tests for 5 seconds (timelimit param). Check for other params and options in documentation. By default, Randoop generates and then outputs two kinds of unit tests: Error-revealing tests are tests that fail when executed, indicating a potential error in one or more classes under test and Regression tests which are tests that pass when executed, and can be used to augment a regression test suite.


4. Resulting tests

The resulting execution created more than 3000 tests! The above example didn't generate any Error revealing tests but lets check some of the resulting regretion tests:

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class RegressionTest1 {

public static boolean debug = false;

@Test public void test001() throws Throwable { if (debug) { System.out.format("%n%s%n","RegressionTest1.test001"); } sample.test.coverage.User user1 = new sample.test.coverage.User("hi!"); user1.setName("hi!"); user1.setName("hi!"); boolean b6 = user1.isValid(); java.lang.String str7 = user1.getName(); java.lang.String str8 = user1.getName(); java.lang.String str9 = user1.getName(); java.lang.String str10 = user1.getName(); boolean b11 = user1.isValid(); boolean b12 = user1.isValid(); user1.setName(""); boolean b15 = user1.isValid(); boolean b16 = user1.isValid();
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b6 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str7 + "' != '" + "hi!"+ "'", str7.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str8 + "' != '" + "hi!"+ "'", str8.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str9 + "' != '" + "hi!"+ "'", str9.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str10 + "' != '" + "hi!"+ "'", str10.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b11 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b12 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b15 == false);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b16 == false); }

@Test public void test002() throws Throwable { if (debug) { System.out.format("%n%s%n","RegressionTest1.test002"); } sample.test.coverage.User user1 = new sample.test.coverage.User("hi!"); java.lang.String str2 = user1.getName(); user1.setName("hi!"); user1.setName("hi!"); java.lang.String str7 = user1.getName(); java.lang.String str8 = user1.getName(); user1.setName("hi!"); boolean b11 = user1.isValid();
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str2 + "' != '" + "hi!"+ "'", str2.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str7 + "' != '" + "hi!"+ "'", str7.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str8 + "' != '" + "hi!"+ "'", str8.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b11 == true); }

@Test public void test003() throws Throwable { if (debug) { System.out.format("%n%s%n","RegressionTest1.test003"); } sample.test.coverage.User user1 = new sample.test.coverage.User(""); java.lang.String str2 = user1.getName(); user1.setName(""); boolean b5 = user1.isValid(); user1.setName("hi!"); boolean b8 = user1.isValid(); java.lang.String str9 = user1.getName(); java.lang.String str10 = user1.getName(); java.lang.String str11 = user1.getName(); user1.setName("");
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str2 + "' != '" + ""+ "'", str2.equals(""));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b5 == false);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b8 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str9 + "' != '" + "hi!"+ "'", str9.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str10 + "' != '" + "hi!"+ "'", str10.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str11 + "' != '" + "hi!"+ "'", str11.equals("hi!")); }

@Test public void test004() throws Throwable { if (debug) { System.out.format("%n%s%n","RegressionTest1.test004"); } sample.test.coverage.User user1 = new sample.test.coverage.User("hi!"); java.lang.String str2 = user1.getName(); user1.setName("hi!"); boolean b5 = user1.isValid(); java.lang.String str6 = user1.getName(); user1.setName("hi!"); boolean b9 = user1.isValid(); boolean b10 = user1.isValid(); java.lang.String str11 = user1.getName(); boolean b12 = user1.isValid(); user1.setName(""); user1.setName("");
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str2 + "' != '" + "hi!"+ "'", str2.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b5 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str6 + "' != '" + "hi!"+ "'", str6.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b9 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b10 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str11 + "' != '" + "hi!"+ "'", str11.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b12 == true); }

@Test public void test005() throws Throwable { if (debug) { System.out.format("%n%s%n","RegressionTest1.test005"); } sample.test.coverage.User user1 = new sample.test.coverage.User("hi!"); java.lang.String str2 = user1.getName(); user1.setName("hi!"); boolean b5 = user1.isValid(); java.lang.String str6 = user1.getName(); user1.setName("hi!"); user1.setName(""); boolean b11 = user1.isValid(); boolean b12 = user1.isValid();
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str2 + "' != '" + "hi!"+ "'", str2.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b5 == true);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue("'" + str6 + "' != '" + "hi!"+ "'", str6.equals("hi!"));
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b11 == false);
// Regression assertion (captures the current behavior of the code) org.junit.Assert.assertTrue(b12 == false); } // ... more tests

}



comments powered by Disqus