Pytest Skill

Generates production-grade pytest tests in Python with fixtures, parametrize, markers, mocking, and conftest patterns. Use when user mentions "pytest", "conftest", "@pytest.fixture", "@pytest.mark", "Python test". Triggers on: "pytest", "conftest", "Python test", "parametrize", "Python unit test".

Published by @LambdaTest·from LambdaTest/agent-skills·0 agent reads / 30d·0 saves·

Pytest Testing Skill

Core Patterns

Basic Test

import pytest

def test_addition():
    assert 2 + 3 == 5

def test_exception():
    with pytest.raises(ValueError, match="invalid"):
        int("not_a_number")

class TestCalculator:
    def test_add(self):
        calc = Calculator()
        assert calc.add(2, 3) == 5

    def test_divide_by_zero(self):
        with pytest.raises(ZeroDivisionError):
            Calculator().divide(10, 0)

Fixtures

@pytest.fixture
def calculator():
    return Calculator()

@pytest.fixture
def db_connection():
    conn = Database.connect("test_db")
    yield conn  # teardown after yield
    conn.rollback()
    conn.close()

@pytest.fixture(scope="module")
def api_client():
    client = APIClient(base_url="http://localhost:8000")
    yield client
    client.logout()

# conftest.py - shared fixtures
@pytest.fixture(autouse=True)
def reset_state():
    State.reset()
    yield
    State.cleanup()

# Usage
def test_add(calculator):
    assert calculator.add(2, 3) == 5

Parametrize

@pytest.mark.parametrize("input,expected", [
    ("hello", 5), ("", 0), ("pytest", 6),
])
def test_string_length(input, expected):
    assert len(input) == expected

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

Markers

@pytest.mark.slow
def test_large_dataset(): ...

@pytest.mark.skip(reason="Not implemented")
def test_future_feature(): ...

@pytest.mark.skipif(sys.platform == "win32", reason="Unix only")
def test_unix_permissions(): ...

@pytest.mark.xfail(reason="Known bug #123")
def test_known_bug(): ...

Mocking

from unittest.mock import patch, MagicMock

def test_send_email(mocker):
    mock_smtp = mocker.patch("myapp.email.smtplib.SMTP")
    send_welcome_email("[email protected]")
    mock_smtp.return_value.sendmail.assert_called_once()

def test_api_call(mocker):
    mock_response = mocker.Mock()
    mock_response.status_code = 200
    mock_response.json.return_value = {"users": [{"name": "Alice"}]}
    mocker.patch("myapp.service.requests.get", return_value=mock_response)
    users = get_users()
    assert len(users) == 1

@patch("myapp.service.database")
def test_save_user(mock_db):
    mock_db.save.return_value = True
    assert save_user({"name": "Alice"}) is True
    mock_db.save.assert_called_once()

Assertions

assert x == y
assert x != y
assert x in collection
assert isinstance(obj, MyClass)
assert 0.1 + 0.2 == pytest.approx(0.3)

with pytest.raises(ValueError) as exc_info:
    raise ValueError("bad")
assert "bad" in str(exc_info.value)

Anti-Patterns

BadGoodWhy
self.assertEqual()assert x == ypytest rewrites give better output
Setup in __init__@pytest.fixtureLifecycle management
Global stateFixture with yieldProper cleanup
Huge test functionsSmall focused testsEasier debugging

Quick Reference

TaskCommand
Run allpytest
Run filepytest tests/test_login.py
Run specificpytest tests/test_login.py::test_login_success
By markerpytest -m slow
By keywordpytest -k "login and not invalid"
Verbosepytest -v
Stop first failpytest -x
Last failedpytest --lf
Coveragepytest --cov=myapp --cov-report=html
Parallelpytest -n auto (pytest-xdist)

pyproject.toml

[tool.pytest.ini_options]
testpaths = ["tests"]
markers = ["slow: slow tests", "integration: integration tests"]
addopts = "-v --tb=short"

Deep Patterns

For production-grade patterns, see reference/playbook.md:

SectionWhat's Inside
§1 Configpytest.ini + pyproject.toml with markers, coverage
§2 FixturesScoping, factories, teardown, autouse, tmp_path
§3 ParametrizeBasic, with IDs, cartesian, indirect
§4 Mockingpytest-mock, monkeypatch, spies, env vars
§5 Asyncpytest-asyncio, async fixtures, async client
§6 Exceptionspytest.raises(match=), warnings
§7 Markers & PluginsCustom markers, collection hooks
§8 Class-BasedNested classes, autouse setup
§9 CI/CDGitHub Actions matrix, coverage gates
§10 Debugging Table10 common problems with fixes
§11 Best Practices15-item production checklist

Bundled with this artifact

2 files

Reference files that ship alongside this artifact. Agents pull these in only when the task needs them.

More on the bench

SKILL0

Skill Creator

Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.

ai-prompt-engineering+1
6
SKILL0

Bash Pro

Master of defensive Bash scripting for production automation, CI/CD pipelines, and system utilities. Expert in safe, portable, and testable shell scripts.

ai-prompt-engineering+3
4
SKILL0

Documentation

Creates, structures, and reviews technical documentation following the Diátaxis framework (tutorials, how-to guides, reference, and explanation pages). Use when a user needs to write or reorganize docs, structure a tutorial vs. a how-to guide, build reference docs or API documentation, create explanation pages, choose between Diátaxis documentation types, or improve existing documentation structure. Trigger terms include: documentation structure, Diátaxis, tutorials vs how-to guides, organize docs, user guide, reference docs, technical writing.

software-engineering
4