0
0
PyTesttesting~8 mins

@pytest.mark.parametrize decorator - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @pytest.mark.parametrize decorator
Folder Structure
tests/
├── test_example.py       # Test files using @pytest.mark.parametrize
├── conftest.py           # Fixtures and hooks
utilities/
├── helpers.py            # Helper functions
config/
├── config.yaml           # Environment and test settings
reports/
├── latest_report.html    # Test run reports
Test Framework Layers
  • Test Layer: Contains test files like test_example.py where @pytest.mark.parametrize is used to run the same test with different inputs.
  • Fixtures Layer: conftest.py holds setup and teardown code shared across tests.
  • Utilities Layer: Helper functions and reusable code to support tests.
  • Configuration Layer: Stores environment variables, credentials, and test parameters in config.yaml or similar files.
  • Reporting Layer: Generates test reports after execution, stored in reports/.
Configuration Patterns
  • Use config.yaml or config.json to store environment URLs, browser options, and credentials.
  • Load configuration in conftest.py using fixtures to provide data to tests.
  • Use @pytest.mark.parametrize to pass multiple sets of test data directly in test functions or load from external files.
  • Keep sensitive data out of code by using environment variables or encrypted files.
Test Reporting and CI/CD Integration
  • Use pytest plugins like pytest-html or pytest-json-report to generate readable reports.
  • Reports are saved in reports/ folder for easy access.
  • Integrate tests into CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Failing tests with @pytest.mark.parametrize show which input data caused failure for quick debugging.
Best Practices
  1. Use @pytest.mark.parametrize to avoid repeating similar test code for different inputs.
  2. Keep test data simple and readable; use descriptive parameter names.
  3. Combine @pytest.mark.parametrize with fixtures for flexible and maintainable tests.
  4. Organize test data in separate files if it grows large, and load it dynamically.
  5. Write clear assertions so failures show exactly what input caused the problem.
Self Check

Where in this folder structure would you add a new test file that uses @pytest.mark.parametrize to test login functionality with multiple username and password combinations?

Key Result
Use @pytest.mark.parametrize in test files under tests/ to run the same test with multiple input sets efficiently.