Django+Selenium: Automated Testing Framework
Automated Testing is a widely used tool that uses a collection of predefined test cases. Learn how Django and Selenium to ensure a bug free system
Table of contents
If you are developing a web application, as the interaction between components becomes more complex, a small change in one area can impact other areas. And if you are working in a team, there are chances that someone else's code might break your changes. So before committing any new updates to your system make sure that everything works as expected and is bug-free, especially the core components.
Automated Testing is a widely used tool that uses a collection of predefined test cases which is called a test-suite to solve, or avoid, a number of unexpected surprises.
To get the most out of it, your test-suite should:
- Ensure that the current system is bug-free.
- Validate if your code works as expected.
- Any new changes haven't affected the entire application's behaviour.
Testing In Django
Django is an open-source high-level Python web framework that allows you to quickly create secure and maintainable web applications. It comes with a well defined test-execution framework that can easily and reliably be run every time you make a change. You can simulate requests, insert test data, inspect your application's output and hence verify that everything in your application is working as expected.
What is Selenium?
Selenium is an open-source tool encapsulating a variety of tools and libraries that automates web browsers. It can be used as an automated testing suite, to test web applications across different browsers and platforms. With Selenium, you can automate user interaction on a given website as if a real user is performing the actions.
Live server testing with Django and Selenium
Among the assorted utilities that Django provides for testing automation, you can use the LiveServerTestCase class from which your Selenium test classes can be inherited. It launches a live Django server in the background on setup. It will shut it down once finished with all the tests in it.
Let's do a small workaround
I assume you have your Django application ready. First of all, you need the python package of Selenium installed in your virtual environment.
It requires a web driver to be configured with Selenium and this can be any of GoogleChrome, Firefox etc. In our example below, we are using Google Chrome.
To inherit the behaviour of a browser your webdriver needs to be added as an executable to your python path. Since we are using Google Chrome, you can download the chrome driver and put it in a path easily accessible from the project root.
Now let's write a simple test case to check the user login using a user account.
Create a file named test_user_login.py in your application's tests directory. In this example, we are using the StaticLiveServerTestCase class which is a base class of LiveServerTestCase.test_user_login.py
In the above example, the live server listens on localhost and binds to port 8888 as defined. And self.live_server_url will have the live server's endpoint at that time.
Execute Test Case
And to execute your test case, just run the below command from your terminal:
In the above example, it will open a web browser and visit our target URL at http://localhost:8888/accounts/login/. You can see a live browser opened while the test is running unless you have explicitly turned on the HEADLESS mode.
Then it will look for specific elements by their <input> tag name. For example, in the above code, the name of the username input field is 'username'. Next, we use the send_keys() method to automate inputting user credentials of the user that we had created in the setUp() method and hence submit the form.
As you can see all these are done automatically as precisely as we defined in our test case. To improve the efficiency of your test cases, the Python TestCase class has a variety of Assertion Methods to check for and report failures. Here is the output of the above test case:
Conclusion
Always make sure your tests are well structured and have the maximum coverage in the entire application. If you are building a web application it's actually a vital part to test and see how your system interacts with real users. With automated testing tools, you can easily simulate any kind of user interaction in your web application.
Just put it as a thumb rule - for any change made in the application, you have a test case with maximum coverage in place to ensure everything is working as expected.
One of our letting experts will ensure you get the most value from your property