Automated testing for ML code in MLOps - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we run automated tests on machine learning code, we want to know how the time to run these tests changes as the code or data grows.
We ask: How does testing time increase when we add more tests or bigger data?
Analyze the time complexity of the following code snippet.
for test_case in test_suite:
model_output = model.predict(test_case.input_data)
assert model_output == test_case.expected_output
This code runs each test case by making the model predict and then checking the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each test case in the test suite.
- How many times: Once per test case, so as many times as there are tests.
As the number of test cases grows, the total time to run all tests grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 model predictions and checks |
| 100 | 100 model predictions and checks |
| 1000 | 1000 model predictions and checks |
Pattern observation: Doubling the number of tests roughly doubles the total work.
Time Complexity: O(n)
This means the testing time grows directly in proportion to the number of test cases.
[X] Wrong: "Running more tests won't affect total time much because each test is fast."
[OK] Correct: Even if each test is quick, many tests add up, so total time grows with the number of tests.
Understanding how test time grows helps you plan testing strategies and shows you can think about code efficiency beyond just writing tests.
"What if each test case input data size also grows with n? How would the time complexity change then?"
Practice
Solution
Step 1: Understand the role of automated testing
Automated testing is used to check if code works correctly without manual checks.Step 2: Identify the main benefit in ML context
In ML, it helps find bugs early and keeps the code reliable during changes.Final Answer:
To catch bugs early and keep the code reliable -> Option DQuick Check:
Automated testing = catch bugs early [OK]
- Thinking automated tests speed up code
- Confusing testing with data processing
- Believing tests replace data cleaning
Solution
Step 1: Recognize Python test function syntax
In Python, functions start with 'def' and have parentheses and a colon.Step 2: Check each option's syntax
def test_accuracy(): assert model_accuracy > 0.8 uses correct Python syntax with 'def' and parentheses. Others have syntax errors or wrong language style.Final Answer:
def test_accuracy(): assert model_accuracy > 0.8 -> Option BQuick Check:
Python test function = def + parentheses + colon [OK]
- Omitting parentheses in function definition
- Using JavaScript syntax in Python
- Missing colon after function header
def test_accuracy():
assert model_accuracy > 0.8, "Accuracy too low"
test_accuracy()Solution
Step 1: Understand the assert statement
The assert checks if model_accuracy > 0.8. If false, it raises AssertionError with message.Step 2: Evaluate the condition with model_accuracy = 0.75
0.75 is not greater than 0.8, so assertion fails and raises error with message "Accuracy too low".Final Answer:
AssertionError: Accuracy too low -> Option AQuick Check:
Assert false triggers AssertionError [OK]
- Thinking assert prints message on success
- Confusing AssertionError with SyntaxError
- Ignoring the error message text
def test_model():
assert model.predict(X) == y
print("Test passed")
test_model()Solution
Step 1: Check indentation of function call
The call to test_model() is indented, which is invalid outside function or block.Step 2: Confirm other syntax parts are correct
Function definition has colon, assert syntax is correct, print is allowed. Only indentation is wrong.Final Answer:
Indentation error before calling test_model() -> Option AQuick Check:
Top-level calls must not be indented [OK]
- Indenting function calls at top level
- Confusing assert syntax errors
- Thinking print is disallowed in tests
Solution
Step 1: Identify goal of testing accuracy
We want to detect if accuracy drops unexpectedly, so tests must check accuracy value.Step 2: Evaluate options for effectiveness
Write tests that assert accuracy is above a set threshold after training asserts accuracy above threshold, catching drops. Write tests that print accuracy without checking values only prints, no check. Write tests that only check if training runs without errors ignores accuracy value. Write tests that compare accuracy to previous run without threshold compares to previous run but no threshold, may miss small drops.Final Answer:
Write tests that assert accuracy is above a set threshold after training -> Option CQuick Check:
Assert accuracy > threshold catches drops [OK]
- Not asserting accuracy value
- Only printing results without checks
- Ignoring accuracy thresholds in tests
