TestProject Forum
Powered by leading experts in the test automation community

Explicit wait statements in unit testing

What is so harmful in explicit sleep statements in UI testing that i read everywhere that it is discouraged ?

Typically a sleep statement would pose as a delay in the application under test - one that does not exist during the test execution.

This is a bad practice since a programmatic delay is only a fake (mockup) of the actual delay in the application, and it will not be able to duplicate the complexity of the real delay.

If you were to take a network delay for example. Assume your app would take about 2-3 seconds to get some data over the network, and then proccess/render/parse it. So, under a test you would mock the data and throw in a 3 second sleep.

As simple as that sounds, this method of testing is inaccurate. You test cannot reflect how things will turn out in a real production enviroment. What if you have network problems or the data is unavailable? You are trying to test elements that are out of your control.
The 3 seconds sleep cannot replicate a network delay because most likely every data fetch over the network will be different from the previous one.