Last active
April 7, 2019 22:00
-
-
Save ryuta46/ec9912ca0afde1c8c650e3afe944477b to your computer and use it in GitHub Desktop.
Swift 4 XCTest parameterized test template and sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import XCTest | |
| // Parameterized Test Template | |
| class ParameterizedTest : XCTestCase { | |
| class func createTestCases() -> [ParameterizedTest] { | |
| fatalError(""" | |
| You must override createTestCases() in following way. | |
| override class func createTestCases() -> [ParameterizedTest] { | |
| return self.testInvocations.map { YourParameterizedTestSubClass(invocation: $0) } | |
| } | |
| """ | |
| ) | |
| } | |
| class var fixtures: [Any] { | |
| get { | |
| fatalError(""" | |
| You must override fixtures. | |
| e.g.: | |
| override class var fixtures: [Any] { | |
| get { | |
| return [ | |
| YourTestFixture(3, 9), | |
| YourTestFixture(4, 16), | |
| ] | |
| } | |
| } | |
| """ | |
| ) | |
| } | |
| } | |
| // Backing field of current fixture | |
| private var _fixture: Any? = nil | |
| // Current fixture. Fixture must not be nil. | |
| final private(set) var fixture : Any { | |
| get { | |
| return _fixture! | |
| } | |
| set { | |
| _fixture = newValue | |
| } | |
| } | |
| override class var defaultTestSuite: XCTestSuite { | |
| let testSuite = XCTestSuite(name: NSStringFromClass(self)) | |
| guard self != ParameterizedTest.self else { | |
| return testSuite | |
| } | |
| for fixture in self.fixtures { | |
| let testCases = self.createTestCases() | |
| for testCase in testCases { | |
| testCase.fixture = fixture | |
| testSuite.addTest(testCase) | |
| } | |
| } | |
| return testSuite | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import XCTest | |
| // Test sample | |
| struct MathTestFixture { | |
| let input: [Int] | |
| let addExpected: Int | |
| let multExpected: Int | |
| init(_ input: [Int], _ addExpected: Int, _ multExpected: Int) { | |
| self.input = input | |
| self.addExpected = addExpected | |
| self.multExpected = multExpected | |
| } | |
| } | |
| class MathTest : ParameterizedTest { | |
| override class func createTestCases() -> [ParameterizedTest] { | |
| return self.testInvocations.map { MathTest(invocation: $0) } | |
| } | |
| override class var fixtures: [Any] { | |
| get { | |
| return [ | |
| MathTestFixture([], 0, 1), | |
| MathTestFixture([0, 3], 3, 0), | |
| MathTestFixture([1, 3], 4, 3), | |
| MathTestFixture([1, 3, 5], 9, 15), | |
| MathTestFixture([2, 3], 4, 5), // error | |
| ] | |
| } | |
| } | |
| // Don't run test method. Run test class instead. | |
| func testAdd() { | |
| let fixture = self.fixture as! MathTestFixture | |
| var actual = 0 | |
| fixture.input.forEach { actual += $0 } | |
| XCTAssertEqual(fixture.addExpected, actual) | |
| } | |
| func testMult() { | |
| let fixture = self.fixture as! MathTestFixture | |
| var actual = 1 | |
| fixture.input.forEach { actual *= $0 } | |
| XCTAssertEqual(fixture.multExpected, actual) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any way to get this to work when you run the unit tests via the test navigator (or just running a single test suite?)
For some reason, defaultTestSuite's don't seem to get picked up unless the Product -> Test command is used (was in an Apple radar in 2015 as well)