DEV Community

Cover image for Python Unit Testing
Mohamad Ashraful Islam
Mohamad Ashraful Islam

Posted on • Edited on

Python Unit Testing

Originally Posted on Ashraful's Blog

What is Testing❓

Testing is basically checking the features are okay or not and finding bugs on the system. Basically, there are many types of testing we do with software. Today we will talk about the most famous unit testing process. Let's keep going.

What is a unit testing❓

A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or property.

Python's builtin unittest 💥

Let's try some functions and their unit tests

# test_add.py
# A very basic function for adding two numbers
def add(a: int, b: int) -> int:
    return a + b

# Writing Unit Test
import unittest

class TryingTheAwesomeUnitTest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(add(5, 7), 12)

if __name__ == '__main__':
    unittest.main()
Enter fullscreen mode Exit fullscreen mode

Save the file as test_add.py and run the file python3 test_add.py and see the following output.

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
Enter fullscreen mode Exit fullscreen mode

Introduction to PyTest 🚀

pytest: helps you write better programs

Pytest is another testing library for Python. Let's dig into it.

Installation💡

pip install pytest
Enter fullscreen mode Exit fullscreen mode
# test_2_add.py

# The same old function
def add(a: int, b: int) -> int:
    return a + b

def test_add():
    assert add(3, 5) == 9 # I want see the fail response
Enter fullscreen mode Exit fullscreen mode

Run the tests🐛

Just type pytest on the directory where you have saved the file.

$ pytest
Enter fullscreen mode Exit fullscreen mode

Result🙈

============================================ test session starts =============================================
platform darwin -- Python 3.8.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/ashraful/Public/scripts
collected 1 item

test_2_add.py F                                                                                        [100%]

================================================== FAILURES ==================================================
__________________________________________________ test_add __________________________________________________

    def test_add():
>       assert add(3, 5) == 9 # I want see the fail response
E       assert 8 == 9
E        +  where 8 = add(3, 5)

test_2_add.py:8: AssertionError
========================================== short test summary info ===========================================
FAILED test_2_add.py::test_add - assert 8 == 9
============================================= 1 failed in 0.04s ==============================================
Enter fullscreen mode Exit fullscreen mode

Tips: Don't forget to put the filename test_ as prefix otherwise pytest can't detect the file. Whatever test_ as prefix is mandatory convention for test case.

Top comments (0)