Infection Command Examples (with examples)

Infection Command Examples (with examples)

1. Analyze code using the configuration file

infection

Motivation:

Running the infection command without any arguments will analyze the code using the configuration file. This is useful when you have already set up a configuration file and want to quickly analyze your code for mutations.

Explanation:

By running infection without any arguments, the command will search for a infection.json configuration file in your project’s root directory. If the configuration file exists, the command will use it to determine the settings and start the mutation testing process.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, including the number of killed mutants, the number of escaped mutants, and the Mutation Score Indicator (MSI).

2. Use a specific number of threads

infection --threads 4

Motivation:

Running mutation testing can be time-consuming, especially for large codebases. By using multiple threads, you can parallelize the mutation testing process and speed up the overall execution time.

Explanation:

The --threads option allows you to specify the number of threads that should be used for mutation testing. Infection will distribute the mutated files across the specified number of threads, which will run in parallel to speed up the mutation testing process.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
Using 4 threads for mutation testing
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, similar to the previous example, but with the added information that 4 threads were used for mutation testing.

3. Specify a minimum Mutation Score Indicator (MSI)

infection --min-msi 80

Motivation:

Setting a minimum Mutation Score Indicator (MSI) threshold allows you to define a certain level of test coverage that your code should meet. This ensures that your tests are robust and effective in detecting mutations.

Explanation:

The --min-msi option allows you to specify the minimum MSI threshold as a percentage. Infection will compare the calculated MSI with the specified threshold and exit with an error if the threshold is not met.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

Mutation Score Indicator (MSI) did not meet the minimum threshold of 80%

This will output the mutation analysis results, similar to the previous examples, but with the additional information that the calculated MSI did not meet the specified minimum threshold of 80%.

4. Specify a minimum covered code MSI

infection --min-covered-msi 90

Motivation:

Mutation testing is more effective when it targets code that is well-covered by tests. By setting a minimum covered code MSI, you can ensure that a significant portion of your code is tested and affected by mutations.

Explanation:

The --min-covered-msi option allows you to specify the minimum MSI threshold for covered code as a percentage. Infection will calculate the percentage of covered code out of the total codebase and compare it with the specified threshold. If the threshold is not met, Infection will exit with an error.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%
Covered Code MSI: 75%

Covered code MSI did not meet the minimum threshold of 90%

This will output the mutation analysis results, similar to the previous examples, but with the additional information that the calculated covered code MSI did not meet the specified minimum threshold of 90%.

5. Use a specific test framework

infection --test-framework phpspec

Motivation:

Infection supports multiple test frameworks, including PHPUnit and PHPSpec. By specifying the test framework, you can use the one that is already set up in your project and ensure compatibility with your existing test suite.

Explanation:

The --test-framework option allows you to specify the test framework that should be used for running the tests during mutation testing. By default, Infection uses PHPUnit. However, if you are using PHPSpec or another supported test framework, you can specify it with this option.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
Running PHPSpec tests...
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, similar to the previous examples, but with the information that PHPSpec tests were executed instead of PHPUnit.

6. Only mutate covered code

infection --only-covered

Motivation:

Mutating only the lines of code that are covered by tests ensures that you maintain the integrity and correctness of the untested code. This option can be useful when you want to focus on improving the coverage of your tests without affecting untested code.

Explanation:

The --only-covered option allows Infection to only mutate the lines of code that are covered by tests. This ensures that the untested code remains unchanged and prevents introducing new bugs in untested areas of your codebase.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, similar to the previous examples, but with the assurance that only the covered code was mutated.

7. Display the applied mutations

infection --show-mutations

Motivation:

By displaying the mutations that have been applied to your code, you can gain insight into which lines of code have been changed and the specific mutations that have been introduced. This can help you understand the impact of the mutations and improve your tests accordingly.

Explanation:

The --show-mutations option allows Infection to display the mutations that have been applied to your code. This includes the mutated lines of code and the specific mutations that were applied, such as flipping a binary operator, changing a comparison operator, or modifying a function call.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Displaying mutations:
1. src/Calculator.php - Line 10: Replaced "+" with "-" (Original: $result = $a + $b)
2. src/Calculator.php - Line 15: Replaced "<" with ">" (Original: if ($a < $b) { return $a; } else { return $b; })
3. src/Calculator.php - Line 20: Removed "!" (Original: return !$b;)
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, similar to the previous examples, but with the additional information that the applied mutations are being displayed.

8. Specify the log verbosity

infection --log-verbosity all

Motivation:

Controlling the verbosity of the log output can help you get the required level of details during mutation testing. By specifying the log verbosity, you can control the amount of information that is displayed during the mutation testing process.

Explanation:

The --log-verbosity option allows you to specify the log output verbosity level. There are three available options: default, all, and none. By default, the log output is set to default, which displays the essential information. The all verbosity level provides detailed information about each mutated file and test results, while the none level disables all log output.

Example Output:

Infection 1.5.0
Reading configuration file "/path/to/project/infection.json"
Loading source files [6/6] 🎯
...
...
Improvement on "src/Calculator.php" (10/100 diffs) 🔥
Total: 24 mutants were killed, 2 got out alive, 0 were not covered by tests.
Mutation Score Indicator (MSI): 92%

This will output the mutation analysis results, similar to the previous examples, but with the specified verbosity level.

Related Posts

How to use the command xterm (with examples)

How to use the command xterm (with examples)

Xterm is a terminal emulator for the X Window System. It provides a graphical user interface that allows users to interact with the Unix-like operating systems.

Read More
How to use the command 'pihole' (with examples)

How to use the command 'pihole' (with examples)

The ‘pihole’ command is a terminal interface for the Pi-hole ad-blocking DNS server.

Read More
How to use the command 'qm guest exec-status' (with examples)

How to use the command 'qm guest exec-status' (with examples)

The command ‘qm guest exec-status’ is used to print the status of a specific PID started by the guest-agent on QEMU/KVM Virtual Machine Manager.

Read More