One of the best practices in software development is unit testing, which should be carried out from the beginning of the development process all the way through. When bugs are rarer and less expensive to fix early in the application's development, unit tests are designed to find them.
This answer will go over the fundamentals of writing and running a straightforward test using unittest
and unit testing libraries in Python such as PyUnit
and PyTest
.
The idea of testing is straightforward and clear. We write tests in parallel to ensure our code operates as we anticipate. Everyone tests their code in some capacity, but there are better and worse approaches to testing.
Most users will use a muddled combination of assert
statements and print
statements to execute short tests in the terminal.
A unit test is a test that verifies the functionality of a single line of code, typically modularized as a function.
Regression testing relies heavily on unit tests to guarantee that the code is stable and continues to operate as expected after modifications are made. After making changes to our code, we may run the unit tests we wrote to ensure that our modifications did not affect the functionality of other areas of the codebase.
unittest
moduleThe unittest
module is part of Python's standard library. We may detect faults in our programs and avoid regressions as we alter our code over time by writing tests using the unittest
module.
TestCase
is the name of one of the most significant classes offered by the unittest
module.
import unittestdef add_numbers(numbers_list):if len(numbers_list) > 10:raise ValueError("10 numbers can only be added to the list")return {"number": numbers_list}class TestAddNumbersToList(unittest.TestCase):def test_add_number_to_list(self):ac = add_numbers(numbers_list=["One", "Two"])ex = {"number": ["Three", "Four", "Five"]}self.assertEqual(ac, ex)# run the testunittest.main()
PyUnit
libraryPyUnit
, the built-in unit testing framework for python, is the equivalent of the Java-based JUnit
testing framework in terms of the language.
The unittest.TestCase
class is where PyUnit
's unit tests are arranged. By overriding the runTest()
method, we may run our unique unit tests that leverage the unittest.numerous
TestCase
's assert functions to verify conditions. To use PyUnit
, we must first import the unittest
library.
import unittestclass Rectangle:def __init__(self, width, height):self.width = widthself.height = heightdef get_rectangle_area(self):return self.width * self.heightdef set_rectangle_height(self, height):self.height = heightdef set_rectangle_width(self, width):self.width = widthclass TestRectangleArea(unittest.TestCase):def test_rectangle(self):rectangle = Rectangle(3, 4)self.assertEqual(rectangle.get_rectangle_area(), 8, "Area is incorrect")# run the testunittest.main()
PyTest
libraryA replacement for the integrated unittest
module is PyTest
. It must first be installed before we can use it.
pip install pytest
Simply prefix the functions with "test" to write tests, and PyTest
's test discovery mechanism will be able to find our tests.
class Square:def __init__(self, width, height):self.width = widthself.height = heightdef get_square_area(self):return self.width * self.heightdef set_square_height(self, height):self.height = heightdef set_square_width(self, width):self.width = width#test function with PyTestdef test_square():square = Square(4, 4)assert square.get_square_area() == 8, "incorrect area"
As opposed to PyUnit
, which has its own set of assert
functions, PyTest
makes use of the built-in assert
keyword in python. This may make things a little more convenient as we won't have to look up all of the different assert
procedures.
Begin with basic intuitive tests, then progress.
Make sure to give tests descriptive names and to use the word "test" throughout.
Every test needs to be independent.
Do not use Assert.IsTrue
.
Through the use of executable code examples, we learned what unit testing is, and how to carry it out using the two well-known Python libraries PyUnit
and PyTest
.
Free Resources