Swift edition
Getting started
This runs mutastic on samples/pricing, a small Swift package in mutastic’s repository whose tests
pass and reach every line, and still miss something. Then it closes the gap and proves it.
1. Install it
Section titled “1. Install it”The Swift edition isn’t released yet, so it’s built from its source. You need Swift 6.0 or later (on macOS, Xcode 16 or later), rustup and just:
git clone https://github.com/mutastic/mutastic-swift.gitcd mutastic-swiftjust installThat builds it and puts it in ~/.mutastic/swift-1.5.0/. From 1.5.0, mutastic, the command the
other editions install, runs it in any Swift package or Xcode project, and fetches it the first
time. Until then, run it by its own name: these pages write mutastic, as the commands it offers
do, and ~/.mutastic/swift-1.5.0/mutastic-swift takes the same words. Installing
mutastic has the details.
2. Run it
Section titled “2. Run it”From the repository’s root:
mutastic run samples/pricingmutastic copies the package into its .build/mutastic, writes every mutant into the copy behind a
switch, and builds it once. It runs each test once to see what it reaches, then runs each mutant
against the tests that reach it. One mutant gets past them:
Missed ────────────────────────────────────────────────────────────
❯ Sources/Pricing/Pricing.swift 1 missed in 1 function
✗ MISSED no test checks discount() at total == 100 ┌─ Sources/Pricing/Pricing.swift:12 │ 11 │ public func discount(total: Int) -> Int { 12 │ if total >= 100 { │ ^^ >= changed to > 13 │ return total / 10 │ = mutant - if total >= 100 { + if total > 100 { = operator mutastic/boundary · https://docs.mutastic.io/swift/operators/conditions/#boundary = note: every test calls discount() away from the boundary inputs seen: 50, 250 (none is 100) covered by PricingTests.bigOrdersGetTenPercentOff(), PricingTests.smallOrdersPayFullPrice() (all 2 covering tests still pass) = help: test discount() with total = 100 and assert the result
// in Tests/PricingTests/PricingTests.swift @Test func discountAtTheTotalEdge() { #expect(Pricing().discount(total: 100) == 10) // today 10 · with this mutant 0 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m86f9a31d samples/pricing passes reproduce mutastic run --only=m86f9a31d samples/pricing or ignore mutastic ignore m86f9a31d --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Files 0 clean | 1 with misses Mutants ✓ 10 caught · ✗ 1 missed (11) Worst at Pricing.discount() 1 ✗ Build one build of every mutant, 1 round (build) Test runs 14 (33 without coverage selection) Start at 22:28:54 Duration 7.3s (setup 35ms, build 7.1s, coverage 22ms, mutants 115ms, checking 22ms, reports 23ms) Tests 3 on swift 6.3.3 · 4 workers Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html Score 90.9% PASS 1 missed, above the threshold of 80.On your own package, run mutastic run inside it, or mutastic run path/to/package.
3. Read the card
Section titled “3. Read the card”Each line with a missed mutant gets a card.
- The gap, first: no test checks
discount()attotal == 100, so nothing tells>=from>. - The change that got through, under the code, and again as a diff.
- The operator that made it, with a link to its section in Operators.
- The evidence: the tests that reach the line, which all still pass with the change in.
- What to do: test
discount()withtotal = 100, and assert the result. The test to add is written out,discountAtTheTotalEdge(), with what the code gives today: 10, where the mutant gives 0. - The commands:
done whenproves the fix, andreproduceruns just this mutant again. Each names this package, so it works as it’s copied.m86f9a31dis the mutant’s id; it stays the same while the code around it moves.
The summary says how the rest went. The score is high enough to pass, but the run still shows the one it missed: a passing score isn’t a reason to leave a gap you can close.
4. Write the test
Section titled “4. Write the test”discount(total: 100) gives 10 today. Is that right? The doc comment says 10% off orders of 100
or more, so yes. Add a test that says so to samples/pricing/Tests/PricingTests/PricingTests.swift:
@Test func discountAtTheTotalEdge() { #expect(Pricing().discount(total: 100) == 10)}With > in place of >=, discount(total: 100) is 0, and this test fails. When today’s value
isn’t the one the code should give, keep the test and write the right value: then it’s the code
that needs fixing.
5. Prove it
Section titled “5. Prove it”Run the command the card offered as done when:
mutastic verify m86f9a31d samples/pricingmutastic: m86f9a31d is caught by PricingTests.discountAtTheTotalEdge()The mutant is caught: a test now fails when >= becomes >. verify exits with 0 when every
mutant it names is caught, and 1 when one isn’t, so a script or a coding agent can use it as the
check that a gap is closed.
6. Run it again
Section titled “6. Run it again”mutastic run samples/pricing Files 1 clean Mutants ✓ 11 caught (11) Build one build of every mutant, 1 round (build) Test runs 19 (44 without coverage selection) Start at 22:29:05 Duration 1.3s (setup 34ms, build 988ms, coverage 23ms, mutants 135ms, reports 24ms) Tests 4 on swift 6.3.3 · 4 workers Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html Score 100.0% PASS Every mutant was caught. ▲ 9.1 vs last run (code changed)Every mutant is caught.
- Running on your project: SwiftPM and Xcode, Swift Testing and XCTest.
- Operators: the kinds of change mutastic makes.
- Commands: every option, for CI and coding agents.
- How the tests are run: what happens to your package, and what doesn’t.