Mutation testing¶
mutate_module runs a module's tests against every
mutant of its rules, lexical entries, and constants and reports the mutants no test rejects.
mutation
¶
Mutation testing: perturb a module and see whether its tests notice.
A test suite proves what it rejects. mutate_module makes small changes to
a module's code, one at a time, each a mutant, and runs the module's
tests against every mutant:
drop-rule: remove one inference rule (a deduction'srule, or a top-levelrule);drop-entry: remove one lexical entry;swap-category: give one lexical entry the category of the next entry in its lexicon with a different one;constant: replace one literal in a computation, handler, or program by another value (an integernbyn + 1andn - 1, a realcby0and2c, or1whencis zero; a truth value by its negation).
A mutant is killed when some test fails or errs against it, survives when every test still passes, and is invalid when the compiler rejects it, so no test needs to. A surviving mutant is a change to the code that no test can tell apart from the original: either the suite misses a behavior, or the mutant is equivalent to the original.
The tests themselves are never mutated, and the module must pass its tests before any mutant runs. A module is mutated as it runs, its module graph interpreted into one flat program; the mutants change the root module's code, or the code of the imported modules named instead, so a library's rules are mutated under the tests of a module that imports it.
OPERATORS
module-attribute
¶
OPERATORS: tuple[Operator, ...] = ('drop-rule', 'drop-entry', 'swap-category', 'constant')
Every mutation operator, in the order mutants are made.
Operator
¶
How a mutant changes the code.
MutantStatus
¶
What the tests made of a mutant.
Step
¶
One step into an AST: a field, and an index when the field is a tuple.
Mutant
didactic-model
¶
Mutant(*, index: int, operator: Operator, description: str, line: int = 0, col: int = 0, file: str = '', status: MutantStatus = 'survived', killed_by: str = '', message: str = '')
Bases: Model
One change to a module and what the tests made of it.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
Its position among the module's mutants.
TYPE:
|
operator
|
The kind of change.
TYPE:
|
description
|
The change, in words.
TYPE:
|
line
|
The source line of the changed node, in its own file.
TYPE:
|
col
|
Its source column.
TYPE:
|
file
|
The file of the changed node.
TYPE:
|
status
|
TYPE:
|
killed_by
|
The first test that failed against it, when killed.
TYPE:
|
message
|
Why the test failed, or why the compiler rejected the mutant.
TYPE:
|
to_data
¶
The mutant as JSON data.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str | int]
|
Every field. |
Source code in src/quivers/testing/mutation.py
MutationReport
didactic-model
¶
MutationReport(*, path: str, baseline: ModuleReport, mutants: tuple[Mutant, ...] = (), error: str = '')
Bases: Model
A module's mutants and how its tests fared against them.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The module.
TYPE:
|
baseline
|
The tests' report on the unmutated module.
TYPE:
|
mutants
|
Every mutant, in the order made.
TYPE:
|
error
|
Why no mutant ran: the module failed to load, has no tests, or its tests do not pass as written; empty otherwise.
TYPE:
|
survivors
property
¶
survivors: tuple[Mutant, ...]
The mutants no test rejected.
| RETURNS | DESCRIPTION |
|---|---|
tuple[Mutant, ...]
|
The survivors. |
ok
property
¶
Whether every valid mutant was killed.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
The verdict; |
to_data
¶
The report as JSON data.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
The counts by status, the survivors, every mutant, and the baseline. |
Source code in src/quivers/testing/mutation.py
mutants_of
¶
mutants_of(module: Module, targets: Callable[[Statement], bool] | None = None) -> Iterator[tuple[Operator, str, int, int, Module]]
Every mutant of a module's code, in source order by operator.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The parsed module; its tests are kept unchanged in every mutant.
TYPE:
|
targets
|
Which top-level statements are mutated; all when
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[tuple[Operator, str, int, int, Module]]
|
The operator, a description, the line and column, and the mutated module. |
Source code in src/quivers/testing/mutation.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
mutate_module
¶
mutate_module(path: Path, operators: tuple[Operator, ...] = OPERATORS, modules: tuple[str, ...] = ()) -> MutationReport
Run a module's tests against every mutant of its code.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The module file.
TYPE:
|
operators
|
The operators whose mutants run; every operator by default. |
modules
|
The module paths whose code is mutated, among those the module imports; the module's own code when empty.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MutationReport
|
Every mutant's status, and the survivors. |
Source code in src/quivers/testing/mutation.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |