Creating partial tests

About partial tests

Normally, dcover create will create complete tests for a method. A complete test consists of Arrange, Act, and Assert sections and passes when executed. However, sometimes Diffblue Cover is not (yet) able to write complete tests and will give the reason for not creating complete tests in the output summary. In these cases, Diffblue Cover can create partial tests by using the keep-partial-tests argument. Partial tests may be incomplete in various aspects:

  • The test does not have assertions.

  • The test does not always pass when executed.

  • The Arrange or Act sections produce an error when executed.

  • The test may execute code that is potentially harmful to your system, leaks resources, or times out.

Creating partial tests is useful to developers for two reasons:

  • Firstly, it will save developers time. Instead of writing tests from scratch, they'll be able to use the partial tests as a starting point.

  • Secondly, it may help developers understand why Diffblue Cover was not able to complete the test automatically, especially in cases where the reason for not producing a test is lack of testability.

For example, for method increment in the following class:

public class MyClass {
    private int x;
    public void increment() {
	  ++x;
    }
}

Using dcover create --keep-partial-tests, Cover will create a partial test:

  @Test
  public void testIncrement() {
    // TODO: This test is incomplete.
    //   Reason: R002 Missing observers.
    //   Diffblue Cover was unable to create an assertion.
    //   Add getters for the following fields or make them package-private:
    //     MyClass.x

    // Arrange
    MyClass m = new MyClass();

    // Act
   m.increment();
  }

The problem is clearly that Diffblue Cover has no opportunity to assert on the side effect of the increment method on field x, which is marked private. The developer could add a getter to MyClass:

public class MyClass {
    private int x;
    public void increment() {
	  ++x;
    }
	public int getX() {
	  return x;
	}
}

Running dcover create again (with or without keep-partial-tests) would now create a complete test.

Retain partial tests

Partial tests are usually discarded by Cover CLI during validation, but can be retained by using the keep-partial-tests argument.

Example: dcover create --keep-partial-tests

Note that this argument will retain all tests created by Diffblue Cover, including partial tests, tests without assertions, non-compiling tests, non-deterministic tests, tests that result in exceptions, and tests that violate the security policy (sandbox).

Last updated