0
0
PyTesttesting~8 mins

@pytest.mark.skip with reason - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @pytest.mark.skip with reason
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   ├── test_login.py
│   └── __init__.py
├── src/
│   └── app_code.py
├── conftest.py
├── pytest.ini
└── requirements.txt
    

This is a typical Pytest project structure. Tests live in the tests/ folder. Configuration files like pytest.ini and fixtures in conftest.py help manage test behavior.

Test Framework Layers
  • Tests: Test functions or classes inside tests/ folder. Use @pytest.mark.skip(reason="...") to skip tests with a clear reason.
  • Fixtures: Setup and teardown code in conftest.py to prepare test data or environment.
  • Application Code: The actual code under test in src/.
  • Configuration: pytest.ini to set global options like markers and test paths.
  • Utilities: Helper functions or modules to support tests.
Configuration Patterns

Use pytest.ini to register custom markers and configure test runs:

[pytest]
markers =
    skip: skip test with a reason

addopts = -ra -q
    

Use conftest.py for fixtures that manage environment setup, credentials, or browser options if needed.

Test Reporting and CI/CD Integration

Pytest outputs test results with clear pass/fail/skip status. Skipped tests show the reason in the report.

Integrate with CI/CD tools (GitHub Actions, Jenkins) by running pytest commands in pipelines. Use plugins like pytest-html for detailed HTML reports.

# Example GitHub Actions step
- name: Run tests
  run: pytest --html=report.html
    
Best Practices
  • Always provide a clear reason when skipping tests to explain why.
  • Use @pytest.mark.skip for tests that should not run temporarily or under certain conditions.
  • Prefer @pytest.mark.skipif if skipping depends on a condition (like OS or environment).
  • Keep skipped tests visible in reports to track what is not tested.
  • Use pytest.ini to register custom markers to avoid warnings.
Self Check

Where in this framework structure would you add a new test that you want to skip with a reason?

Key Result
Use @pytest.mark.skip(reason) in test files under tests/ to skip tests with clear reasons.