swiftlang / swift-corelibs-xctest

The XCTest Project, A Swift core library for providing unit test support
swift.org
Apache License 2.0
1.15k stars 267 forks source link

Add async support for class tearDown() function #415

Open shepazon opened 2 years ago

shepazon commented 2 years ago

While we now have async versions of the per-test setUp() and tearDown() functions, we don't yet have an async version of the class-wide tearDown(). Adding this would allow tests that are able to ensure that any artifacts left behind by the main testing are cleaned up before the test suite ends:

override class func tearDown() async {
    super.tearDown()

    do {
        await someCleanupFunction()
    } catch {
        /* do error handling */
    }
}

I've tried using a Task() and a semaphore in the class tearDown(), but the task gets terminated early when the main test program exits.

grynspan commented 1 year ago

Sorry for the late reply. If you are blocked on a solution here, consider using addTeardownBlock() instead:

func testWhatever() {
  addTeardownBlock {
    await someTeardownWork()
  }
  XCTAssertTrue(whatever())
}