- Guide
- Playground
- GitHub
-
Ecosystem
Help
Resource Lists
Documentation
- Introduction
- Installation
- Usage
- Command Line Options
- Mutators
- Custom Mutators
- Profiles
- Using with CI
- Mutation Badge, cloud HTML report
- How-to Guides
- Caveats
- Debugging Issues
- Infection Playground
- GitHub Sponsors ♥️
- Supported Test Frameworks
Miscellaneous
- Compare with competitors
Posts
- What's new in Infection 0.26.0
- What's new in Infection 0.25.0
- What's new in Infection 0.24.0
- What's new in Infection 0.23.0
- What's new in Infection 0.21.0
Caveats
Mutations must occur when CodeCoverage has already started
It may happen that a mutation that fails a test isn’t reported as covered.
This can occur when the code-coverage profiler hasn’t started before the mutations occurs.
Take for example:
class SourceClassTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider instancesProvider
*/
public function testAlwaysTrueFromFactoryMethod(SourceClass $class): void
{
self::assertTrue($class->getValue());
}
public function instancesProvider(): array
{
return [
[SourceClass::factoryMethod()],
];
}
}
class SourceClass
{
private bool $value;
private function __construct(bool $value)
{
$this->value = $value;
}
public static function factoryMethod(): self
{
return new self(true);
}
public function getValue(): bool
{
return $this->value;
}
}
If the TrueValue Boolean Substitution mutator is active, you
would expect the mutation to be correctly covered. But in the provided example the mutation
occurs in the @dataProvider which runs before the code-coverage driver gets started.
Be sure to check your mutated code runs inside the test in this case, so a workaround can be the following:
class SourceClassTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider instanceFactoriesProvider
*/
public function testAlwaysTrueFromFactoryMethod(callable $classFactory): void
{
$class = $classFactory();
self::assertTrue($class->getValue());
}
public function instanceFactoriesProvider(): array
{
return [
[static function() {
return SourceClass::factoryMethod();
}],
];
}
}
Caught a mistake or want to contribute to the documentation?
Edit this page on Github!