Pytest Testing
Master pytest through test-driven development, fixtures, and mocking strategies. Build comprehensive test suites with parametrized tests, external dependency mocking, and CI/CD pipeline integration while measuring code coverage.
Pytest Testing teaches you to run and execute pytest tests for Python projects using fixtures, mocking, and coverage measurement.
AI-generated summary based on this skill's SKILL.md
Install
pluginagentmarketplace/custom-plugin-python/pytest-testing · repository language: Python
git clone https://github.com/pluginagentmarketplace/custom-plugin-python
cp -r custom-plugin-python ~/.claude/skills/pytest-testinggenerated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub
npx skillfed install pluginagentmarketplace/custom-plugin-python/pytest-testingFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to use pytest for Python testing?
Pytest Testing is a powerful framework for running and executing tests in Python projects. To use pytest, install it via pip, write test functions prefixed with `test_`, and run `pytest` from your command line. Pytest automatically discovers tests in files matching `test_*.py` or `*_test.py` patterns. You can run specific tests, use markers to organize tests, and leverage fixtures for setup and teardown. The framework supports assertions, parametrization for testing multiple inputs, and integration with CI/CD pipelines for automated quality checks.
What are pytest fixtures and how do they work?
Pytest Testing fixtures are reusable functions that provide setup and teardown for your tests. Define fixtures using the `@pytest.fixture` decorator; they can return data, configure resources, or perform cleanup. Fixtures are passed as arguments to test functions by name, enabling dependency injection and reducing code duplication. Common use cases include database connections, mock objects, temporary files, and API clients. Pytest's fixture system supports scopes (function, class, module, session) to control how long resources persist, making tests cleaner and more maintainable.
Pytest configuration setup and test discovery explained?
Pytest Testing uses `pytest.ini`, `setup.cfg`, or `pyproject.toml` files to configure behavior like test paths, markers, and plugins. Test discovery automatically finds tests in directories matching patterns like `test_*.py` or `*_test.py`, and within test classes named `Test*`. You can customize discovery with the `testpaths` option, exclude directories, and set minimum Python versions. Configuration also controls output verbosity, parallel execution, coverage thresholds, and fixture availability. Proper setup ensures consistent test execution across development and CI/CD environments.
How does pytest parametrize tests with multiple inputs?
Pytest Testing's `@pytest.mark.parametrize` decorator lets you run the same test with different input values, eliminating code duplication. Specify parameter names and a list of values; pytest generates separate test cases for each combination. For example, parametrizing a validation function tests multiple valid and invalid inputs in one concise test. You can stack multiple parametrize decorators for matrix testing. This approach improves test coverage, makes tests more readable, and simplifies maintenance when adding new test cases.
How can I generate test coverage and quality reports with pytest?
Pytest Testing integrates with coverage tools to measure code coverage. Install `pytest-cov`, then run `pytest --cov=your_module` to generate coverage reports showing which lines were executed. Generate HTML reports with `--cov-report=html` for visual inspection. Pytest also supports plugins like `pytest-html` for detailed test reports and `pytest-xdist` for parallel execution. These reports help identify untested code paths, track quality metrics over time, and enforce coverage thresholds in CI/CD pipelines.
What techniques help debug and troubleshoot failing pytest tests?
Pytest Testing offers several debugging strategies: use `pytest -v` for verbose output showing test names and results, `pytest -s` to capture print statements, and `pytest --tb=short` for concise tracebacks. The `-x` flag stops after the first failure, `-lf` reruns last failures, and `--pdb` drops into the debugger on failure. Add `pytest.set_trace()` in code for breakpoints. Use markers like `@pytest.mark.skip` or `@pytest.mark.xfail` to manage problematic tests. Fixtures with mocking isolate dependencies, making failures easier to diagnose.