Writing Your First Unit Test in Python (PyTest/Unittest)

IHUB Talent – Best Full Stack Software Testing Tools Training Course Institute in Hyderabad

In today’s technology-driven world, software quality is everything. Whether it's a mobile app, web application, or enterprise software, users expect a flawless experience. This is where software testing plays a critical role. For those who aspire to build a rewarding career in QA and testing, IHUB Talent is the best Full Stack Software Testing Tools Training Course Institute in Hyderabad. The institute offers a live intensive internship program conducted by industry experts, making it ideal for graduates, postgraduates, education gap individuals, and those seeking a career change.

Writing Your First Unit Test in Python (PyTest/Unittest)

Unit testing is a crucial part of the software development process. It ensures individual units of code work as intended, making your application more reliable and easier to maintain. Python offers two powerful testing frameworks: Unittest (built-in) and PyTest (third-party but widely used). In this blog post, we'll walk through writing your first unit test using both.

🔹 What is Unit Testing?

Unit testing involves writing small test cases to verify specific functions or methods in your code. Each test checks a "unit" of functionality in isolation from the rest of the system.

🔹 Example Code to Test

Let's consider a simple function in a file called math_utils.py:

python

# math_utils.py

def add(a, b):

    return a + b

🔸 Writing Unit Tests with unittest

Python's unittest module is inspired by Java’s JUnit. Here's how you can test the add() function:

python

# test_math_utils_unittest.py

import unittest

from math_utils import add

class TestMathUtils(unittest.TestCase):

    def test_add(self):

        self.assertEqual(add(2, 3), 5)

        self.assertEqual(add(-1, 1), 0)

        self.assertEqual(add(0, 0), 0)

if __name__ == '__main__':

    unittest.main()

To run the test, use:

bash

python test_math_utils_unittest.py

🔸 Writing Unit Tests with pytest

PyTest is simpler and more readable, especially for beginners.

python

# test_math_utils_pytest.py

from math_utils import add

def test_add():

    assert add(2, 3) == 5

    assert add(-1, 1) == 0

    assert add(0, 0) == 0

To run the test:

bash

pytest test_math_utils_pytest.py

🔹 Key Differences

Feature unittest pytest

Boilerplate More Less

Assertion Style self.assertEqual assert

Extensibility Moderate High (via plugins)

🔹 Final Thoughts

Whether you choose unittest or pytest, the goal is the same—write clean, reliable, and testable code. Start small, keep tests independent, and run them frequently. For beginners, PyTest’s simplicity makes it a great choice, while unittest remains valuable for those wanting a more structured, OOP-style approach.

Keywords: 

Python Unit Testing

PyTest tutorial

unittest example

write test cases Python

Python add function test

software testing basics  

Read More

Introduction to Unit Testing in Software Development

Why Every Developer Should Know Testing

The Pyramid of Testing: Unit, Integration, E2E

Testing Challenges in Full-Stack Development

Visit Our I-HUB Testing Training Institute Hyderabad

Comments