Search code examples
classstaticextension-methodshelperxcuitest

Extension vs Class in UITests helper class


I want to write helper class for my UI tests.

A.swift (Test cases class)

class A:XCTestCase {
//contains test cases, setUp() & tearDown()
...
}

B.swift (Helper class)

Method 1:

class B:XCTestCase {
//only helper functions, no setUp, no tearDown and no test cases

func sampleHelper() {
 ...
}
...
}

B().sampleHelper() will call the sampleHelper function (when used within A)

Method 2:

extension XCTestCase {
//only helper functions, no setUp, no tearDown and no test cases
func sampleHelper() {
 ...
}
...
}

sampleHelper() will call the helper function (when used within A)

Question:

Which is the optimal method in writing helper classes? I know extension is static but does it really affect the memory/performance if the code base is huge?


Solution

  • If you want to add only some custom methods to XCTestCase it is better and more clear to use method #2 and place the whole extension to separate file/folder somewhere near the place you want to use your custom methods

    On the other hand, you should not use method #2 if you want to override methods of XCTestCase as it is violation of the language directive. More details you can read in Apple Developer Guide and in here.