【问题标题】:Reusable XCTests for several products多种产品的可重复使用 XCTests
【发布时间】:2016-12-08 09:48:20
【问题描述】:

我需要对 XCode 项目进行 UI 测试,该项目是多个产品的平台。 这意味着某些元素是为某些产品定制的。例如,它们可以具有不同的颜色、文本样式等,从产品到产品,或者相同的元素对于一个项目是可见的,但对于另一个项目是隐藏的。

如何配置我的 XCode UI 测试以使其可重复用于各种产品?我知道我需要不同的模式。但是,例如可见性元素呢?看来我需要在 UI 测试代码中检查它?但我认为使用任何配置文件会更好。我对吗?有没有人有任何想法?我会很感激所有的建议。

【问题讨论】:

    标签: xcode xctest ios-ui-automation xcode-ui-testing


    【解决方案1】:

    我建议构建一套您在每个产品中使用的测试助手。这些是通用的参数化功能,例如登录用户或将商品添加到购物车。无需在这些帮助程序中硬编码您的 UI 元素表示,而是参数化输入。然后你可以反复使用它们。

    func addItemToCart(named: String, saveButtonName: String)
    func login(username: String, password: String, submitButtonText: String)
    func tapTableCell(imageNamed: String)
    

    一旦您为导航创建了基本脚手架,您就可以继续使用断言助手。将复杂的逻辑留在帮助程序中使您能够重用它们并保持产品特定测试的精简和可读性。

    func assertCurrentScreen(named: String)
    func assertHighlightedCell(colorNamed: String)
    func assertCartTotal(cents: String, containerIdentifier: String)
    

    对于所有这些函数,我建议在末尾添加两个默认参数,以记录调用者文件和行号。如果您做出任何自定义断言,则可以pass these references in to show your failure at the callers line, not the helpers

    func assertScreen(titled: String, file: StaticString = #file, line: UInt = #line) {
        if !XCUIApplication().navigationBars[titled].exists {
            XCTFail("Item was not added to cart.", file: file, line: line)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我在每次测试开始时都使用一个函数。

      class SomeTestsClass: XCTestCase {
          func testSomeTest() {
              var externalConfigVariable_1 = "value for defult test"
              var externalConfigVariable_2 = "value for defult test"
      
              // here You use the external config to override
              // the default test logc
              if let testConfig = getConfig(for: self.name) {
                  // read config parameters here
                  externalConfigVariable_1 = testConfig["test_var_1"]
                  externalConfigVariable_2 = testConfig["test_var_2"]
                  // ..........
              }
      
              // use the variables as You like
              // ......
          }
      }
      
      extension XCTestCase {
          public func getConfig(for name: String?) -> [String: Any]? {
              let nameComponents = name?.replacingOccurrences(of: "-", with: "").replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "").components(separatedBy: " ")
      
              if let fileName = nameComponents?.last {
                  let testBundle = Bundle(for: type(of: self))
                  guard let path = testBundle.url(forResource: fileName, withExtension: "JSON") else {
                      return nil
                  }
      
                  if let data = try? Data(contentsOf: path) {
                      if let testConfig = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any] {
                          return testConfig
                      }
                  }
              }
      
              return nil
          }
      }
      

      这是一个 JSON 示例:

      {
        "test_var_1": "Some var",
        "test_var_2": "Some other var"
      }
      

      【讨论】:

      • 是的,我想过这个,但我没有这方面的经验。你能举个例子来了解更多细节吗?
      • 我已经编辑了答案。我已经为 get config 方法编写了一个示例(有点粗略,但可以工作)。和一个示例用法。
      • 另外请记住,您可以在所有测试目标中添加XCTestCase 并重复使用它。
      猜你喜欢
      • 2013-02-15
      • 2019-12-04
      • 2017-12-23
      • 2010-10-16
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多