0
0
PyTesttesting~15 mins

Test packages in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify pytest discovers and runs tests in a test package
Preconditions (4)
Step 1: Open a terminal or command prompt
Step 2: Navigate to the project root directory (the parent of 'tests' folder)
Step 3: Run the command 'pytest tests'
Step 4: Observe the test discovery and execution output
✅ Expected Result: pytest discovers all test functions in both 'test_math.py' and 'test_string.py' inside the 'tests' package and runs them, showing a summary with all tests passed
Automation Requirements - pytest
Assertions Needed:
Verify that pytest discovers all test functions in the 'tests' package
Verify that all tests run and pass successfully
Best Practices:
Use a test package (folder with __init__.py) to organize tests
Name test files and functions with 'test_' prefix for pytest discovery
Use pytest's built-in assertion rewriting for clear test assertions
Keep tests independent and simple
Automated Solution
PyTest
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.

Common Mistakes - 4 Pitfalls
{'mistake': "Not naming test files or functions with 'test_' prefix", 'why_bad': "pytest will not discover tests without the 'test_' prefix, so tests won't run", 'correct_approach': "Always name test files and functions starting with 'test_' to ensure pytest finds them"}
Running pytest without specifying the test package folder
Not checking pytest exit code in automation
Parsing pytest output with fragile string matching
Bonus Challenge

Now add data-driven testing with 3 different inputs in one of the test modules

Show Hint