Swiftpack.co - filip-sakel/SourceTests as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by filip-sakel.
filip-sakel/SourceTests 0.0.1
A library that makes unit testing fun by bringing it into the source code.
⭐️ 0
🕓 2 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/filip-sakel/SourceTests.git", from: "0.0.1")

SourceTest

Make unit testing fun by including them in all the source-code action. SourceTest allows you to do that by writing auto-discoverable unit tests in your source code without any plugins, post-build scripts or significant build-time penalties.

Note: This package is currently only available on MacOS.

Why?

Make Testing Fun

Testing is usually treated as an afterthought due to the boilerplate required and the context switching between the test files and the source-code files. By having tests and the tested functionality in the same file, you'll have an easier time figuring out what you need to test and doing the actual debugging.

Access Control

With the current approach of @testable imports, sometimes you have to mark fileprivate declarations as internal. This not only pollutes your namespace, but it also reduces your local reasoning over code, since you may forget that a property shouldn't be touched outside of its file and accidentally introduce a bug in your code.

Easy Documentation

Easily accessible tests act like a kind of documentation, that can help maintainers understand what a piece of code is supposed to do and how it should be used. Even if other documentation perfectly describes a given declaration, seeing how it should be used in one place is always useful.

Usage

To use, just write your source code and after you finish, import SourceTests and extend the UnitTests namespace, declaring your unit test:

// Sources/MyApp/CustomAdd.swift

func customAdd(_ a: Int, _ b: Int) -> Int {
    a + b
}

extension UnitTests {
    struct TestAdd: Test {
        func run() async {
            assert(customAdd(4, 5), equals: 9)
            assert(customAdd(5, 4), equals: 9)
        }
    }
    
    // Other tests...
}

After you write your tests, make sure to connect them with a suite:

// Tests/MyAppTests/MyAppTests.swift

@testable import MyApp
import TestRunner

final class MyAppTests: TestSuite {}

Now, simply run swift test on your package's directory or test using your IDE of choice.

To divide your tests into categories, you may want to use namespaces. Thus, the above sample would look like this:

// Sources/MyApp/CustomAdd.swift

func customAdd(_ a: Int, _ b: Int) -> Int {
    a + b
}

// We declare the namespace.
enum AddTests: UnitTestNamespace {} 

extension AddTests { // <- Here we use our namespace
    struct TestAdd: Test {
        func run() async {
            assert(customAdd(4, 5), equals: 9)
            assert(customAdd(5, 4), equals: 9)
        }
    }
    
    // Other tests...
}

Lastly, we have to specify our namespace in the test suite, found under the Test directory.

// Tests/MyAppTests/MyAppTests.swift

@testable import MyApp
import TestRunner

final class MyAppTests: TestSuite {}

// We add a new test suite for each namespace.
final class AddTests: TestSuite {
    static var testNamespace: UnitTestNamespace.Type {
        MyApp.AddTests.self
    }
}

Adding as Package Dependency

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/filip-sakel/SourceTests", branch: "main"),
    ],
    targets: [
        .target(name: "<MyApp>", dependencies: [
            // other dependencies
            .product(name: "SourceTests", package: "SourceTests"),
        ]),
        .testTarget(name: "<MyAppTests>", dependencies: [
            "<MyApp>",
            .product(name: "TestRunner", package: "SourceTests"),
        ])
        // other targets
    ]
)

GitHub

link
Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

Initial release v0.0.1
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics