DEV Community

Neo Developer
Neo Developer

Posted on

Python Code Testing Frameworks to Choose From

Something to learn while writing quality code, as there are levels of development and best practices. The selection of tools and techniques is just as important.

Testing frameworks based on needs or requirements:

Doctest

  • A simple testing framework
  • Write test cases within function docstrings
  • Automatically locates the test cases within the docstrings
  • Good for documentation and keeping code up to date

Example:

def add(a, b):
    """
    Add two numbers
    >>> add(2, 3)
    5
    """
    return a + b

if __name__=="__main__":
    import doctest
    doctest.testmod()
    print(add(2, 3))
Enter fullscreen mode Exit fullscreen mode

Unittest

  • A Python built-in library
  • Write class and method-based test cases
  • Separate code and test cases
  • Test case names should start with 'test_'

Example:

import unittest
from main import add

class TestAdd(unittest.TestCase):
    def test_add(self):
        self.assertEqual(add(2, 3), 5)
        self.assertEqual(add(-2, -3), -5)
        self.assertEqual(add(-2, 3), 1)
        self.assertEqual(add(2, -3), -1)

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

Pytest

  • An external Python library
  • No need to write class-based test cases
  • Less verbose compared to unittest
  • More descriptive and colorful outputs
  • Supports code coverage

Example:

from main import add

def test_add():
    assert add(2, 3) == 5
    assert add(2, -3) == -1
    assert add(-2, 3) == 1
    assert add(-2, -3) == -5
Enter fullscreen mode Exit fullscreen mode

Finally, let's also consider cases where test cases require specific setup to keep the tests consistent.

Unittest provides setUp() and tearDown() functionality, which runs before and after every test execution.

Pytest provides the @pytest.fixture decorator, which runs before and after every test execution.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay