TestCaseKit is a Swift Macro that lets you create test cases with arguments, without needing to create separate tests for each set of arguments.
@testCase(3, 1, 4)
@testCase(5, 3, 8)
@testCase(6, 12, 18)
func testSumNumbers(_ lhs: Int, _ rhs: Int, _ result: Int) {
XCTAssertEqual(lhs + rhs, result)
}
@testCase("1", "1", "11")
@testCase("5", "3", "53")
@testCase("6", "12", "612")
func testConcatStrings(_ lhs: String, _ rhs: String, _ result: String) {
XCTAssertEqual(lhs + rhs, result)
}
@testCase(nil as String?)
func testOptional(_ x: String?) {
XCTAssertNil(x)
}
To use TestCaseKit, simply import the library into your test project and add the @testCase
attribute to your test function. The @testCase attribute takes a variable number of arguments, which will be passed to the test case as arguments.
To install TestCaseKit in Xcode go to your project settings, click Package Dependencies
> +
> Enter https://github.com/alimyuz/TestCaseKit in the search bar, add the package to your test target.
OR simply add the following dependency to your Package.swift file:
dependencies: [
.package(url: "https://github.com/alimyuz/TestCaseKit.git", .upToNextMajor(from: "1.0.0")),
]
After the above steps you might get a warning in Issue Navigator. You need to click Trust the Library to procceed. After this step, you are good to go!
- Currently the external parameter name in test functions should be ommitted for the attribute to work (see Example above).
- Do not pass nil without specifying the desired argument type.
@testCase(nil) // won't work
@testCase(nil as String?) // will work