-
Notifications
You must be signed in to change notification settings - Fork 1
TDD Report
Iffat Ara Sanzida (IA)
Test Driven Development is the process in which test cases are written before the code that validates those cases. It depends on the repetition of a very short development cycle. Test Driven Development is a technique in which automated Unit tests are used to drive the design and free decoupling of dependencies. The steps are as follows:
- Add a test: Write a test case that describes the function completely. In order to make the test cases, the developer must understand the features and requirements using user stories and use cases.
- Run all the test cases and make sure that the new test case fails.
- Write the code that passes the test case.
- Run the test cases.
- Refactor code: This is done to remove duplication of code.
- Repeat the above-mentioned steps again and again.
Tools: Virtual Studio Code
Framework: Python-Django
Unit Testing Tool: Django’s built-in testing framework for unit testing.
For Generating Test Report: pip install coverage
-
Create a
tests.py
file to write test cases.
-
From
django.test
, import Django’s built-in unit testing moduleTestCase
. Also, importRequestFactory
andClient
for simulating various kinds of test cases. -
Import all the files and modules you want to perform unit testing on. In our project’s case, we have imported
User
, elements ofmodels.py
,views.py
, andservices.py
.
-
Now, write down the test cases. Follow the format mentioned in Unit Testing for test case writing. After adding test cases, to run the test cases of the functions in
views.py
, writepython manage.py test
in the terminal.
Feature: Review Add Product Request
We have added 7 test cases, but 3 of them failed.
![]() |
![]() |
---|---|
![]() |
![]() |
At the time of failure, the views functions for the 3 of them were as below:
The test_add_product_rqsts_with_no_products
was failing as there were no conditions in the views for add_product_rqsts
to handle the situation when there are no products for adding requests. After putting the condition, the test case ran successfully for that function.
We have checked if the function is now performing ok for the test case by the following command.
Also, we have modified the template code in approved_rqsts.html
and disapproved_rqsts.html
as there were some errors. After modifying the needed codes in views.py
file and template files, we have successfully passed all the test cases written in the tests.py
file.
Feature: Review Return Product Request
For TDD, we have written the test cases first for ReturnRequest feature.
After running the test cases, we can see that the test case fails.
At that time, the body of the return_requests function in views.py was not fully implemented. That’s why test case failed.
After modifying the body of the return_requests function in views.py according to the test functions in tests.py, the code looks as following:
After implementing the function body according to the test function, the test cases passes.
To measure the coverage and accuracy of the test cases upon the modules, write in the command line the following commands.
pip install coverage
coverage run manage.py test
coverage report
Basically, we have tested the modules in our project app and found the following coverage rate:
So, the total coverage is 90%.