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

I am developing a Django site. I use Selenium for functional testing (from an end user perspective) and TDD. I also do basic unit testing of all my models.

Usually I write a new Selenium functional test (for example, to submit a form and check for updates there), and then I wrote code several times to pass the test. I create a view, some forms, model methods, templates, etc. In the end, the test passes, and I move on to the next Selenium test.

The problem is that this approach is not entirely correct. Maybe I should write some more unit tests, so here are the questions :

  • Why do I need unit tests if the functional tests seem to have done everything? Please note that Selenium tests are fully automated and managed through Jenkins CI, so there is no problem managing many functional tests and running them after each commit.
  • What parts of the application should be performed during testing?

PS The great Test-Driven Development book with Python by Harry Percival (available online for free) suggests using the following workflow:

testing workflow

+4
source share
1 answer

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

Unit tests are faster than selenium script tests. Unit tests are easier to write than selenium script tests. Unit tests are much more granular than selenium scenario tests. Unit tests are less fragile than selenium scenario tests.

My tests for selenium, as a rule, are carried out within a few minutes (their length is several pages) compared to single tests, which are designed for less than a second. I have to spend a lot of time setting up the environment for a specific selenium test and then running it and then checking that the whole environment is in the correct state. To test even a slightly different scenario, let's say what happens if someone enters a bad value for one field, I have to start from scratch.

Unit tests, on the other hand, especially with some clever taunts, do not need the whole environment. Configure the required inputs for this method or function, run the method or function, and then check the outputs. They usually have a much smaller “tweak” to make it work correctly.

Unit tests are also much less fragile than selenium tests. Selenium tests are very dependent not only on what you are testing, but also on the structure of the web page itself. Make changes to the HTML layer of the view, and you have a chance to break many Selenium tests. Because unit tests are more grouped, they are less dependent on many other factors, such as the exact structure of the presentation layer.

In general, I start with unit tests, and then use selenium tests for wider tests, rather than writing a selenium test for each scenario.

+8
source

Source: https://habr.com/ru/post/1494821/


All Articles