0
0
Testing Fundamentalstesting~15 mins

Shift-left testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Automate unit test execution early in development
Preconditions (2)
Step 1: Write a unit test for the function that checks correct output for input 5
Step 2: Run the unit test immediately after writing the function
Step 3: Verify the test passes without errors
✅ Expected Result: The unit test runs successfully and confirms the function returns the expected output for input 5
Automation Requirements - pytest
Assertions Needed:
Verify function output equals expected value
Best Practices:
Write tests close to code (shift-left principle)
Use clear and simple assertions
Keep tests small and focused
Automated Solution
Testing Fundamentals
def multiply_by_two(x):
    return x * 2


def test_multiply_by_two():
    result = multiply_by_two(5)
    assert result == 10, f"Expected 10 but got {result}"

if __name__ == "__main__":
    import pytest
    pytest.main([__file__])

This script defines a simple function multiply_by_two that doubles the input.

The test function test_multiply_by_two calls this function with input 5 and asserts the output is 10.

Running the test immediately after writing the function follows the shift-left testing idea: testing early in development to catch issues quickly.

The assertion includes a helpful message if it fails.

The pytest.main call allows running the test by executing the script directly.

Common Mistakes - 3 Pitfalls
Writing tests only after full feature completion
Using print statements instead of assertions
Testing too many things in one test
Bonus Challenge

Now add data-driven testing with 3 different inputs (2, 5, 10) and expected outputs

Show Hint