pytest-cov setup - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
import subprocess import sys import os # Step 1: Install pytest-cov subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pytest-cov']) # Step 2: Create calculator.py calculator_code = ''' def add(a, b): return a + b ''' with open('calculator.py', 'w') as f: f.write(calculator_code) # Step 3: Create test_calculator.py test_code = ''' import calculator def test_add(): assert calculator.add(2, 3) == 5 ''' with open('test_calculator.py', 'w') as f: f.write(test_code) # Step 4: Run pytest with coverage result = subprocess.run([ sys.executable, '-m', 'pytest', '--cov=calculator', '--cov-report=term', 'test_calculator.py' ], capture_output=True, text=True) # Step 5: Print output print(result.stdout) # Assert coverage report presence assert 'coverage' in result.stdout.lower() or '100%' in result.stdout, 'Coverage report not found in output' # Assert test passed assert '1 passed' in result.stdout, 'Test did not pass as expected' # Cleanup created files os.remove('calculator.py') os.remove('test_calculator.py')
This script automates the setup and verification of pytest-cov coverage reporting.
First, it installs pytest-cov using pip to ensure coverage measurement is available.
Then, it creates a simple Python module calculator.py with an add function.
Next, it writes a test file test_calculator.py that tests the add function using pytest.
It runs pytest with the --cov=calculator option to measure coverage only on the calculator module and requests a terminal coverage report.
The script captures and prints the test run output, then asserts that the coverage report and test pass message appear in the output.
Finally, it cleans up the created files to keep the environment tidy.
This approach ensures the coverage tool is correctly installed, the test runs successfully, and coverage is reported as expected.
Now add data-driven testing with 3 different input pairs for the add function
Practice
pytest-cov in testing?Solution
Step 1: Recall pytest-cov's core function
pytest-cov is a plugin that tracks which parts of your code are executed during tests.Step 2: Eliminate incorrect options
Only To measure how much of your code is tested by your tests correctly describes coverage measurement, others describe unrelated features.Final Answer:
To measure how much of your code is tested by your tests -> Option BQuick Check:
pytest-cov measures coverage = A [OK]
- Confusing coverage with test speed
- Thinking pytest-cov fixes tests
- Assuming it generates test data
Solution
Step 1: Recall the exact package name
The official package name is 'pytest-cov' with a hyphen.Step 2: Compare options with correct spelling
Only pip install pytest-cov matches the correct package name and syntax.Final Answer:
pip install pytest-cov -> Option AQuick Check:
Correct package name = B [OK]
- Using underscore instead of hyphen
- Adding spaces in package name
- Misspelling the package name
pytest --cov=my_module if all code in my_module is covered by tests?Solution
Step 1: Analyze the pytest --cov command
The command runs tests and measures coverage for 'my_module'.Step 2: Determine output for 100% coverage
If all code is tested, coverage report shows 100% coverage.Final Answer:
A coverage report showing 100% coverage for my_module -> Option DQuick Check:
Full coverage means 100% report = A [OK]
- Expecting errors when coverage is full
- Thinking coverage report is hidden by default
- Assuming tests fail if coverage is 100%
pytest --cov=my_module --cov-report=html but no HTML report is generated. What is the most likely cause?Solution
Step 1: Check common reasons for missing HTML report
pytest-cov generates HTML reports in a folder named 'htmlcov' by default.Step 2: Understand report location
The report is generated but may be in a folder you did not check.Final Answer:
The HTML report is generated in a different folder -> Option CQuick Check:
HTML report folder = C [OK]
- Assuming plugin is not installed without checking
- Thinking report is shown in terminal only
- Believing tests must fail to generate report
mod1 and mod2 and generate both terminal and HTML reports. Which command is correct?Solution
Step 1: Understand multiple module coverage syntax
pytest-cov accepts multiple modules separated by commas in a single --cov option.Step 2: Understand multiple report formats syntax
Multiple reports are specified by repeating the --cov-report flag, e.g., --cov-report=term --cov-report=html.Final Answer:
pytest --cov=mod1,mod2 --cov-report=term --cov-report=html -> Option AQuick Check:
Comma for modules, repeat --cov-report = D [OK]
- Using multiple --cov options instead of comma separation
- Combining modules without commas
- Incorrectly combining report types in one option
