0
0
PyTesttesting~15 mins

PyTest installation (pip install pytest) - Build an Automation Script

Choose your learning style9 modes available
Verify PyTest installation and basic test execution
Preconditions (2)
Step 1: Open the command line or terminal
Step 2: Run the command 'pip install pytest' to install PyTest
Step 3: Verify the installation by running 'pytest --version'
Step 4: Create a new Python file named 'test_sample.py'
Step 5: Add a simple test function inside 'test_sample.py' that asserts 1 + 1 equals 2
Step 6: Run 'pytest' command in the directory containing 'test_sample.py'
Step 7: Observe the test execution result
✅ Expected Result: PyTest installs successfully, 'pytest --version' shows the installed version, and running 'pytest' executes the test with a passing result.
Automation Requirements - pytest
Assertions Needed:
Verify 'pytest --version' command outputs version info
Verify test function runs and passes with assertion 1 + 1 == 2
Best Practices:
Use subprocess module to run shell commands and capture output
Use assert statements to verify command outputs and test results
Keep test code simple and readable
Clean up created test files after test run
Automated Solution
PyTest
import subprocess
import os
import sys
import pytest

def test_pytest_installation_and_basic_test():
    # Step 1 & 2: Install pytest
    install_result = subprocess.run([sys.executable, '-m', 'pip', 'install', 'pytest'], capture_output=True, text=True)
    assert install_result.returncode == 0, f"pip install pytest failed: {install_result.stderr}"

    # Step 3: Verify pytest version
    version_result = subprocess.run(['pytest', '--version'], capture_output=True, text=True)
    assert version_result.returncode == 0, "pytest --version command failed"
    assert 'pytest' in version_result.stdout.lower(), "pytest version info not found"

    # Step 4 & 5: Create a simple test file
    test_file_content = '''def test_addition():\n    assert 1 + 1 == 2\n'''
    test_file_name = 'test_sample.py'
    with open(test_file_name, 'w') as f:
        f.write(test_file_content)

    # Step 6: Run pytest on the test file
    run_result = subprocess.run(['pytest', test_file_name, '--tb=no', '-q'], capture_output=True, text=True)

    # Step 7: Check test execution result
    assert run_result.returncode == 0, f"pytest run failed:\n{run_result.stdout}\n{run_result.stderr}"
    assert '1 passed' in run_result.stdout, "Test did not pass as expected"

    # Cleanup
    os.remove(test_file_name)

This test function automates the manual test case steps:

  • It installs pytest using pip install pytest via subprocess and checks the command succeeded.
  • It runs pytest --version to confirm pytest is installed and captures the output.
  • It creates a simple test file test_sample.py with one test function asserting 1 + 1 equals 2.
  • It runs pytest on that test file and asserts the test passes by checking the output and return code.
  • Finally, it cleans up by deleting the test file.

This approach uses subprocess to run shell commands and assert statements to verify each step, following best practices for clarity and reliability.

Common Mistakes - 3 Pitfalls
Not checking the return code of pip install command
{'mistake': "Hardcoding 'pytest' command without considering environment", 'why_bad': "On some systems, 'pytest' might not be in PATH or might refer to a different Python environment.", 'correct_approach': "Use sys.executable with '-m pytest' or ensure the environment PATH is correct to run pytest reliably."}
Not cleaning up the created test file after test run
Bonus Challenge

Now add data-driven testing by creating multiple test files with different simple assertions and verify all pass.

Show Hint