【问题标题】:Why no conditional statements in functional testing frameworks like KIF?为什么像 KIF 这样的功能测试框架中没有条件语句?
【发布时间】:2014-07-10 12:25:47
【问题描述】:

我是 iOS、xcode、KIF 框架、Objective C 的新手。我的第一个任务是使用 KIF 编写测试代码。如果 KIF 有条件语句,它肯定会容易得多。

基本上是这样的:

if ([tester existsViewWithAccessibilityLabel:@"Login"]) {
    [self login];
}
// continue with test in a known state

当您一次运行一项测试时,KIF 在测试后退出应用程序。如果您一次运行所有测试,它不会在测试之间退出 - 要求测试人员非常非常小心应用程序的状态(这非常耗时且不好玩)。

【问题讨论】:

    标签: ios testing integration-testing functional-testing kif-framework


    【解决方案1】:

    您总是可以尝试tryFindingViewWithAccessibilityLabel:error: 方法,如果可以找到它则返回true,否则返回false。

    if ([tester tryFindingViewWithAccessibilityLabel(@"login") error: nil]){
        //Test things
    }
    

    【讨论】:

      【解决方案2】:

      每个测试的第一步应该是将应用重置为已知状态,因为这是保证可重复测试的唯一方法。一旦您开始将条件代码放入测试本身,您就会在结果中引入不可预测性。

      【讨论】:

        【解决方案3】:

        测试框架通常不会实现 if 条件,因为它们已经以其原生形式存在。
        您可以查看测试框架的源代码以了解它如何进行“如果状态检查”。这将教你如何做大部分你可能想做的事情(即使在功能测试期间做这些事情并不总是一个好主意)。你也可以看这里:Can I check if a view exists on the screen with KIF?

        此外,您的测试本质上应该是自信的,遵循以下工作流程:

         given:
            the user has X state setup  
            (here you write code to assertively setup the state)
        
            It is OK and preferred to isolate your tests and setup
            the "given" state (e.g. set login name in the model directly without going
            through the UI) as long as you have covered that behavior in another test. 
        
            When:
            The user tries to do X
            (here you tap something etc..)
        
            Then:<br>
            The system should respond with Z
            (here you verify the system did what you need it) 
        

        【讨论】:

        • KIF 框架像用户一样“驱动” UI - 因此测试代码根本无法访问任何 UI 元素或任何其他代码。除非您执行添加单例引用(或类似操作)之类的操作,否则您基本上对代码中发生的事情视而不见。
        • 很好的澄清@Gerry
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 1970-01-01
        相关资源
        最近更新 更多