wiki:ComTestInEnglish
Last modified 3 years ago Last modified on 2014-11-23 12:47:08

ComTest

ComTest = Comments for Testing

ComTest is made under MIT-licence.

SVN source: https://svn.cc.jyu.fi/srv/svn/comtest/proto

Example:

1. Main idea

  • The main idea in ComTest is to simplify unit testing. During Programming 2 (CS2) course it was observed that the load to make JUnit test was quite high and students refused to make JUnit-tests.
  • Design tool
  • Documentation tool (by giving examples how to use the unit)

1.1 Simple tests

So we decided to make a bit simpler way to do unit testing. The solution was ComTest. Idea is that the test are written in the same place where the code is written, so for example to test the function smallestDivisor on write the comments

public class SmallestDivisor {
    
    /**
     * Function finds the smallest divisor for given number
     * @param n number to study
     * @return smallest divisor for n.  1 if n is a prime
     * @example
     * <pre name="test">
     *   smallestDivisor(1) === 1;
     *   smallestDivisor(2) === 1;
     *   smallestDivisor(3) === 1;
     *   smallestDivisor(4) === 2;
     *   smallestDivisor(5) === 1;
     *   smallestDivisor(6) === 2;
     * </pre>
     */
    public static int smallestDivisor(int n) {
        for (int i=2; i<=n/2; i++)
            if ( n % i == 0 ) return i;
        return 1;
    }
    
    /**
     * @param args not used
     */
    public static void main(String[] args) {
        int n = 25;
        int divisor = smallestDivisor(n);
        System.out.println(divisor);
    }

}

As you see from previous, ComTest is based on a macro language inside Java comments. Mostly one can write Java in ComTest comments, but for test purpose there is way to shorten the test (in previous example ===). There is also way to make tests as a table and that makes long tests very easy to write and read.

1.2 Tests in table format

1.3 Few special cases

  object1 === object2;                     /// are two objects equal using equals method
  object1 == object2 === true;             /// do both values reference to same object
  object1.compareTo(object2) < 0 === true; /// is object1 less than object2
  angle ~~~ 3.141592;                      /// is real number almost pi
  st  ~= "cat.*moon";                      /// is string same in sense of reg.ex
  list.get(3) === "three"; #THROWS IndexOutOfBoundsException  // should throw expetion 
  a[2] === 2; #THROWS (IndexOutOfBoundsException ex) { ex.getMessage() =R= ".*2.*"; } // can be tested by regexp

1.4 Testing arrays

Example of testing what is inside of the array:

	/**
	 * Transpose the table
	 * @param t table to transpose
	 * @example
	 * <pre name="test">
	 * #import java.util.Arrays;
	 *    int[] t = {2, 32, 76, 62, 31, 86};
	 *    transpose(t); 
	 *    Arrays.toString(t) === "[86, 31, 62, 76, 32, 2]";
	 * </pre>
	 */

Example of creating an array inside the test:

	/**
	 * Count sum of the items in an array
	 * @param t arrays to sum
	 * @return sum of items
	 * @example
	 * <pre name="test">
	 *   int[] t = {1,2,3,4,5,6};
	 *   sum(t) === 21;
	 *   sum(new int[]{1,1,1,1,1,1}) === 6;
	 * </pre>
	 */

2. How to use

2.1 Command line

  1. Load comtest.jar -file
  2. Write macro language comments
  3. Write from command line (with proper path to ComTest and file name)
    java -jar E:\kurssit\ohj2\comtest\comtest.jar FileName.java  
    
  4. Run JUnit for generated FileNameTest.java

2.2 Eclipse plugin

  • Eclipse 3.3 and later: Save comtestplug_1.0.6.jar to eclipse/plugins directory (includes command line .jar).
  • Run: right mouse button and ComTest (you can also make s shortcut for this)
  • The JUnit 4 must be part of project

3. Other benefits

3.1 Code examples

By ComTest one can write code examples same time as test

3.2 Desing help

ComTest serves also a desing tool for classes and functions

3.3 Measured results

By ComTest students wrote more tests to their program than control group

4. Test coverage

Many test coverage packet is directly usable under ComTest. Below is few examples:

4.1 Clover

Clover feels quite good test coverage tool.

  • install Clover and right click over project and enable Clover. Then just run ComTest tests to get the coverage.
  • example coverage from Ali-library

4.2 eclEmma

eclEmma is ok for line coverage, but does not show f.ex if sentences that are not executed

  • install and run ComTest and then press Shift-Ctrl-F11

4.3 Cobertura

Cobertura

Contact: vesal@…