Check out How to keep you tests non-flakey by JR. Here is an excerpt:
Something I am always striving to avoid in my unit tests is flakiness. That’s why when I needed to deal with a UINavigation bar in the previous version of iMessage, I ended up writing a method that looked like this:
This method puts the responsibility on the client to figure out the runtime of…
The full article is available here.