What is unit testing? Checking behavior with fixed inputs and assertions
Understand unit tests with locally run Python assertions that preserve zeros, including pass status, coverage, delivery limits, and shared assumptions in AI-written code and tests.
Understand the concept
Unit tests check a small software unit with controlled inputs and expected results to detect defects and regressions. They validate covered conditions rather than the entire system or external flows. AI-generated tests are a starting point; assertions must come from requirements rather than merely mirror implementation.
Express expected behavior through inputs and assertions
The function below counts non-missing elements, treating None as missing and zero as valid. Three local Python assertions passed for empty, zero-containing, and all-missing lists. This validates only the standalone example, not an entire project or external system.
An implementation using if value would wrongly exclude zero, exposed by these cases. Expected values should come from requirements, not copied function outputs, or tests can agree with a flawed implementation.
def count_valid(values):
return sum(value is not None for value in values)
assert count_valid([]) == 0
assert count_valid([0, None, 2]) == 2
assert count_valid([None, None]) == 0Define unit scope and external dependencies
Python unittest organizes test cases and assertions, while pytest supports assert. Controlled dependencies validate simulated conditions; real database or network interactions require integration checks beyond unit tests.
Control time and randomness to avoid intermittent results. Avoid real business writes and retain failing inputs. The example uses only local lists, making its reproducibility boundary explicit.
Passing tests still need coverage and regression checks
Confirm a new boundary test detects the original defect and passes after the fix, then check affected callers. Coverage measures executed code rather than complete requirement or input correctness; interpret it with failure cases.
AI can propose cases and drafts, but expected results need independent review, especially when the same model writes code and tests. Record commands and outcomes and identify untested interfaces or services rather than only reporting green tests.
Checked sources
These sources support the concepts and methods described here. Examples and test scope are labeled separately; check the version of the tools you use.