pytest-skill: Enabling AI Assistants to Write pytest Following Production Practices

Introduction

In Python backend and AI engineering, pytest is almost the default unit testing stack. When writing tests in practice, common issues are rarely about “not knowing how to import pytest,” but rather involve fixture lifecycles, conftest.py sharing, the usage of parametrize and markers, and knowing exactly which path to patch when mocking. If you leave these tasks to general conversations, assistants tend to write code that runs but doesn’t align with team conventions: abusing unittest-style assertions, relying on global state, or lacking proper teardown.

The following introduces the pytest-skill provided by TestMu AI (formerly LambdaTest) in the agent-skills repository: a Skill designed for AI coding assistants to generate pytest tests featuring fixtures, parametrize, markers, mocking, and conftest patterns. It aligns with the mainstream testing stacks of Python backend and AI engineering, covering a broad developer base among similar Agent Skills.

What is this

pytest-skill is an Agent Skill categorized under unit-testing for the Python language, licensed under MIT, with metadata identifying the author as TestMu AI and the version as 1.0.

The official description summarizes it as: generating production-grade pytest tests covering fixtures, parametrize, markers, mocking, and conftest patterns. It is recommended for use when users mention pytest, conftest, @pytest.fixture, @pytest.mark, Python test, etc. Trigger words include: pytest, conftest, Python test, parametrize, Python unit test.

The Skill itself is located at the repository path pytest-skill; deeper explanations of production patterns are found in reference/playbook.md within the same directory (the material lists chapters on configuration, fixtures, parametrize, mocking, async, exceptions, markers, CI/CD, troubleshooting, and checklists).

Core Features

The Skill documentation organizes pytest writing styles into directly referenceable patterns, complete with anti-pattern comparisons and command quick-references.

Basic Testing and Assertions

It supports functional testing, class-based grouped testing, and exception validation with pytest.raises. The assertion section emphasizes native assert (combined with pytest assertion rewriting), pytest.approx for floating-point comparisons, and match and exc_info within pytest.raises.

Fixtures and conftest

Documentation examples include: ordinary fixtures, teardown after yield (database rollback/close), scope="module", and using autouse=True in conftest.py for shared resets. The anti-pattern table explicitly advises: do not use self.assertEqual(), use assert instead; do not perform setup in __init__, use @pytest.fixture; avoid bare global state, use fixtures with yield for cleanup.

Parametrize and Markers

@pytest.mark.parametrize is used for multiple sets of input/output; marker examples include common ones like slow, skip, skipif, and xfail.

Mocking

Examples cover patch/MagicMock from unittest.mock, as well as mocker.patch from pytest-mock, and mocking HTTP responses.

Project Configuration and Execution

The pyproject.toml snippet demonstrates [tool.pytest.ini_options]: testpaths, markers registration, addopts, etc.

Quick Reference summarizes common commands, for example:

Task Command
Run all pytest
Specific file pytest tests/test_login.py
Specific case pytest tests/test_login.py::test_login_success
By marker pytest -m slow
By keyword pytest -k "login and not invalid"
Coverage pytest --cov=myapp --cov-report=html
Parallel pytest -n auto (requires pytest-xdist)

reference/playbook.md further covers pytest.ini / pyproject configuration, async (pytest-asyncio), CI (e.g., GitHub Actions matrix), and checklists for debugging and production. It is suitable for when you need “a complete set of engineering habits” rather than just single-file examples.

Installation and Activation

pytest-skill is released with LambdaTest/agent-skills. The parent repository instructions state: run the installer npx agentskillsforall via Node.js / npm, combined with TestMu AI account credentials (LT_USERNAME, LT_ACCESS_KEY environment variables). See TestMu AI Agent Skills Documentation for complete steps.

The example for installing a single Skill is as follows (the parent repository README uses selenium, playwright, and cypress as examples; pytest-skill uses the same mechanism, replacing --skill with pytest-skill):

npx agentskillsforall add https://github.com/LambdaTest/agent-skills.git --skill pytest-skill

Browse all Skills in the repository:

npx agentskillsforall list https://github.com/LambdaTest/agent-skills.git

Credential example (Linux / macOS):

export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR_ACCESS_KEY"

The parent repository states support for AI coding assistants such as Claude Code, GitHub Copilot, Cursor, and Gemini CLI; mounting methods for Skill directories may vary across IDEs/CLIs, so refer to the official TestMu AI documentation. Once enabled, naturally mentioning pytest, conftest, parametrize, etc., in conversations will align with the Skill’s trigger conditions.

Typical Usage

After installation and credential configuration, you can describe testing requirements directly in natural language. For example: supplementing unit tests with fixtures for a Python module, writing cases for an API layer that mock out requests.get, or adding an autouse state reset in conftest.py. The patterns embedded in the Skill will constrain the generated results to be closer to the “Good” column in the documentation.

If you only need to manually write or review AI output against patterns locally, you can also open the example snippets in the Skill repository directly. For instance, parametrize:

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

Fixture and teardown:

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

Project-level pytest configuration:

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

On the execution side, you still use the pytest installed on your local machine; refer to the Quick Reference above for parallel and coverage commands.

Applicable Scenarios and Notes

Who is it for: Teams or individuals maintaining Python services, libraries, or AI application code who want the tests generated by assistants to adhere to pytest conventions by default (fixture scope, conftest, marker registration, mock paths).

Limitations and Notes:

  1. The Skill guides the form and patterns of test code; it does not replace the installation of pytest and plugins (such as pytest-mock, pytest-xdist, pytest-asyncio) in your repository.
  2. The installation process via agentskillsforall is tied to a TestMu AI account and belongs to the TestMu AI Agent Skills ecosystem; if you only need static reference, you can read SKILL.md and reference/playbook.md directly on GitHub.
  3. The module paths in mock examples (e.g., myapp.email.smtplib) must be consistent with your project’s package structure; adjustments may be needed based on actual import paths after generation.
  4. The materials do not separately list a binding between pytest-skill and cloud testing execution capabilities; for E2E or cloud execution of other frameworks, the parent repository has separate selenium-skill, playwright-skill, etc. Do not confuse them with the unit testing Skill.

Conclusion

pytest-skill consolidates core pytest writing styles, anti-patterns, command quick-references, and deeper playbooks into a single Agent Skill, providing Cursor and other assistants with a unified reference when “writing Python tests.” If you are already using pytest as a quality gate for backend or AI engineering, you can treat it as a default constraint for generating and reviewing test code.

Official Directory: https://github.com/LambdaTest/agent-skills/tree/main/pytest-skill

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye