0
0
Testing Fundamentalstesting~15 mins

Test suite organization in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Organize and run multiple test cases in a test suite
Preconditions (3)
Step 1: Create three test files: test_math.py, test_strings.py, test_lists.py
Step 2: In test_math.py, write a test function to check addition of two numbers
Step 3: In test_strings.py, write a test function to check string uppercase conversion
Step 4: In test_lists.py, write a test function to check list append operation
Step 5: Create a test suite file named test_suite.py that imports all three test files
Step 6: Run the test suite using pytest command
Step 7: Verify that all three tests run and pass
✅ Expected Result: All three tests run successfully and pass, showing a summary report with 3 passed tests
Automation Requirements - pytest
Assertions Needed:
Verify addition result equals expected value
Verify string uppercase conversion matches expected
Verify list append results in expected list length
Best Practices:
Use descriptive test function names
Keep tests independent and isolated
Organize tests logically by feature or functionality
Use pytest conventions for test discovery
Avoid hardcoding values inside tests; use variables
Automated Solution
Testing Fundamentals
import pytest

# test_math.py

def test_addition():
    result = 2 + 3
    assert result == 5

# test_strings.py

def test_uppercase():
    text = "hello"
    assert text.upper() == "HELLO"

# test_lists.py

def test_append():
    items = [1, 2, 3]
    items.append(4)
    assert len(items) == 4

# test_suite.py

import test_math
import test_strings
import test_lists

if __name__ == "__main__":
    pytest.main()

The code defines three test functions, each in a separate section simulating separate files: test_math.py, test_strings.py, and test_lists.py. Each test checks a simple operation with an assertion.

The test_suite.py file imports these test modules and runs pytest to discover and execute all tests automatically.

This organization shows how to group related tests and run them together as a suite, following pytest conventions for easy test discovery and reporting.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not using pytest naming conventions for test functions', 'why_bad': "Pytest will not discover tests that do not start with 'test_' automatically, so tests won't run.", 'correct_approach': "Always name test functions starting with 'test_' so pytest can find and run them."}
Putting multiple unrelated tests in one function
Hardcoding values inside tests without variables
Not isolating tests leading to shared state
Bonus Challenge

Now add data-driven testing with 3 different inputs for the addition test

Show Hint