import subprocess
import sys
def test_run_pytest_command():
# Run pytest command on test_sample.py
result = subprocess.run([sys.executable, '-m', 'pytest', 'test_sample.py'], capture_output=True, text=True)
# Check that pytest exited with code 0 (success)
assert result.returncode == 0, f"pytest failed with code {result.returncode}"
# Check output contains '1 passed' indicating one test passed
assert '1 passed' in result.stdout, "Expected '1 passed' in pytest output"
This test runs the pytest command on the test_sample.py file using Python's subprocess module.
We capture the output and check the return code to ensure pytest ran successfully.
Then we assert that the output contains '1 passed' which means one test passed as expected.
This simulates running pytest from the command line and verifies the test execution result.