0
0
PyTesttesting~15 mins

Why CI integration enables continuous quality in PyTest - Automation Benefits in Action

Choose your learning style9 modes available
Verify that CI integration runs tests automatically on code push
Preconditions (3)
Step 1: Push a commit with a small code change to the main branch
Step 2: Observe the CI service starting a test run automatically
Step 3: Wait for the test run to complete
Step 4: Check the test results in the CI dashboard
✅ Expected Result: The CI service automatically runs the pytest tests on the pushed commit and reports all tests passing without manual intervention
Automation Requirements - pytest
Assertions Needed:
Verify that pytest test suite runs without failures
Verify that test results are reported (simulated by pytest output)
Best Practices:
Use pytest fixtures for setup and teardown
Write clear and independent test functions
Use assert statements for validation
Keep tests fast and isolated
Automated Solution
PyTest
import pytest

# Sample function to test
def add(a, b):
    return a + b

# Test case to verify add function

def test_add_positive_numbers():
    assert add(2, 3) == 5

def test_add_negative_numbers():
    assert add(-1, -1) == -2

def test_add_zero():
    assert add(0, 0) == 0

# To run these tests, push this code to your Git repo and observe CI running pytest automatically

if __name__ == "__main__":
    pytest.main(["-v"])

This script defines a simple add function and three pytest test functions to check it with positive numbers, negative numbers, and zero.

Each test uses assert to verify the expected output.

When this code is pushed to a Git repository configured with CI (like GitHub Actions), the CI system will automatically run pytest and report the results.

This automation ensures continuous quality by running tests on every code change without manual effort.

Common Mistakes - 4 Pitfalls
Not using assert statements in pytest tests
Writing tests that depend on each other
{'mistake': 'Not configuring CI to run pytest on push', 'why_bad': "If CI is not set up properly, tests won't run automatically, losing continuous quality benefits.", 'correct_approach': 'Configure CI workflows to trigger pytest on every push or pull request.'}
Using print statements instead of asserts for validation
Bonus Challenge

Now add data-driven testing with 3 different input pairs for the add function

Show Hint