Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm developing a Django website. I use Selenium for functional testing (from end-user point of view) and do TDD. I also do basic unit-testing of all of my models.

I usually write a new functional Selenium test (e.g. for submitting a form and checking that updates are there) and then write somewhat a lot of code to make the test pass. I create a view, some forms, methods of models, templates etc. After all, the test passes and I go on to the next Selenium test.

The problem is that this approach does not feel completely right. Maybe I should write more unit tests as well, so here are the questions:

  1. Why do I need unit tests if functional tests seem to get things done? Note that my Selenium tests are fully automated and managed via Jenkins CI, so there is no problem to manage many functional tests, and run them after every commit.
  2. Which parts of the app should I unit-test?

P.S. The great book Test-Driven Development with Python by Harry Percival (available online for free) suggests that you use the following workflow:

testing workflow

like image 940
Dennis Golomazov Avatar asked Aug 01 '13 20:08

Dennis Golomazov


People also ask

What is the point of unit testing?

The main objective of unit testing is to isolate written code to test and determine if it works as intended. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

Can Selenium be used for unit testing?

Unit Testing can be performed in both manual and by automation. Unit Testing Frameworks can be used in Selenium to automate/support/perform any of the below: Controls the flow of test case execution.

What should you test in Django?

As discussed above, we should test anything that is part of our design or that is defined by code that we have written, but not libraries/code that is already tested by Django or the Python development team. For example, consider the Author model below.

Can Selenium be used with Django?

Django 1.4 got built-in selenium support, and you can continue to use django-selenium with it, while keeping the same shortcut webdriver functions.


1 Answers

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.

like image 69
Brian Hoover Avatar answered Oct 10 '22 18:10

Brian Hoover