0
0
PyTesttesting~8 mins

Running tests (pytest command) - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Running tests (pytest command)
Folder Structure
tests/
├── test_example.py
├── test_login.py
├── test_checkout.py
conftest.py
pytest.ini
requirements.txt

This is a simple pytest project structure. All test files are inside the tests/ folder. The conftest.py file holds shared fixtures and hooks. The pytest.ini file contains pytest configuration.

Test Framework Layers
  • Test files: Contain test functions or classes starting with test_. These are the actual tests pytest runs.
  • Fixtures (conftest.py): Setup and teardown code shared across tests, like preparing test data or opening a browser.
  • Helpers/Utilities: Optional Python modules with reusable functions to keep tests clean.
  • Configuration: Files like pytest.ini to customize pytest behavior.
Configuration Patterns
  • pytest.ini file to set default options, e.g., test paths, markers, or verbosity.
  • Use environment variables or pytest command-line options to select environments or browsers.
  • conftest.py fixtures can read config values and provide them to tests.
  • Example pytest.ini content:
    [pytest]
    addopts = -v --maxfail=2
    markers =
        smoke: quick smoke tests
        regression: full regression suite
    
Test Reporting and CI/CD Integration
  • Pytest outputs test results in the console by default (pass/fail with details).
  • Use plugins like pytest-html to generate HTML reports for easy reading.
  • Integrate pytest commands in CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Example command to run tests and generate HTML report:
    pytest tests/ --html=report.html --self-contained-html
Best Practices
  • Keep test names descriptive and start with test_ so pytest can find them easily.
  • Use pytest.ini to avoid repeating command-line options every time.
  • Run tests often during development to catch issues early.
  • Use markers to group tests and run subsets (e.g., smoke tests) quickly.
  • Keep tests independent so they can run in any order without failing.
Self Check

Where in this framework structure would you add a new fixture to open a browser before tests?

Key Result
Use pytest command to run tests organized in the tests/ folder with configuration in pytest.ini and shared fixtures in conftest.py.