DEV Community

Nitinn S Kulkarni
Nitinn S Kulkarni

Posted on

2

Python Testing – Unit Tests, Pytest, and Best Practices

Writing tests ensures that your code works as expected and helps prevent bugs from creeping into your applications. Python provides several testing frameworks to write, automate, and manage tests efficiently.

In this post, we’ll cover:

✅ Why testing is important
✅ Writing basic unit tests using unittest
✅ Using pytest for simple and powerful testing
✅ Best practices for writing effective tests

Let’s dive in! 🚀

1️⃣ Why is Testing Important?

✅ Helps catch bugs early before deployment.
✅ Ensures code reliability and prevents regressions.
✅ Saves debugging time and makes refactoring safer.
✅ Encourages better code design.

There are different types of tests:

Test Type Purpose
Unit Tests Test individual functions or methods.
Integration Tests Test how different parts of the system work together.
Functional Tests Test overall application behavior from a user perspective.
Regression Tests Ensure new changes don’t break existing functionality.

2️⃣ Writing Unit Tests Using unittest (Built-in Framework)

Python comes with a built-in testing framework called unittest.

✅ Basic unittest Example


import unittest

def add(a, b):
    return a + b

class TestMathOperations(unittest.TestCase):

    def test_add(self):
        self.assertEqual(add(2, 3), 5)  # Passes
        self.assertEqual(add(-1, 1), 0)  # Passes

if __name__ == "__main__":
    unittest.main()

Enter fullscreen mode Exit fullscreen mode

🔥 Key Features of unittest:

✅ Uses assertEqual(), assertTrue(), and other assertion methods.
✅ Runs tests using unittest.main().
✅ Automatically detects and runs test cases in a module.

3️⃣ Using pytest for Simpler and More Powerful Testing

pytest is a popular testing framework that is easier to use than unittest.

✅ Installing pytest


pip install pytest

Enter fullscreen mode Exit fullscreen mode

✅ Writing a Simple pytest Test

Create a file called test_math.py:


import pytest

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5  # Test passes
    assert add(-1, 1) == 0  # Test passes

Enter fullscreen mode Exit fullscreen mode

✅ Running Tests with pytest

Run tests using:


pytest test_math.py

Enter fullscreen mode Exit fullscreen mode

🔥 Why Use pytest Over unittest?

✅ No need to use classes – Simple functions work.
✅ Better error messages – Shows where tests failed.
✅ Supports fixtures and parameterized tests for better organization.

4️⃣ Advanced pytest Features

✅ Using Fixtures to Set Up Test Data

Fixtures allow preparing test data before running tests.


import pytest

@pytest.fixture
def sample_data():
    return {"name": "Alice", "age": 30}

def test_person_data(sample_data):
    assert sample_data["name"] == "Alice"
    assert sample_data["age"] == 30

Enter fullscreen mode Exit fullscreen mode

🔹 Fixtures run automatically before each test.

✅ Parameterized Tests in pytest (Testing Multiple Cases)


import pytest

@pytest.mark.parametrize("a, b, expected", [
    (2, 3, 5),
    (-1, 1, 0),
    (0, 0, 0)
])
def test_add(a, b, expected):
    assert add(a, b) == expected

Enter fullscreen mode Exit fullscreen mode

✅ Why Use Parameterized Tests?

Avoids duplicating similar test cases.
Runs tests efficiently with different input values.
Enter fullscreen mode Exit fullscreen mode

5️⃣ Running and Managing Tests Efficiently

✅ Run All Tests in a Directory


pytest

Enter fullscreen mode Exit fullscreen mode

✅ Run a Specific Test File


pytest test_math.py

Enter fullscreen mode Exit fullscreen mode

✅ Show Detailed Output


pytest -v

Enter fullscreen mode Exit fullscreen mode

✅ Stop Execution on First Failure


pytest -x

Enter fullscreen mode Exit fullscreen mode

6️⃣ Mocking: Testing Code That Depends on External Services

Sometimes, functions depend on APIs, databases, or system resources.

We can mock these dependencies using unittest.mock.

✅ Mocking an API Call in Python


from unittest.mock import patch
import requests

def fetch_data(url):
    response = requests.get(url)
    return response.json()

@patch("requests.get")
def test_fetch_data(mock_get):
    mock_get.return_value.json.return_value = {"status": "success"}
    assert fetch_data("https://api.example.com") == {"status": "success"}

Enter fullscreen mode Exit fullscreen mode

✅ Why Use Mocks?

Prevents slow API calls in tests.
Ensures tests don’t depend on external systems.
Helps simulate different responses.
Enter fullscreen mode Exit fullscreen mode




7️⃣ Best Practices for Writing Effective Tests

✅ Follow the AAA Pattern → Arrange, Act, Assert.
✅ Write separate test functions instead of testing everything in one function.
✅ Use meaningful test names (test_addition_returns_correct_value).
✅ Keep test cases independent – One test should not rely on another.
✅ Run tests frequently to catch bugs early.
✅ Use pytest for simple and powerful test management.

🔹 Conclusion

✅ Unit tests ensure correctness and prevent regressions.
✅ unittest is built-in but pytest makes testing easier.
✅ Fixtures and parameterized tests improve test coverage.
✅ Mocking allows testing code with external dependencies.

By mastering testing, you’ll write better, bug-free Python applications. 🚀

What’s Next?

In the next post, we’ll explore Python Performance Optimization – Profiling, Caching, and Code Efficiency. Stay tuned! 🔥

💬 What Do You Think?

Do you use pytest for testing? How do you handle test failures? Let’s discuss in the comments! 💡

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Jetbrains Survey

Calling all developers!

Participate in the Developer Ecosystem Survey 2025 and get the chance to win a MacBook Pro, an iPhone 16, or other exciting prizes. Contribute to our research on the development landscape.

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️