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.
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.
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.
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.
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
}
}
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
]
)
link |
Stars: 0 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics