Xctest 注销场景的IOS UI测试问题

Xctest 注销场景的IOS UI测试问题,xctest,xcode-ui-testing,ios-ui-automation,Xctest,Xcode Ui Testing,Ios Ui Automation,在使用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 } 这在单独运行测试

在使用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
}
现在的问题是,在运行第二个测试用例时,它调用第一个函数,该函数注销用户,我们不能重用代码。
有更好的方法吗?

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

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()
}
然后,在测试中,调用这些操作函数,如下所示:

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()
}

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

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()
}
然后,在测试中,调用这些操作函数,如下所示:

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()
}

您应该使用XCTest的和方法来重置应用程序的状态


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

您应该使用XCTest的和方法重置应用程序的状态


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

嘿,霍德森,谢谢您的建议。我同意你的观点,即每个测试都应该自行设置,而不是依赖其他测试。注销的实现对于每个测试用例都是不同的,所以不能用tearDown方法编写注销代码。Hey Hodson,谢谢你的建议。我同意你的观点,即每个测试都应该自行设置,而不是依赖其他测试。注销的实现对于每个测试用例都是不同的,因此无法使用tearDown方法编写注销代码。谢谢!它帮助了汉克斯!这有帮助