Skip to content

A Swift macro library that generates fixtures for value types to simplify unit test writing

License

Notifications You must be signed in to change notification settings

gibachan/swift-fixtures

Repository files navigation

swift-fixtures

CI Swift 5.9+ SPM Compatible License

A Swift package that simplifies unit test writing by providing fixtures for Swift value types through a macro system.

Motivation

When writing unit tests, you often need to create model objects that are not the focus of your test logic. This library helps you generate these objects easily with .fixture, allowing you to focus on what you're actually testing rather than spending time on boilerplate object creation.

The @Fixture macro generates fixture methods for structs and enums, making test data creation simple and consistent.

Installation

Add swift-fixtures to your project using Swift Package Manager:

// Package.swift
dependencies: [
    .package(url: "https://github.com/gibachan/swift-fixtures.git", from: "0.2.0")
],
targets: [
    .target(
        name: "YourTarget",
        dependencies: [
            .product(name: "Fixtures", package: "swift-fixtures")
        ]
    )
]

Features

  • Struct fixtures - Generate .fixture static property and customizable initializers
  • Enum fixtures - Generate fixtures for enum types
  • Builder pattern - Customize properties using closures: Type.fixture { $0.property = value }
  • Nested types - Support for nested structs and enums
  • Standard type support - 15+ pre-built fixtures for common types (String, Int, Date, URL, UUID, Arrays, etc.)
  • Type safety - Recursive fixture generation for custom types conforming to Fixtureable
  • Class support - Classes (reference types) are not supported
  • 🚧 Actor support - Actors are not yet supported

Usage

@Fixture
struct User {
  let id: String
  let name: String
  var age: Int

  @Fixture
  enum Role {
    case guest
    case user(name: String)
    case admin(id: String, permissions: [String])
  }
  var role: Role
}

// Generate fixture with default values
let user: User = .fixture
// User(id: "a", name: "a", age: 1, role: .guest)

// Customize specific properties
let bob = User.fixture {
  $0.name = "Bob"
  $0.age = 30
  $0.role = .admin(id: "admin123", permissions: ["read", "write"])
}
// User(id: "a", name: "Bob", age: 30, role: .admin(id: "admin123", permissions: ["read", "write"]))

External Library Types

The @Fixture macro can only be applied to types you define. For types from external libraries, you can manually conform to Fixtureable:

import ExternalLibrary
import Fixtures

extension ExternalType: Fixtureable {
    static var fixture: Self {
        ExternalType(
            name: .fixture,
            value: .fixture
        )
    }
}

// Now you can use .fixture with external types
let external: ExternalType = .fixture

Development

make build    # Build project
make test     # Run tests
make format   # Format code
make lint     # Lint code
make clean    # Clean build artifacts
make help     # Show all commands

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Swift macro library that generates fixtures for value types to simplify unit test writing

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published