I am facing an issue while writing UI test cases for IOS using XCTest framework.
Consider a 3 step signup process and 3 test cases for each step. And for running each test case, it requires the user to be logged out. So, I have written the code as
test1(){
//some code here for step 1
}
test2(){
test1()
//some code here for step 2
}
test3(){
test2()
//some code here for step 3
}
This works fine while running the test cases individually. But when running it collectively, the first test runs successfully but the second test fails, as it requires the registered user to logged out before running the text. Now to solve this, I write the code as follows :
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
}
Now the issue is, while running the second test case, it calls the first function which logs out the user and we cannot reuse the code. Any better way to do this?
Yes, sure. Don't call test functions from another test function, but create new functions for actions, like this:
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
}
Then, in your tests, call those action functions, like this:
func test1() {
action1()
logoutAction()
}
func test2() {
action2()
logoutAction()
}
func test3() {
action3()
logoutAction()
}