Rundown: BDD testing framework

I'd like to introduce Rundown, a BDD Swift testing framework patterned after frameworks such as RSpec and Quick, and compatible with both XCTest and Swift Testing. It uses a result builder to build test elements - such as describe, context, and it - into an executable spec, and the builder also enforces proper ordering. For example, putting a beforeAll at the end instead of the beginning produces a compile-time error.

Here’s a Swift Testing example, using a typed fixture:

import RundownSwiftTesting
import Testing

@Suite(.rundownPaths)
struct CartTests: SwiftTestingSuite {
  @Test
  func total() throws {
    try spec("Cart") {
      context("with two items") {
        // Fixture creates a new Cart for each test
        let cart = Fixture { Cart(prices: [19, 23]) }

        it("updates when an item is removed", using: cart) { cart in
          cart.prices.removeLast()
          #expect(cart.total == 19)
        }
        
        it("totals their prices", using: cart) { cart in
          #expect(cart.total == 42)
        }
      }
    }
  }
}

private final class Cart {
  var prices: [Int]
  var total: Int { prices.reduce(0, +) }

  init(prices: [Int]) {
    self.prices = prices
  }
}

Tests can be async, @MainActor, or normal. In Xcode test results, XCTest non-async tests show each element as a nested activity.

Writing Rundown tests requires a call to the spec() function, and I'm looking into ways to simplify that. One is an experimental @Spec macro patterened after SwiftUI.View. It works, with the drawback that Xcode doesn't discover the tests to list them in the navigator. It does run the tests if you run the whole target, and the results still show in the log.

The .rundownPaths trait seen above is required on non-async Swift Testing suites in order for failure messages to include the path, like "Cart, with two items, totals their prices". This isn't required for async tests because I was able to take advantage of an async-only API to insert the path into the message. I think the Add Issue Capturing proposal may provide a fix for this.

I'd appreciate any feedback on the API and what you think might make this library more useful or easier to adopt. Thanks!

https://github.com/Uncommon/Rundown

1 Like