【问题标题】:Extension vs Class in UITests helper classUITests 助手类中的扩展与类
【发布时间】:2019-07-17 12:13:52
【问题描述】:

我想为我的 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中使用时)

问题:

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

【问题讨论】:

  • 我总是使用第二种方法。这比第一种方法更简洁,对我来说更有意义。

标签: class static extension-methods helper xcuitest


【解决方案1】:

如果您想向 XCTestCase 添加一些自定义方法,最好使用 method #2 并将整个扩展名放在单独的文件/文件夹中在您想使用自定义方法的地方附近

另一方面,如果您想覆盖 XCTestCase 的方法,则不应使用 method #2,因为它违反了语言指令。更多详情,您可以在Apple Developer Guidehere 阅读。

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2011-10-31
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多