Writing Functional Test python in Django Web Framework

Feri Lukmansyah
Remote Worker Indonesia
3 min readSep 16, 2021
image from pexels

hello and welcome to my blog, in this post I will discuss how to writing unitest (functional test) with python,

What is Functional Testing

Tests that use Selenium let us drive a real web browser, so they really let us see how the application functions from the user’s point of view. That’s why they’re called functional tests.

Project Preparing

before implementing functional testing on Django project using python we need, to set up several things like

  • Create Virtual Environment
  • installing the required package
  • creating Django project
  • Set-Up unittest

Create Virtual Environment

o create a virtual environment, using this command

python -m venv django-tdd

and activate

source django-tdd/bin/activate

Installing required Package

to writing unittest, I need to Install some python package, the package is

install with pip

pip install selenium webdriver-manager django

Creating Django Project

to create a Django project using this command

django-admin startproject config

we create Django project named config

then the project structure will be like this

Writing Functional Testing

create a file named functional_test.py then write code like this

in this case, we write the test to check whether the website is running or not

on this setUp the method we initialize the driver to remote our chrome browser

and method tearDown is to set up when the test case is complete

and method test_start_web is a method to test our web

and then you run this case you got a failed response because the title is not the same with todo

======================================================================FAIL: test_start_web (__main__.NewVisitorTest)----------------------------------------------------------------------Traceback (most recent call last):File "/Devs/JOBS/Django Project/django-tdd/fuctional_tests.py", line 33, in test_start_webself.assertIn('Todo', self.driver.title)AssertionError: 'Todo' not found in 'The install worked successfully! Congratulations!

Terminology: Functional Test == Acceptance Test == End-to-End Test

What I call functional tests, some people prefer to call acceptance tests or end-to-end tests. The main point is that these kinds of tests look at how the whole application functions, from the outside. Another term is black box test because the test doesn’t know anything about the internals of the system under test.

FTs should have a human-readable story that we can follow. We make it explicit using comments that accompany the test code. When creating a new FT, we can write the comments first, to capture the key points of the User Story. Being human-readable, you could even share them with nonprogrammers, as a way of discussing the requirements and features of your app.

TDD and agile software development methodologies often go together, and one of the things we often talk about is the minimum viable app; what is the simplest thing we can build that is still useful? Let’s start by building that so that we can test the water as quickly as possible.

Conclusion

This means that an FT can be a sort of specification for your application. It tends to track what you might call a User Story, and follows how the user might work with a particular feature and how the app should respond to them.

thank you for reading, hopefully, it’s useful, click here to join our community, originally posted on my personal blog: https://blog.perykun.xyz/p/writing-functional-test-in-django-web-framework/

--

--