0
0
PyTesttesting~8 mins

Built-in markers (skip, skipif, xfail) in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Built-in markers (skip, skipif, xfail)
Folder Structure
pytest-project/
├── tests/
│   ├── test_example.py
│   └── test_feature.py
├── conftest.py
├── pytest.ini
└── utils/
    └── helpers.py
    

This is a simple pytest project structure. Tests live in the tests/ folder. Shared helpers go in utils/. The conftest.py file holds fixtures and hooks. The pytest.ini file configures pytest settings.

Test Framework Layers
  • Test Layer: Test files inside tests/ folder contain test functions using pytest markers like @pytest.mark.skip, @pytest.mark.skipif, and @pytest.mark.xfail.
  • Fixture Layer: conftest.py holds fixtures that prepare test data or environment.
  • Utility Layer: Helper functions and reusable code in utils/ support tests.
  • Configuration Layer: pytest.ini manages pytest options and marker registration.
Configuration Patterns

Use pytest.ini to register custom markers and set default options.

[pytest]
markers =
    skip: skip a test unconditionally
    skipif(condition): skip a test if condition is true
    xfail: mark a test as expected to fail

addopts = -ra -q
    

Use skipif with conditions like OS, Python version, or environment variables to skip tests dynamically.

Credentials and environment-specific data should be handled via environment variables or separate config files, not hardcoded.

Test Reporting and CI/CD Integration

Pytest outputs test results with clear markers for skipped and xfailed tests.

  • skip tests show as skipped in reports.
  • skipif tests show as skipped only if condition matches.
  • xfail tests show as expected failures; if they pass unexpectedly, pytest reports them as XPASS.

Integrate pytest with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) by running pytest commands and collecting reports.

Use plugins like pytest-html or pytest-junitxml to generate detailed HTML or XML reports for CI dashboards.

Best Practices
  • Use skip for tests that should never run temporarily, like incomplete features.
  • Use skipif for environment-dependent skips, e.g., skip Windows-only tests on Linux.
  • Use xfail to mark known bugs or flaky tests, so failures don't break the build.
  • Always document why a test is skipped or xfailed with a reason string.
  • Keep skip conditions simple and maintainable to avoid hiding real issues.
Self Check

Where in this pytest project structure would you add a new test that should be skipped on Python versions below 3.8?

Key Result
Use pytest built-in markers skip, skipif, and xfail to control test execution based on conditions and expected failures.