0
0
PyTesttesting~15 mins

pytest-cov setup - Build an Automation Script

Choose your learning style9 modes available
Setup pytest-cov to measure test coverage
Preconditions (3)
Step 1: Install pytest-cov using pip
Step 2: Create a simple Python function in a file named calculator.py
Step 3: Write a pytest test function in test_calculator.py to test the calculator function
Step 4: Run pytest with the --cov option to measure coverage on calculator.py
Step 5: Verify that the coverage report is displayed in the console
✅ Expected Result: pytest runs the tests and shows a coverage report indicating which lines of calculator.py were executed
Automation Requirements - pytest
Assertions Needed:
Test function asserts expected output from calculator function
Coverage report includes calculator.py with coverage percentage
Best Practices:
Use pytest fixtures if needed
Keep tests isolated and independent
Use clear and descriptive test function names
Run coverage only on the target module, not tests
Automated Solution
PyTest
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.

Common Mistakes - 4 Pitfalls
Not installing pytest-cov before running coverage
Running coverage on test files instead of source files
Not asserting coverage report presence in automated tests
Hardcoding file paths without cleanup
Bonus Challenge

Now add data-driven testing with 3 different input pairs for the add function

Show Hint