1

I have found this question Can I run an XCTest suite multiple times? Which basically ask how to run XCTestCase suite multiple times. However, I would like to run only one method multiple times. How can I run for instance

final class MyTests: XCTestCase {
  var subject: Subject!

  override func setUp() {
    super.setUp()
    self.subject = Subject()
  }

  func testSujectDoesStuff() async {
      subject.stuff()
  }

Then, I want to run testSujectDoesStuff 10 times and fail fast. Xcode itself allows you to do this with

enter image description here

However, I would like to do it programatically instead of relying on the IDE.

1 Answers1

0

Every time the test is invoked, it will also carry the var invocation: NSInvocation? along side with the attempt to run the test case.

So, you just have to check the right selector for the test.

override func invokeTest() {
    if invocation?.selector == #selector(self. testSujectDoesStuff) {
      (1...10).forEach { _ in
        super.invokeTest() // It will run the test case 10 times
      }
      return
    }
    super.invokeTest()
  }