Skip to content

Running unittest Tests with pytest

pytest is compatible with Python’s built-in unittest framework and can discover and run most unittest-based tests without modification. This makes pytest a practical choice for projects migrating gradually from unittest to pytest.

python
import unittest

class TestMath(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(1 + 1, 2)

pytest will collect and run this test alongside native pytest-style tests. Over time, teams can adopt pytest features such as fixtures and parametrization while still supporting existing unittest test suites.

WARNING

pytest can execute unittest-based tests, but its session lifecycle (especially session-scoped fixtures and startup hooks) can interfere with unittest tests. In mixed test suites, it may be necessary to run unittest tests separately to preserve isolation.