Search code examples
objective-cswiftxcodexctestxctestcase

Performing an action after a test suite has finished running in XCTest


I need to make an API call after all the tests in my test suite has finished running. I'm aware of the method testSuiteDidFinish(_ testSuite: XCTestSuite), this method does get called after a test suite execution, however it gets called as many times as the number of test cases I have. In my test suite I have three test cases. I've added an observer to the test class. In my observer class, this is what I have:

public func testSuiteDidFinish(_ testSuite: XCTestSuite) {
   print("done")
}

"Done" gets printed three times after the execution of three tests. Why is doing so?


Solution

  • The function below works fine for what I was trying to achieve:

    override class func tearDown() { 
       }
    

    Whatever is written within it gets called only once, after my tests when a test class finishes executing.