Search code examples
iosswiftxcodeunit-testingxcode-ui-testing

UI Tests Pass Manually, Fail When Ran Automatically


I am trying to run my UI Tests all at once. All the tests work fine when I run them individually, but when I run them automatically all together the tests fail because Xcode does not update where it is at in the app to successfully run the other tests.

So just to clarify, my first test is the signUpTest, so I'll run that test and it will work and then it will go to the second test which is the signUpMyInfo test. My issue is that this second test will only run once the app has moved to the second view controller, which is where the first unit test ends, however when the second test begins for some reason Xcode throws the app back to the first view controller, causing the second test to fail.

In short, I am unclear why the app returns to the first view controller after the first test successfully passes rather than remaining on the second view controller and running the second test from there.

Also worth noting, that this problem does not always happen, Xcode does sometime successfully throw me to the next view controller where the second test passes fine, but this problem is happening enough that I have to bother someone on stack to help me solve it :p


Solution

  • At the end of each test Xcode runs, it will close the app, and you'll need to launch the app again at the beginning of each test. This is to encourage test independence, which allows each test to stand alone, so that you know that if a test fails, it fails because of something that test did, not as a result of something another test did. This makes test failures easier to diagnose and makes your test results more informative and accurate.

    You will need to add code in your second test to move from the first view controller to the second. This may seem like duplication at first glance, but as explained above, this is for the greater good of your test suite.