Free landlord compliance checklist

Equip yourself with all the knowledge you will need to be a fully compliant and law-abiding landlord. Just fill in your details and we'll send a compliance checklist right to your inbox for FREE!

compliance checklist book artwork

Thank you!

The free download has been sent to your email. Please check your account to ensure you have received it
Oops! Something went wrong while submitting the form.

Free landlord compliance checklist

Equip yourself with all the knowledge you will need to be a fully compliant and law-abiding landlord. Just fill in your details and we'll send a compliance checklist right to your inbox for FREE!

compliance checklist book artwork

Thank you!

The free download has been sent to your email. Please check your account to ensure you have received it
Oops! Something went wrong while submitting the form.
Landlords - Find quality tenants faster than ever before for only £900 flat rate
Enquire Today
28 September 2021

Django+Selenium: Automated Testing Framework

28 September 2021
Written by
Akhil Sundar

The top news stories this week in the world of UK property: London landlords are being offered a £6,000 incentive

A home for sale in the UK with a for sale sign outside

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.

screen shot of 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

code example for django & selenium

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:

python manage.py test

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:

code example of using Django with Selenium

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.

April 13, 2023
April 13, 2023
Latest News: Landlords offered £6,000 to house tenants on benefits

The top news stories this week in the world of UK property: London landlords are being offered a £6,000 incentive

Read More
April 3, 2023
April 3, 2023
Latest News: Landlords to evict disruptive renters with 2 weeks notice

The most recent info, news, and insights on the UK property market for both tenants and landlords and everyone in between.

Read More
March 24, 2023
March 24, 2023
Latest News: Why have house prices risen in March?

All the latest news, commentary and insights on the UK property market for landlords, tenants, and everybody in between.

Read More
March 17, 2023
March 17, 2023
Latest news: The budget breakdown

In the wake of the Spring 2023 budget, we're bringing you all the latest headlines from the UK property world.

Read More
A UK red brick property for sale
April 13, 2023
April 13, 2023
Latest News: Landlords offered £6,000 to house tenants on benefits

The top news stories this week in the world of UK property: London landlords are being offered a £6,000 incentive

January 9, 2023
January 9, 2023
Property News: Where to buy in London in 2023

The very latest property news for landlords in 2023, from the best areas in London to invest this year, to the increasing importance of EPC to tenants.

Read More
December 16, 2022
December 16, 2022
Property News: Mortgage companies help borrowers

The latest news for landlords, from mortgage companies' promises to help borrowers in need, to the predicted 8% decline in UK home values in 2023.

Read More
December 9, 2022
December 9, 2022
Property News: The Happiest Borough in London 2022

We're bringing you the latest property news, from which London borough has been voted the happiest, to rental premiums for eco homes.

Read More
December 1, 2022
December 1, 2022
Property News: John Lewis Announces £500m BTR Deal

We're bringing you the latest updates in property news, from John Lewis entering the London rental market to 1 million tenants facing eviction.

Read More
Antisocial tenants evicted after 2 weeks
April 3, 2023
April 3, 2023
Latest News: Landlords to evict disruptive renters with 2 weeks notice

The most recent info, news, and insights on the UK property market for both tenants and landlords and everyone in between.

November 22, 2022
November 22, 2022
Property News: London House Prices Fall £13k!

Find out the most recent developments in UK property news, from a nose dive in London house prices to half a million uses of the Mayor's online tool.

Read More
November 3, 2022
November 3, 2022
Property News: Home prices dropped in October

Find out the most recent developments in UK property news, from rising interest rates to the government's commitment to 300k homes per year.

Read More
October 24, 2022
October 24, 2022
October 2022 Property News

Get an update on all the latest happenings in the property market in October 2022. We cover raising rents, interest rates, converted barracks, and more.

Read More
September 23, 2022
September 23, 2022
UK Stamp Duty Cut: A guide to the threshold Changes

The threshold for paying stamp duty has doubled to £250,000 and for first-time buyers, it's been increased to £425,000. This guide explains the changes in full!

Read More
A family with children moving into a rental home
March 24, 2023
March 24, 2023
Latest News: Why have house prices risen in March?

All the latest news, commentary and insights on the UK property market for landlords, tenants, and everybody in between.