Question

Is there a way to add a method to your XCTestCase without it being counted as a test? Something like setUp and tearDown which exists already.

One usecase would be to do a certain operation on an object in some of my test cases at specific points in time, while accessing the instance variables of the class itself (which rolls out the possibility of using static methods and external helper files)

Was it helpful?

Solution

Only methods that start with test will be considered tests. So, just name your helper method something that doesn't start with test.

By the way, I would not be inclined to put XCTAssert statements in your helper method. It works (the appropriate tests will fail), but in some scenarios it makes it hard to decipher which tests caused the assert in the helper method to fire.

OTHER TIPS

I use what Steve suggested with the optional line parameter in XCTAssert functions so the test failure appears at the callsite and append check instead of test so it isn't recognized as one.

private func checkSunSetsInTheEast(callLine: UInt = #line) {
    XCTFail(callLine: #line)
}

An alternative if you prefer your asserts to be visible in the actual test would be:

func testSun() {
    XCTAssert(sunSetsInEast)
}

private var sunSetsInEast: Bool { return false }

Keeping the test prefix and setting the test to private will stop them being run as tests but XCode's UI will still recognize them as a test so it's probably best to avoid prefixing test at the beginning of helper functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top