Creating unit tests using pytest is easy as we only need to create functions for the test. We can create a simple test function in just two lines of code.
Look at the below example, in which we have created a square method and run a unit test using the test() function.
Save the below code in the file named 'test1.py'.
def square(x):
return x**2
def test():
assert square(3) == 8
As we can see the assert statement is logically wrong as the value of 32 should be 9. We will now run the test using the below command.
pytest test1.py
The output of the above is
============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 1 item
test1.py F [100%]
================================= FAILURES ==================================
_____________________________________ test _______________________________________
def test():
> assert square(3) == 8
E assert 9 == 8
E + where 9 = square(3)
test1.py:6: AssertionError
============================ short test summary info =============================
FAILED test1.py::test - assert 9 == 8
=============================== 1 failed in 0.04s ================================
Now, we will correct the assertion statement.
Save the below code in file 'test2.py'.
def square(x):
return x**2
def test():
assert square(3) == 9
Now, we will run the test on this file using the below command.
pytest test2.py
The output is as follows
============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 1 item
test2.py . [100%]
=============================== 1 passed in 0.00s ===============================
The output shows that the test has passed.
We can also write multiple tests together by using a class whose methods represent different tests.
Save the below code in a file named 'test3.py'.
def square(x):
return x**2
class Test:
def test_one(self):
assert square(2) == 4
def test_two(self):
assert square(3) == 8
def test_three(self):
assert square(4) == 16
Again, we will run the tests using the below command.
pytest test3.py
The output is as follows
============================== test session starts ===============================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: <file_path>
collected 3 items
test3.py .F. [100%]
================================= FAILURES ==================================
__________________________________ Test.test_two __________________________________
self = <test3.Test object at 0x7fe39eca2880>
def test_two(self):
> assert square(3) == 8
E assert 9 == 8
E + where 9 = square(3)
test3.py:10: AssertionError
============================ short test summary info =============================
FAILED test3.py::Test::test_two - assert 9 == 8
=========================== 1 failed, 2 passed in 0.05s ============================
The output shows which test cases passed and which failed, again with detailed info why they failed and how much time it took to run the tests.
In the similar way we can run any number of tests and check if the code we have is working as expected and if not what and where it is not showing expected results.
Comments