2

What does removed call to "com.some.Filename::someMethodName" --> SURVIVED mean in pitest. Does it mean that if that method call is removed, the code will still work properly?

Brooklyn99
  • 987
  • 13
  • 24
Shruti Joshi
  • 301
  • 1
  • 4
  • 16

3 Answers3

3

When pitest says the mutantion has survived it means it changed the codebase, and not a single test detected the code has changed. So you are not being very demanding on your test suite.

Ideally each mutation created should be killed by at least 1 unit test.

some more information regarding mutation test that may help you: https://pedrorijo.com/blog/intro-mutation/. (disclaimer, I'm the tutorial author)

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
  • The enum call that I was trying to cover in mutation wasn't covered by the test itself. For future reference one can try covering assert for all enum values – Shruti Joshi Jun 17 '20 at 17:28
2

Pi-Mutation wants:-

  1. Every line of code got executed by unit test cases
  2. And data being modified by the source code must asserted/validated.

Just master's these above two point's to be master of pi-mutation API.

Suppose you have following source code for which pi-mutation must be passed.

public Optional<CodeRemovedModel> method1(List<CodeRemovedModel> list) {
    if(list.isEmpty()) {
        return Optional.empty();
    }
    return doSomething(list);
}

private Optional<CodeRemovedModel> doSomething(List<CodeRemovedModel> list) {
    // iterating over list item and modifying two fields.
    // as per mutation this forEach loop must be executed
    // And, the modified fields must be asserted - if not you will get "removed call .... -> SURVIVED" error
    list.forEach(s -> {
        s.setFirstName("RAHUL");
        s.setLastName("VSK");
    });
    return Optional.of(list.get(0));
}

In following test case I'm ignoring assertion of one field, hence the error will shows up.

@Test
public void testMethod1_NON_EMPTY_LIST() {
    List<CodeRemovedModel> l = new ArrayList<>();
    l.add(new CodeRemovedModel());
    Optional<CodeRemovedModel> actual = this.codeRemovedMutation.method1(l);
    assertEquals("RAHUL", actual.get().getFirstName());
    //assertEquals("VSK", actual.get().getLastName());
}

enter image description here

vsk.rahul
  • 433
  • 1
  • 6
  • 11
0

In Simple mutation testing terms:

Faults (or mutations) are automatically seeded into your code, then your tests are run. If your tests fail then the mutation is killed, if your tests pass then the mutation lived/survived.

In most cases, developers tend to write Unit Test cases targeting the Code coverage only which may not test each statement.So using a mutation testing tool like PIT is very helpful because it actually detects any faults in the executed code. But in most cases, to kill the mutants introduced by PIT, you have to write assert statements. Mutation testing improves the testing standard because it enforces to write test cases which tests each statement of the code.

Does it mean that if that method call is removed the code will still work properly?

It does not mean that when the method call is removed, the actual code will work but it means even the method call is removed, at least one of the test cases has not detected the change and the test case passed and mutant survived.

Brooklyn99
  • 987
  • 13
  • 24