| Type: | Package |
| Title: | Mutation Testing |
| Version: | 0.1.0 |
| Description: | Measure quality of your tests. 'muttest' introduces small changes (mutations) to your code and runs your tests to check if they catch the changes. If they do, your tests are good. If not, your assertions are not specific enough. 'muttest' gives you percent score of how often your tests catch the changes. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| Depends: | R (≥ 4.1.0) |
| Imports: | checkmate, cli, dplyr, fs, purrr, R6, rlang, testthat, tibble, treesitter, treesitter.r, withr |
| Config/testthat/edition: | 3 |
| URL: | https://jakubsob.github.io/muttest/ |
| Suggests: | box, covr, cucumber (≥ 2.1.0), ggplot2, shiny |
| Config/Needs/website: | rmarkdown |
| NeedsCompilation: | no |
| Packaged: | 2025-05-28 17:46:29 UTC; jakub |
| Author: | Jakub Sobolewski [aut, cre] |
| Maintainer: | Jakub Sobolewski <jakupsob@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2025-05-30 09:40:05 UTC |
CopyStrategy interface
Description
Extend this class to implement a custom copy strategy.
Methods
Public methods
Method execute()
Copy project files according to the strategy
Usage
CopyStrategy$execute(original_dir)
Arguments
original_dirThe original directory to copy from
planThe current test plan
Returns
The path to the temporary directory
Method clone()
The objects of this class are cloneable with this method.
Usage
CopyStrategy$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other CopyStrategy:
PackageCopyStrategy,
default_copy_strategy()
Run tests matching the mutated source file name
Description
This strategy tells if a mutant is caught by a test matching the source file name.
For example, if the source file name is foo.R, and there are test files named test-foo.R or test-bar.R,
only test-foo.R will be run.
This strategy should give faster results than ?FullTestStrategy, especially for big codebases,
but the score might be less accurate.
Super class
muttest::TestStrategy -> FileTestStrategy
Methods
Public methods
Method new()
Initialize the FileTestStrategy
Usage
FileTestStrategy$new(
load_helpers = TRUE,
load_package = c("source", "none", "installed")
)Arguments
load_helpersWhether to load test helpers
load_packageThe package loading strategy
Method execute()
Execute the test strategy
Usage
FileTestStrategy$execute(path, plan, reporter)
Arguments
pathThe path to the test directory
planThe current mutation plan. See
plan().reporterThe reporter to use for test results
Returns
The test results
Method clone()
The objects of this class are cloneable with this method.
Usage
FileTestStrategy$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other TestStrategy:
FullTestStrategy,
TestStrategy,
default_test_strategy()
Run all tests for a mutant
Description
This test strategy tells if a mutant is caught by any test.
To get faster results, especially for big codebases, use ?FileTestStrategy instead.
Super class
muttest::TestStrategy -> FullTestStrategy
Methods
Public methods
Method new()
Initialize
Usage
FullTestStrategy$new(
load_helpers = TRUE,
load_package = c("source", "none", "installed")
)Arguments
load_helpersWhether to load test helpers
load_packageThe package loading strategy
Method execute()
Execute the test strategy
Usage
FullTestStrategy$execute(path, plan, reporter)
Arguments
pathThe path to the test directory
planThe current mutation plan. See
plan().reporterThe reporter to use for test results
Returns
The test results
Method clone()
The objects of this class are cloneable with this method.
Usage
FullTestStrategy$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other TestStrategy:
FileTestStrategy,
TestStrategy,
default_test_strategy()
Reporter for Mutation Testing
Description
The job of a mutation reporter is to aggregate and display the results of mutation tests. It tracks each mutation attempt, reporting on whether the tests killed the mutation or the mutation survived.
Public fields
test_reporterReporter to use for the testthat::test_dir function
outOutput destination for reporter messages
widthWidth of the console in characters
unicodeWhether Unicode output is supported
crayonWhether colored output is supported
rstudioWhether running in RStudio
hyperlinksWhether terminal hyperlinks are supported
current_filePath of the file currently being mutated
current_mutatorMutator currently being applied
planComplete mutation plan for the test run
resultsList of mutation test results, indexed by file path
current_scoreCurrent score of the mutation tests
Methods
Public methods
Method new()
Initialize a new reporter
Usage
MutationReporter$new(test_reporter = "silent", file = stdout())
Arguments
test_reporterReporter to use for the testthat::test_dir function
fileOutput destination (default: stdout)
Method start_reporter()
Start reporter
Usage
MutationReporter$start_reporter(plan = NULL)
Arguments
planThe complete mutation plan
temp_dirPath to the temporary directory for testing
Method start_file()
Start testing a file
Usage
MutationReporter$start_file(filename)
Arguments
filenamePath to the file being mutated
Method start_mutator()
Start testing with a specific mutator
Usage
MutationReporter$start_mutator(mutator)
Arguments
mutatorThe mutator being applied
Method add_result()
Add a mutation test result
Usage
MutationReporter$add_result(plan, killed, survived, errors)
Arguments
planCurrent testing plan. See
plan().killedWhether the mutation was killed by tests
survivedNumber of survived mutations
errorsNumber of errors encountered
Method end_mutator()
End testing with current mutator
Usage
MutationReporter$end_mutator()
Method end_file()
End testing current file
Usage
MutationReporter$end_file()
Method end_reporter()
End reporter and show summary
Usage
MutationReporter$end_reporter()
Method get_score()
Get the current score
Usage
MutationReporter$get_score()
Method cat_tight()
Print a message to the output
Usage
MutationReporter$cat_tight(...)
Arguments
...Message to print
Method cat_line()
Print a message to the output
Usage
MutationReporter$cat_line(...)
Arguments
...Message to print
Method rule()
Print a message to the output with a rule
Usage
MutationReporter$rule(...)
Arguments
...Message to print
Method clone()
The objects of this class are cloneable with this method.
Usage
MutationReporter$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other MutationReporter:
ProgressMutationReporter,
default_reporter()
Package copy strategy
Description
It copies all files and directories from the original directory to a temporary directory.
Super class
muttest::CopyStrategy -> PackageCopyStrategy
Methods
Public methods
Method execute()
Copy project files, excluding hidden and temp directories
Usage
PackageCopyStrategy$execute(original_dir, plan)
Arguments
original_dirThe original directory to copy from
planThe current test plan
Returns
The path to the temporary directory
Method clone()
The objects of this class are cloneable with this method.
Usage
PackageCopyStrategy$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other CopyStrategy:
CopyStrategy,
default_copy_strategy()
Progress Reporter for Mutation Testing
Description
A reporter that displays a progress indicator for mutation tests. It provides real-time feedback on which mutants are being tested and whether they were killed by tests.
Super class
muttest::MutationReporter -> ProgressMutationReporter
Public fields
start_timeTime when testing started (for duration calculation)
min_timeMinimum test duration to display timing information
col_configList of column configuration for report formatting
Methods
Public methods
Inherited methods
Method format_column()
Format a column with specified padding and width
Usage
ProgressMutationReporter$format_column(text, col_name, colorize = NULL)
Arguments
textText to format
col_nameColumn name to use configuration from
colorizeOptional function to color the text
Method fmt_h()
Format the header of the report
Usage
ProgressMutationReporter$fmt_h()
Method fmt_r()
Format a row of the report
Usage
ProgressMutationReporter$fmt_r(status, k, s, e, t, score, mutator, file)
Arguments
statusStatus symbol (e.g., tick or cross)
kNumber of killed mutations
sNumber of survived mutations
eNumber of errors
tTotal number of mutations
scoreScore percentage
mutatorThe mutator used
fileThe file being tested
Returns
Formatted row string
Method new()
Initialize a new progress reporter
Usage
ProgressMutationReporter$new( test_reporter = "silent", min_time = 1, file = stdout() )
Arguments
test_reporterReporter to use for testthat::test_dir
min_timeMinimum time to show elapsed time (default: 1s)
fileOutput destination (default: stdout)
Method start_reporter()
Start reporter
Usage
ProgressMutationReporter$start_reporter(plan = NULL)
Arguments
planThe complete mutation plan
Method add_result()
Add a mutation test result
Usage
ProgressMutationReporter$add_result(plan, killed, survived, errors)
Arguments
planCurrent testing plan. See
plan().killedWhether the mutation was killed by tests
survivedNumber of survived mutations
errorsNumber of errors encountered
Method update()
Update status spinner (for long-running operations)
Usage
ProgressMutationReporter$update(force = FALSE)
Arguments
forceForce update even if interval hasn't elapsed
Method end_file()
End testing current file
Usage
ProgressMutationReporter$end_file()
Method cr()
Carriage return if dynamic, newline otherwise
Usage
ProgressMutationReporter$cr()
Method end_reporter()
End reporter with detailed summary
Usage
ProgressMutationReporter$end_reporter()
Method clone()
The objects of this class are cloneable with this method.
Usage
ProgressMutationReporter$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other MutationReporter:
MutationReporter,
default_reporter()
TestStrategy interface
Description
Extend this class to implement a custom test strategy.
Methods
Public methods
Method execute()
Execute the test strategy
Usage
TestStrategy$execute(path, plan, reporter)
Arguments
pathThe path to the test directory
planThe current mutation plan. See
plan().reporterThe reporter to use for test results
Returns
The test result
Method clone()
The objects of this class are cloneable with this method.
Usage
TestStrategy$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
See Also
Other TestStrategy:
FileTestStrategy,
FullTestStrategy,
default_test_strategy()
Create a default project copy strategy
Description
Create a default project copy strategy
Usage
default_copy_strategy(...)
Arguments
... |
Arguments passed to the |
Value
A ?CopyStrategy object
See Also
Other CopyStrategy:
CopyStrategy,
PackageCopyStrategy
Create a default reporter
Description
Create a default reporter
Usage
default_reporter(...)
Arguments
... |
Arguments passed to the |
See Also
Other MutationReporter:
MutationReporter,
ProgressMutationReporter
Create a default run strategy
Description
Create a default run strategy
Usage
default_test_strategy(...)
Arguments
... |
Arguments passed to the |
Value
A ?TestStrategy object
See Also
Other TestStrategy:
FileTestStrategy,
FullTestStrategy,
TestStrategy
Run a mutation test
Description
Run a mutation test
Usage
muttest(
plan,
path = "tests/testthat",
reporter = default_reporter(),
test_strategy = default_test_strategy(),
copy_strategy = default_copy_strategy()
)
Arguments
plan |
A data frame with the test plan. See |
path |
Path to the test directory. |
reporter |
Reporter to use for mutation testing results. See |
test_strategy |
Strategy for running tests. See |
copy_strategy |
Strategy for copying the project. See |
Value
A numeric value representing the mutation score.
Mutate an operator
Description
It changes a binary operator to another one.
Usage
operator(from, to)
Arguments
from |
The operator to be replaced. |
to |
The operator to replace with. |
Examples
operator("==", "!=")
operator(">", "<")
operator("<", ">")
operator("+", "-")
Create a plan for mutation testing
Description
Each mutant requires rerunning the tests. For large project it might be not feasible to test all mutants in one go. This function allows you to create a plan for selected source files and mutators.
Usage
plan(mutators, source_files = fs::dir_ls("R", regexp = ".[rR]$"))
Arguments
mutators |
A list of mutators to use. See |
source_files |
A vector of file paths to the source files. |
Details
The plan is in a data frame format, where each row represents a mutant.
You can subset the plan before passing it to the muttest() function.
Value
A data frame with the test plan. The data frame has the following columns:
-
filename: The name of the source file. -
original_code: The original code of the source file. -
mutated_code: The mutated code of the source file. -
mutator: The mutator that was applied.