🐍 Python Intermediate

What is pytest in Python?

Answer

pytest is the most popular Python testing framework. Tests are simple functions starting with test_, using plain assert statements: def test_add(): assert add(2, 3) == 5. Run: pytest (discovers tests automatically). Fixtures provide reusable setup/teardown: @pytest.fixture def db(): conn = create_connection(); yield conn; conn.close() — inject into tests by name. Fixture scopes: function, class, module, session. Parameterize: @pytest.mark.parametrize("x,y,result", [(1,2,3),(4,5,9)]). Marks: @pytest.mark.skip, @pytest.mark.xfail, custom marks. Plugins: pytest-mock (mocking), pytest-cov (coverage), pytest-asyncio (async tests), pytest-django. Pytest automatically provides detailed failure diffs and does not require subclassing TestCase.