Django unit testing - Part (1/2): Testing and Coverage concepts
Introduction
"Software testing is the process of evaluating and verifying that a software product or application does what it is supposed to do"
This is the first thing you will see when you search for software testing in Google, and as clear as that is, some people took that mission on their shoulders and hence the software testing branch started.
In this article, we will focus mainly on the part related to the developers which is (unit testing)
Testing types in software
| Source: https://www.javatpoint.com/types-of-software-testing |
In the classification of the testing, we have two main branches:
I- Automation Testing
This is not our scope here at all, but it is meant to run test cases via script or testing tools
II- Manual Testing
As plain as it is, to run test cases manually. we have here three divisions as per the application code
A- White Box
To review the code itself and test it
B- Gray Box
A mixture of white and black box
C- Black Box
To treat the application as a black box, we know nothing about what happens inside it. and that's the important part, it is also divided into two main parts
1- Non-Functional Testing
Mainly focuses on the properties that are not critical to functionality. However, they are still important for the user experience like performance, compatibility etc.
2- Functional Testing
on the requirements set, and validating whether the application is achieving them or not
There are many types inside it like integration testing, system testing and the most important for this article, unit testing
Unit testing
What is unit testing?
Unit testing in short is testing each unit or an individual component. This shall be done by the developer, so it is the first level of functional testing.
This part aims at validating unit components with their performance, so we are testing the correctness of the isolated code, testing each part individually.
Why unit testing?
Understand the base of the code
Helps in documentation
Helps in fixing defects very early in the development phase
Helps with code re-usability by migrating code and test cases
How to achieve the best results of unit testing?
Test cases must be independent: The test case shall not be dependent on any external factor like the database contents, as they shall test the isolated code as explained earlier
Naming conventions for unit test cases must be clear and consistent: For readability purpose, and to make it easier for any other developer to understand, modify or add new test cases
The identified bugs must be fixed before jumping on next phase: To avoid any legacy code issues, and to keep your code clean of bugs and issues you have to do that. This also helps in avoiding any dependent bugs/ issues
Only one code should be tested at one time: To achieve the concept of (unit) testing, we shall test specific code with a specific unit test case
Adopt test cases with the writing of the code: To keep track of your code and always have an eye on the features, and the possible bugs that may occur. Also, keep the test cases updated for any upcoming features. The last thing is to avoid keeping huge legacy code without any test cases
Ensure the corresponding unit test is available or not for that module if changes: Always have another look at the test cases whenever you make any change to the code, to keep your test cases updated, so that you can update, remove or add the test case as per the change you have done
Coverage
Coverage is a metric in software testing, that measures the amount of code covered by a set of test cases. It is a very important metric and is used widely to check the quality of the test cases, and also mainly used inside the sonarqube to make sure a minimum percentage of the code is covered by test cases as we will explain in the next article.
Conclusion
As we have talked much about the testing and the unit testing, in the next article, we will focus more on how to implement these topics in Django.
Subscribe to my newsletter
Read articles from Mohamed Elzomor directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mohamed Elzomor
Mohamed Elzomor
Software engineer, backend developer Been working as a software engineer for 3 years and focusing mainly enhancements