A Swift package that provides data-driven testing functionality for your project.
To run a test with data-driven testing, you just need to run dataTests
method on your test case:
@testable import DataDrivenTesting
import XCTest
final class DataDrivenTests: XCTestCase {
func testDescriptionFromInteger() {
dataTests([
TestData((input: 1, expected: "1")),
TestData((input: 2, expected: "2")),
]) { testData, _ in
let result = testData.data.input.description
XCTAssertEqual(result, testData.data.expected, file: testData.file, line: testData.line)
}
}
}
This will run the test for each data from array using passed closure. To get more readable test results, you can check Report Navigator in Xcode. It will show you all test cases with data that was used to run them.
Passing file and line parameters to XCTAssertEqual
is optional, but it will help you to find the failing test case in the code.
You can also change default activity name by passing name as a second parameter to TestData
struct:
func testSumOfInt() {
dataTests([
.init(([], 0), name: "Empty array"),
.init(([1], 1), name: "One value in array"),
.init(([2,3], 5), name: "Two values in array"),
]) { testData, activity in
let result = testData.data.0.reduce(0, +)
XCTAssertEqual(result, testData.data.1, file: testData.file, line: testData.line)
}
}
Which will result in Report Navigator like this:
If you need add any attachments to your test case, you can use XCTActivity
parameter in closure:
private struct CEO: Encodable {
let firstName: String
let lastName: String
}
func testUsingActivity() {
dataTests([
TestData(CEO(firstName: "Steve", lastName: "Jobs")),
TestData(CEO(firstName: "Tim", lastName: "Cook")),
]) { testData, activity in
let json = try! JSONEncoder().encode(testData.data)
let attachment = XCTAttachment(data: json)
attachment.lifetime = .keepAlways
activity.add(attachment)
}
}
⚠️ Warning: By default, Xcode will try to add the DataDrivenTesting package to your project's main application/framework target. Please ensure that DataDrivenTesting is added to a test target instead, as documented in the last step, below.
https://github.com/WezSieTato/DataDrivenTesting
If you want to use DataDrivenTesting in any other project that uses SwiftPM, add the package as a dependency in Package.swift
:
dependencies: [
.package(
url: "https://github.com/WezSieTato/DataDrivenTesting",
from: "1.0.0"
),
]
Next, add DataDrivenTesting
as a dependency of your test target:
targets: [
.target(name: "MyApp"),
.testTarget(
name: "MyAppTests",
dependencies: [
"MyApp",
.product(name: "DataDrivenTesting", package: "DataDrivenTesting"),
]
)
]
If you want to use DataDrivenTesting in any other project that uses CocoaPods, add the package as a dependency in Podfile
:
pod 'DataDrivenTesting', '~> 1.0'
This library is released under the MIT license. See LICENSE for details.
link |
Stars: 2 |
Last commit: 3 days ago |
🎉 I'm excited to announce the first stable release of DataDrivenTesting, a Swift package that provides data-driven testing functionality for your project. 🎉
With this release, developers can now easily test their code using multiple sets of data and get more readable test results in Xcode's Report Navigator.
Features in this release include:
dataTests
methodI hope that this package will simplify the testing process and make it easier for developers to catch bugs and edge cases. We encourage users to try it out and let us know what they think.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics