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. By default Diffblue Cover results are filtered if they meet the criteria of 3 stars and above.

  1. Select the Tests tab.

  2. Select tests using the Filter drop-down menu and apply the filters.

  3. Click Create filtered tests archive and a tar file containing your filtered tests will be created.

  4. Click Download filtered tests archive to download the filtered test archive.

  5. 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.
@Test
public void checkTicTacToePositionInput9OutputPositive7() throws IllegalArgumentException {
  // Arrange
  final TicTacToe ticTacToe = new TicTacToe();
  final int[] board = {1, 1, 2, 1, 2, 2, 2, 2, 1};
  // Act and Assert result
  Assert.assertEquals(2, ticTacToe.checkTicTacToePosition(board));
}

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: 'org.mockito', name: 'mockito-all', version: '1.10.19'
         testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.6.5'
         testCompile group: 'org.powermock', name: 'powermock-module-junit4', 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 ""