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.

Top comments (0)