import subprocess
import sys
import re
def test_pytest_module_execution():
# Run pytest on the test_sample.py module
result = subprocess.run([sys.executable, '-m', 'pytest', 'test_sample.py', '--tb=short', '-q'], capture_output=True, text=True)
output = result.stdout
# Check pytest exited with code 0 (success)
assert result.returncode == 0, f"pytest exited with code {result.returncode}"
# Check output contains test function names and pass status
assert re.search(r'test_function_one', output), "test_function_one not found in output"
assert re.search(r'test_function_two', output), "test_function_two not found in output"
assert re.search(r'\d+ passed', output), "No tests passed message found"
This test script runs pytest on the module test_sample.py using Python's subprocess module. It captures the output and return code.
We assert that pytest exits with code 0, meaning all tests passed. Then we check the output text to confirm the test function names appear and that the summary shows tests passed.
This approach verifies that the test module runs correctly and pytest reports results as expected.