import subprocess
import sys
import os
def test_pytest_discovers_and_runs_tests():
# Run pytest on the 'tests' package
result = subprocess.run([sys.executable, '-m', 'pytest', 'tests', '--tb=short', '--disable-warnings'], capture_output=True, text=True)
output = result.stdout
# Check that pytest ran tests from both test modules
assert 'test_math.py' in output, "test_math.py tests not found in pytest output"
assert 'test_string.py' in output, "test_string.py tests not found in pytest output"
# Check that all tests passed
assert 'failed' not in output.lower(), "Some tests failed"
assert 'error' not in output.lower(), "Some tests had errors"
assert 'passed' in output.lower(), "No tests passed"
# Check that pytest exited with code 0
assert result.returncode == 0, f"pytest exited with code {result.returncode}"This test runs pytest as a subprocess on the 'tests' package folder.
It captures the output and checks that both test modules appear in the output, meaning pytest discovered them.
It asserts that no tests failed or errored, and that some tests passed.
Finally, it asserts that pytest exited with code 0, indicating success.
This approach tests the package discovery and execution end-to-end, simulating a real user running pytest.