Run pytest tests and verify output
Preconditions (3)
✅ Expected Result: pytest runs all tests in test_sample.py and shows a summary with number of tests passed or failed
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.
Now add data-driven testing by running pytest on three different test files and verify each output
pytest command do when run in a project folder?pytest without arguments searches for test files and runs all tests found.-v option stands for verbose and shows detailed test results.-d, --detail, and --verbose-output are not valid pytest flags.pytest test_math.py, what will happen?pytest -v test_example.py but get an error: pytest: error: unrecognized arguments: -v. What is the likely cause?-v is unrecognized, which is unusual because -v is a valid pytest option.slow in your project using pytest. Which command correctly runs only those tests?-m option runs tests matching a marker expression, such as slow.--only, --mark, and -s do not filter tests by marker.Pass the test and this pattern moves unknown