【问题标题】:Call a method only once within XCUITest in Swift 4在 Swift 4 的 XCUITest 中只调用一次方法
【发布时间】:2018-12-24 07:58:41
【问题描述】:

我的测试套件包含一些测试用例。我有一些私人功能,我将检查元素的存在。考虑我有三个测试用例:

func test_1() {
    ...
    checkListViewElements()
    ...
}

func test_2() {
    ...
}

func test_3() {
    ...
    checkListViewElements()
    ...
}

private func checkListViewElements() {
    //Checking existence
}

由于我认为每个测试用例都是独立的,因此私有函数 checkListViewElements() 可能会在测试用例中重复。

问题:

  • 当我运行整个测试套件时,所有三个测试用例(test_1test_2test_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


    【解决方案1】:

    您可以使用函数func setUp()。每间套房只调用一次。更多信息请访问What is the purpose of XCTestCase's setUp method?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2023-03-16
      相关资源
      最近更新 更多