skillfed

Pytest

Master pytest for Python testing with guidance on fixtures, parametrization, markers, and mocking strategies. Learn test organization patterns, coverage best practices, and common commands to streamline your testing workflow.

Pytest helps you run and execute Python unit tests with fixtures, parametrization, mocking, and coverage tracking.

AI-generated summary based on this skill's SKILL.md

6 1 unlicensed — metadata only updated by eyadsibai

Install

eyadsibai/ltk/pytest · repository language: Python

git clone https://github.com/eyadsibai/ltk
cp -r ltk ~/.claude/skills/pytest

generated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub

npx skillfed install eyadsibai/ltk/pytest

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How do I use pytest to run and execute Python unit tests?

Pytest runs tests by discovering test files (test_*.py or *_test.py) and test functions (test_*) in your project. Execute tests with `pytest` from your project root, or target specific files: `pytest test_module.py`. Pytest automatically collects and runs all matching tests, displaying results with pass/fail status and detailed failure information.

What are pytest fixtures and how do I use them?

Pytest fixtures are reusable test components that set up preconditions and clean up after tests. Define fixtures with the `@pytest.fixture` decorator, then pass them as function arguments to your test functions. Fixtures support setup/teardown logic, parametrization, and scoping (function, class, module, session) to control their lifecycle and reuse across multiple tests.

How can I parametrize tests to run multiple test cases?

Pytest parametrize uses the `@pytest.mark.parametrize` decorator to run the same test with different input values. Specify parameter names and a list of value tuples: `@pytest.mark.parametrize('input,expected', [(1, 2), (3, 4)])`. This generates separate test runs for each parameter set, reducing code duplication and improving test coverage.

What pytest command line options are most useful?

Pytest command line options include: `-v` for verbose output, `-s` to show print statements, `-k` to filter tests by name, `--tb=short` for concise tracebacks, `--lf` to run last failed tests, `-x` to stop on first failure, and `--cov` for coverage reports. Combine options for flexible test execution: `pytest -v -s --tb=short test_file.py`.

How do I generate test coverage reports with pytest?

Pytest generates coverage reports using the pytest-cov plugin. Install with `pip install pytest-cov`, then run `pytest --cov=module_name` to measure code coverage. Add `--cov-report=html` to generate an HTML report, or `--cov-report=term-missing` to display uncovered lines in terminal output. Coverage helps identify untested code paths.

What's the difference between pytest and unittest?

Pytest offers simpler syntax (plain functions vs. class-based tests), powerful fixtures (vs. setUp/tearDown), parametrization, and better assertion introspection. Pytest requires less boilerplate and provides richer plugin support. Unittest is Python's standard library framework with more verbose structure. Pytest is generally preferred for modern Python projects due to its flexibility and ease of use.

Related skills

Tags

test-automation python-testing quality-assurance continuous-integration code-validation developer-tools test-framework debugging-support