首页 > 解决方案 > UITests 助手类中的扩展与类

问题描述

我想为我的 UI 测试编写助手类。

A.swift(测试用例类)

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

B.swift(助手类)

方法一:

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

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

B().sampleHelper()将调用 sampleHelper 函数(在A中使用时)

方法二:

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

sampleHelper()将调用辅助函数(在A中使用时)

问题:

编写辅助类的最佳方法是什么?我知道扩展是静态的,但如果代码库很大,它真的会影响内存/性能吗?

标签: classstaticextension-methodshelperxcuitest

解决方案


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.


推荐阅读