Skip to content
Understudy Get started

All examples

UnderstudySamplesSamples

A free product in one line

@Sample writes sample(...) for a struct, so a test passes only the values it is about and every other property gets its default.

Run it

swift test --filter sampleOverridesOnlyTheGivenProperties
The whole file on GitHub
SampleTests.swift
@Test("A sample changes only the properties the test passes")
func sampleOverridesOnlyTheGivenProperties() {
    let tags = ["home"]
    let free = Product.sample(price: 0, tags: tags)

    let expected = Product(
        id: Product.sample().id,
        name: "",
        price: 0,
        tags: tags,
        stock: 5,
        currency: "EGP",
        seller: Seller.sample()
    )
    #expect(free == expected)
}