pitest

How to tell PIT to not mutate some specific portions of code?


It happens that there are sometimes lines of code or methods that can't produce mutants that are going to be killed by any relevant test. (For instance I may be using a null pattern object, and some of the implemented methods are not relevant in prod, so any implementation (even throwing) would be correct).

It would be nice to be able to tell pit to avoid them (so that the mutation coverage is more relevant), but I couldn't find a way to do it in the documentation.

Is there a way to do it?


Solution

  • PIT currently has five mechanisms by which code can be filtered out.

    1. By class, using the excludedClasses parameter
    2. By method, using excludedMethods
    3. Using a custom mutation filter
    4. By adding an annotation named Generated, CoverageIgnore, or DoNotMutate with at least class level retention. (N.B javax.annotation.Generated has only source retention and will not work)
    5. The arcmutate base extensions allow mutation to be filtered using code comments or external text files

    For your use case it sounds like options 1, 4 or 5 would fit.

    Option 2 only allows for a method to be filtered in all classes (this is most commonly used to prevent mutations in toString or hashcode methods).

    Option 3 is a little involved, but would allow you to (for example) to filter out methods with a certain annotation.


    An aside.

    I don't I follow your example of the null object pattern.

    A null object needs to implement all methods of an interface and it is expected that they will be called. If they were to throw this would break the pattern.

    In the most common version of the pattern the methods would be empty, so there would be nothing to mutate apart from return values.

    This behaviour would be worth describing with tests. If your null object fails to return whatever values are considered neutral this would cause a problem.