0
0
PyTesttesting~8 mins

Multiple parameters in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Multiple parameters
Folder Structure
tests/
├── test_example.py       # Test files with multiple parameter tests
├── conftest.py           # Fixtures and hooks
utilities/
├── data_provider.py      # Data sources for parameterization
pytest.ini               # Pytest configuration file
Test Framework Layers
  • Tests Layer: Contains test functions using @pytest.mark.parametrize with multiple parameters to run tests with different input combinations.
  • Utilities Layer: Provides data providers or helper functions to supply complex parameter sets.
  • Fixtures Layer: Defined in conftest.py to setup test preconditions or shared resources.
  • Configuration Layer: Manages pytest.ini and environment variables for test runs.
Configuration Patterns
  • Use pytest.ini to configure markers and default options.
  • Use fixtures in conftest.py to inject environment-specific data or setup.
  • Parameterize tests with multiple parameters using @pytest.mark.parametrize decorator, passing tuples or lists of tuples for combinations.
  • Use helper functions in utilities to generate complex parameter sets if needed.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with --junitxml=report.xml for CI systems.
  • Integrate with CI tools like GitHub Actions, Jenkins, or GitLab CI to run parameterized tests automatically on code changes.
  • Use plugins like pytest-html for readable HTML reports showing each parameter combination as a separate test case.
  • Ensure test failures clearly indicate which parameter set caused the failure for easy debugging.
Best Practices
  1. Use descriptive test IDs or names for parameter sets to make reports readable.
  2. Keep parameter sets manageable in size to avoid very long test runs.
  3. Use fixtures to handle setup/teardown separately from parameter data.
  4. Organize parameter data in separate utility files if complex or reused across tests.
  5. Use explicit tuple unpacking in test function parameters for clarity.
Self Check

Where in this framework structure would you add a new set of complex parameter data for a test that requires three input values?

Key Result
Use @pytest.mark.parametrize with tuples in test files and organize parameter data in utilities for clean multiple parameter testing.