0
0
PyTesttesting~8 mins

Basic assert statement in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Basic assert statement
Folder Structure
my_pytest_project/
├── tests/
│   ├── test_example.py
│   └── __init__.py
├── conftest.py
├── pytest.ini
└── README.md

This is a simple pytest project structure. The tests/ folder contains test files. conftest.py holds shared fixtures or hooks. pytest.ini configures pytest options.

Test Framework Layers
  • Tests: Test functions using assert statements to verify expected outcomes.
  • Fixtures (conftest.py): Setup and teardown code shared across tests.
  • Utilities: Helper functions or data used by tests (optional in basic setups).
  • Configuration: pytest.ini file to set test options like markers or test paths.
Configuration Patterns

Use pytest.ini to configure pytest behavior:

[pytest]
minversion = 7.0
addopts = -ra -q
testpaths = tests

For environment variables or credentials, use OS environment variables or pytest fixtures in conftest.py. This keeps sensitive data out of code.

Test Reporting and CI/CD Integration

Pytest outputs test results in the console by default. For better reports, use plugins like pytest-html to generate HTML reports.

Example command to generate report:

pytest --html=report.html

Integrate pytest in CI/CD pipelines (GitHub Actions, Jenkins) by running tests and collecting reports automatically.

Best Practices
  • Use simple assert statements to check expected results clearly.
  • Write one assertion per test function to isolate failures.
  • Keep tests independent and repeatable.
  • Use descriptive test function names to explain what is tested.
  • Use fixtures for setup to avoid duplication.
Self Check

Where in this folder structure would you add a new test file for checking user login functionality?

Key Result
Organize pytest tests in a tests/ folder using simple assert statements and fixtures for setup.