When and why should I switch from functional Selenium testing to unit testing in a Django web app? When and why should I switch from functional Selenium testing to unit testing in a Django web app? selenium selenium

When and why should I switch from functional Selenium testing to unit testing in a Django web app?


I'm using both selenium and unit tests right now, and here are some of my thoughts:

Unit test run faster than selenium scenario tests.Unit tests are easier to write then selenium scenario tests.Unit tests are much more granular than selenium scenario tests.Unit tests are less brittle than selenium scenario tests.

My selenium tests tend to run in a matter of minutes (they are several pages long) as compared to unit tests which are designed to run in less than a second. I have to spend a lot of time setting up the environment for a specific selenium test and then run it, and then check that the entire environment is in the right state. To test even a slightly different scenario, say, what happens if someone enters a bad value for one field, I have to start over from scratch.

Unit tests, on the other hand, especially with some clever mocking, don't need the whole environment. Setup the needed inputs for a given method or function, run the method or function and then test the outputs. They tend to have a lot less "setup" to make it run correctly.

Unit tests are also a lot less brittle than selenium tests. The selenium tests are very dependent not only on what you are testing, but the structure of the webpage itself. Make a modification to the HTML of the view layer, and you've got a chance of breaking a lot of Selenium tests. Because unit tests are more contained, they are less dependent a lot of other factors such as the exact structure of the view layer.

In general, I start with unit tests and then use my selenium tests for the broader tests, not writing a selenium test for each and every scenario.