Test Driven Development approach using Django Rest Framework

Test-Driven Development is not something that happens naturally. It’s a discipline, as a martial art, and just like in a Kung-Fu movie, we need an irritated and disobliging master to force us to learn the discipline. Ours is Testing.

It is bad practice to jump straight in and start to code because it is a basic thing you must think about before acting. But in the end, the cleanest way to reach a goal is to write a plan and then implement it. This applies to code efficiency, and TDD enables this discipline to write good-quality code. 


Why do we need to create tests?

When we plan to build a website, even if we’re not quite sure what it’s going to do yet, basically, the first step in web development is getting your framework installed and configured. Once configured, run the script, but TDD requires a different mindset and different scenarios. When we’re doing TDD, the aim is to create clean, simple, quality code that satisfies the requirements with minimal code bloat. 

In test development, the first step is always the same: write a test.

First, we write the test, then run it and check that it fails or gives output as expected. Only then do we go ahead and build some of our apps. Repeat that until you achieve what you expect as output. 

TDD Process Flow

The Basic

There are four different types of tests, each depending on the quality of code being tested, as well as the goal of the test.

1. Unit Test

Unit test cases test the single unit or piece of code that verifies a piece of code works as expected without considering the rest of the code. This will help to bug-free pieces of code. Unit testing is the base of testing. We can’t think that without a unit testing program, it will work as expected.

2. Feature Test

This testing comes mostly after the unit because the goal of feature testing that verifies the component works as expected as they specify requirements. Sometimes unit testing is not part of a feature test.

3. Integration Test

Integration tests mainly check all functionality on one common platform like all integration, all combinations of units working as expected or not, and simplify its end-to-end testing where various modules integrate into one form.

4. Performance Test

As the name suggests the type of testing ensures that the software or application works as expected or well under their workload. It mostly checks for workload balancing of different modules or functionality in different workload

Some Functions of Testing

1. assertEqual()
This assertion checks that its two parameters are equal along with the actual values of the parameters and return True is successfully else False.

2. assertNotEqual()
The assertNotEqual assertion success when assertEqual fails, its output indicates that the values are not equal. It accepts 3 params and returns results depending upon assert expression, returns True else False.

3. assertContain()
Asserts that a Response instance means it is specifically used for producing the mentioned status_code and that text comes in the content of the response.

  Django Setup

We’ll proceed with the next small steps now we’re going to use Django for writing tests, which is a popular Python web framework, to build our app. The first thing we want to do is check that we’ve got Django installed and that it’s ready for us to work with all the setup. We’ll write our test case in tests.py which we have already created in the installed app. 

The way we’ll check is by confirming that we can check Django’s development server is running or not in our web browser, on our local PC.

Example of Test Cases 

Let’s start with writing test cases

1. Firstly we will write the test case by creating models for our view. This test case for creation Dependent Details model for writing view 

Wow..! We successfully ran the first Test case for the model. 

Now we’ll write the next test case for views. However, it’s easy to know what to test when you know what to implement when you are clear on requirements and what is expected output. In our situation, we want to create views that will handle the following: 

Create, Read, Update and Delete Dependent Details i.e POST, GET, PUT, DELETE Request 

A. Create/ Add Details: This test case is for creating/adding details in the database. 

APIClient() It supports the request interface as Django Client class provides.

Reverse This import from django.urls, it takes an input as a url name and returns the acute url, which reverses the having a url first and then gives it a name.

Here we add new dependent details and return if not equal values.

B. Get Details: This test case for getting Details.

Here we get the dependent details using GET Request where we get details from the primary key of specific dependent details.

C. Update Details: This test case for update details.

Here we update the details with PUT Request, where we pass the primary key for update details of a particular dependent_id and return if the status code is equal.

D. Delete Details: This test case for deleting dependent details.

 

Here we delete the specific dependent details bypassing the primary key to delete details and return the response.


I
mportance of Test Case

The TestCase class provides us with several useful methods for testing code and helps to improve good code quality. 

The process of writing test cases that satisfy all the requirements by defining test cases in different ways like unit testing, integration testing, feature testing and many more etc 

Nowadays increasing use of multiple applications with different languages, Automation testing also integrates with manual testing, likewise, the popularity of testing frameworks is increasing as well. Some of the popular testing frameworks in various languages are 

Python – Robot, Pytest, Nose2, Behave, Testify etc. 

Java –  JBehave, JUnit, Serenity, TestNG, Selenide etc.

Related read: Test Cases in Django and Django Multi-Tenant

coma

Conclusion 

TDD is an important part of our software development, the basic idea of a test case in our application to measure functionality across a set of actions or programs or functions.  Also, it is a document with a set of test data preconditions and expected results to develop a particular senior and verify against that compliance. So make sure yourself before going for actual development I need to write a test case. 

Keep Reading

Struggling with EHR integration? Learn about next-gen solutions in our upcoming webinar on Mar 6, at 11 AM EST.

Register Now

Let's create something together!