hotai.news
← Back to AI WikiAI Wiki / Core concepts

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.

HotAI.news · Sources checked with CodexPublished on 2026-10-20Sources checked on 2026-10-20
Our take

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.

01

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]) == 0
02

Define 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.

03

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.

Back to AI Wiki ↗