【发布时间】:2018-12-24 07:58:41
【问题描述】:
我的测试套件包含一些测试用例。我有一些私人功能,我将检查元素的存在。考虑我有三个测试用例:
func test_1() {
...
checkListViewElements()
...
}
func test_2() {
...
}
func test_3() {
...
checkListViewElements()
...
}
private func checkListViewElements() {
//Checking existence
}
由于我认为每个测试用例都是独立的,因此私有函数 checkListViewElements() 可能会在测试用例中重复。
问题:
- 当我运行整个测试套件时,所有三个测试用例(test_1、test_2 和 test_3)都会被执行。
- 私有方法
checkListViewElements()将被调用两次。这将导致测试套件完成时间的增加。
我想要的:
- 我的代码中有很多函数,例如
checkListViewElements()。当我运行整个测试套件时,我希望它们只运行一次。 (请记住,对于每个测试用例,应用程序都会终止并重新打开)
我尝试了什么:
var tagForListViewElementsCheck = "firstTime" //Global variable
private func checkListViewElements() {
if tagForListViewElementsCheck == "firstTime" {
//Checking existence
tagForListViewElementsCheck = "notFirstTime"
}
else {
//Skip
}
}
- 如果我使用局部变量作为标记,它工作正常。但是在这里,我必须为每个私有方法创建每个标签。我真的很讨厌那样。
- 我尝试使用
dispatch_once,但 Swift 4 似乎不支持它 - 然后我通过引用this 尝试使用静态结构。它似乎也不起作用。
如果还有其他好的方法可以做到这一点?提前致谢!
【问题讨论】:
标签: swift static-methods xcuitest xctestcase