Executing tests written by Diffblue Cover

Introduction

After Diffblue Cover has written tests for your project you can integrate them back into your own test suite. You can choose to download all the tests or select tests individually. Before you can run any tests, you'll need to add the dependencies in your build system.

The dependencies are:

  • Powermock version 1.6.5
  • Mockito 1.10.19
  • Diffblue deeptestutils 1.9.0 (or newer)
  • JUnit 4.7 (or newer)

Download or copy the tests

There are three ways you can receive the tests written by Diffblue Cover:

a. Download all the tests (with all the required imports and class-level annotations).

b. Download one test (with its imports and class-level annotations).

c. Copy the body of one test (with no imports or class-level annotations).

We describe these three options next.

a. Download all the tests

This method makes it easy to see the overall increase in test coverage that Diffblue has produced.

  1. Select the Tests tab.

  2. Click Create tests archive and wait for us to create a tar file containing your tests.

  3. Click Download tests archive. Diffblue Cover downloads a tar file.

  4. Extract the archive into a folder for your project. In the example of java-demo you would merge the contents of javademo/test from the archive into the src/test folder of the project.

b. Download one test

This is useful when you want to integrate one test quickly.

  1. Select the Tests tab.

  2. Choose a test and click View Test.

  3. Click Download java file.

  4. Extract the test.

For example, copy the TicTacToeTest.java test from the java-demo project to src/test/java/com/diffblue/javademo/TicTacToeTest.java.

c. Copy a test

This is great for situations where you want to integrate a test case into an existing test file for the given .java file.

  1. Select the Tests tab.

  2. Choose a test and click View Test.

  3. Click Copy test.

The test is now on your clipboard.

In this example we will take a test from the java-demo project:

/*
 * Test written by Diffblue Cover.
 * This test covers `int checkTicTacToePosition(int [])' block 1 (line 20)
 * This test covers `int checkTicTacToePosition(int [])' block 6 (line 25)
 * This test covers `int checkTicTacToePosition(int [])' block 7 (line 26)
 * This test covers `int checkTicTacToePosition(int [])' block 8 (line 26)
 * This test covers `int checkTicTacToePosition(int [])' block 9 (line 37)
 * This test covers `int checkTicTacToePosition(int [])' block 10 (line 27)
 * This test covers `int checkTicTacToePosition(int [])' block 11 (line 28)
 * This test covers `int checkTicTacToePosition(int [])' block 12 (line 28)
 * This test covers `int checkTicTacToePosition(int [])' block 13 (line 29)
 * This test covers `int checkTicTacToePosition(int [])' block 14 (line 30)
 * This test covers `int checkTicTacToePosition(int [])' block 15 (line 30)
 * This test covers `int checkTicTacToePosition(int [])' block 16 (line 31)
 * This test covers `int checkTicTacToePosition(int [])' block 20 (line 26)
 * This test covers `int checkTicTacToePosition(int [])' block 21 (line 26)
 * This test covers `int checkTicTacToePosition(int [])' block 22 (line 37)
 * This test covers `int checkTicTacToePosition(int [])' block 23 (line 37)
 * This test covers `int checkTicTacToePosition(int [])' block 27 (line 44)
 * This test covers `int checkTicTacToePosition(int [])' block 28 (line 44)
 * This test covers `int checkTicTacToePosition(int [])' block 30 (line 45)
 * This test covers `int checkTicTacToePosition(int [])' block 31 (line 45)
 * This test covers `int checkTicTacToePosition(int [])' block 36 (line 53)
 * This test covers `int checkTicTacToePosition(int [])' block 37 (line 53)
 * This test covers `int checkTicTacToePosition(int [])' block 38 (line 54)
 * This test covers `int checkTicTacToePosition(int [])' block 39 (line 55)
 * This test covers `int checkTicTacToePosition(int [])' block 42 (line 44)
 * This test covers `int checkTicTacToePosition(int [])' block 43 (line 44)
 */
@org.junit.Test
public void com_diffblue_javademo_TicTacToe_checkTicTacToePosition_004_6c06ae82d9098b40() throws Throwable {
  int retval;
  {
    /* Arrange */
    com.diffblue.javademo.TicTacToe param_3 = (com.diffblue.javademo.TicTacToe) Reflector.getInstance("com.diffblue.javademo.TicTacToe");
    int[] board = { 2, 2, 1, 0, 2, 2, 1, 1, 1 };
    /* Act */
    retval = param_3.checkTicTacToePosition(board);
  }
  {
    /* Assert result */
    Assert.assertEquals(1, retval);
  }
}

You can now take this and paste it into an existing test file. In this example it would be src/test/java/com/diffblue/javademo/TicTacToeTest.java.

Make sure you have the correct imports. If you download the test, you'll find the import statements there.

Add the dependencies

The dependencies are available from the Maven Central Repository.

To use them, you need to follow the guide relevant to your build tool. Here we discuss Maven and Gradle.

If you are unable to access the Maven Central Repository, you can download deeptestutils directly.

Maven projects

To add the Diffblue Cover dependencies to your local Maven repository:

  1. Edit the root pom.xml.

  2. Add this dependency information to the dependencies section.

     <dependency>
       <groupId>org.powermock</groupId>
       <artifactId>powermock-api-mockito</artifactId>
       <version>1.6.5</version>
       <scope>test</scope>
     </dependency>
    
     <dependency>
       <groupId>org.powermock</groupId>
       <artifactId>powermock-module-junit4</artifactId>
       <version>1.6.5</version>
       <scope>test</scope>
     </dependency>
    
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-all</artifactId>
       <version>1.10.19</version>
       <scope>test</scope>
     </dependency>
    
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.8.2</version>
       <scope>test</scope>
     </dependency>
    
     <dependency>
       <groupId>com.diffblue</groupId>
       <artifactId>deeptestutils</artifactId>
       <version>1.9.0</version>
       <scope>test</scope>
     </dependency>
    

Gradle projects

To add the Diffblue Cover dependencies to your build.gradle file:

  1. Add Maven to the repositories section.

     repositories {
         mavenCentral()
     }
    
  2. Add jUnit and Deeptestutils to the dependencies section.

     dependencies {
         testCompile group: 'junit', name: 'junit', version: '4.8.2'
         testCompile group: 'com.diffblue', name: 'deeptestutils', version: '1.9.0'
         testCompile group: 'mockito', name: 'mockito', version: '1.10.19'
         testCompile group: 'powermock', name: 'powermock', version: '1.6.5'
     }
    

Java versions

By default the tests that Diffblue Cover writes work with Java 8. If you are using an older version of Java you must add the version to the CBMC Arguments in the project settings; for example:

java-version: 6

results matching ""

    No results matching ""