【问题标题】:IOS UI Testing issue for Logout scenario注销场景的 IOS UI 测试问题
【发布时间】:2018-08-03 12:57:19
【问题描述】:

我在使用 XCTest 框架为 IOS 编写 UI 测试用例时遇到问题。
考虑一个 3 步注册过程和每个步骤的 3 个测试用例。并且为了运行每个测试用例,它需要用户退出。所以,我把代码写成

test1(){
    //some code here for step 1
}

test2(){
    test1()
    //some code here for step 2
}

test3(){
    test2()
    //some code here for step 3
}

这在单独运行测试用例时效果很好。但是集体运行时,第一个测试运行成功,第二个测试失败,因为它要求注册用户在运行文本之前注销。 现在为了解决这个问题,我编写代码如下:

test1(){
    //some code here for step 1
    //logout code
}

test2(){
    test1()
    //some code here for step 2
    //logout code
}

test3(){
    test2()
    //some code here for step 3
    //logout code
}

现在的问题是,在运行第二个测试用例时,它调用了第一个注销用户的函数,我们无法重用代码。 有更好的方法吗?

【问题讨论】:

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


    【解决方案1】:

    是的,当然。不要从另一个测试函数调用测试函数,而是为操作创建新函数,如下所示:

    private func action1() {
        //some code here for step 1
    }
    
    private func action2() {
        action1()
        //some code here for step 2
    }
    
    private func action3() {
        action2()
        //some code here for step 3
    }
    
    private func logoutAction() {
        //logout code
    }
    

    然后,在您的测试中,调用这些操作函数,如下所示:

    func test1() {
        action1()
        logoutAction()
    }
    
    func test2() {
        action2()
        logoutAction()
    }
    
    func test3() {
        action3()
        logoutAction()
    }
    

    【讨论】:

    • 谢谢!它有帮助
    【解决方案2】:

    您应该使用XCTestsetup()tearDown() 方法来重置应用程序的状态。

    另外,不要调用另一个测试。每个测试都应该自行设置,而不是依赖于其他测试。如果测试之间有共同的功能,您可以在测试类中创建一个从每个测试调用的函数。

    【讨论】:

    • 嗨,霍德森,感谢您的建议。我同意您的观点,即“每个测试都应该自行设置,而不是依赖于其他测试”。每个测试用例的注销实现会有所不同,所以不能在tearDown()方法中编写注销代码。
    猜你喜欢
    • 2011-09-07
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2018-10-26
    • 2022-10-01
    • 1970-01-01
    • 2012-02-25
    相关资源
    最近更新 更多