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.

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. 

Arti

Full-Stack Developer

I’m a Full-stack developer with over 2+ years of experience in developing web applications. I have good knowledge of Python Django and React. I have experience in creating REST APIs, developing web applications using python, and react. Always look forward to learning new stuff.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks