Test Behavior, Not Implementation
Write tests that assert observable behavior through a code's public interface, ensuring they remain valid even when internal implementation details change.
Editor's Note
Ensure tests focus on observable outcomes such as return values, errors, and external effects, not internal helper methods or data structures. Prepare concrete inputs based on requirements and assert error types that callers would branch on.
When to Use It
- Validating that a function returns the correct value after an external system call
- Ensuring that an error type is raised when a specific condition occurs
- Testing that a method correctly updates a shared state without asserting internal helper calls
SKILL.md
SkillsHunter provides this listing for informational purposes only. Review the skill’s source, license, and required permissions before use. You are responsible for deciding whether to install or run it. Terms of Use
---
name: principle-test-behavior-not-implementation
description: In tests, assert observable behavior, not internals.
disable-model-invocation: true
---
# Test Behavior, Not Implementation
A test is a claim about what users of the code can observe through its interface: return values, errors, state they can read back, and effects on outside systems. Private helpers, internal call order, and intermediate data are implementation.
Judge each test against two imagined edits:
- **Break the behavior** (a wrong value, a skipped or doubled effect). If the test still passes, it proves nothing. The usual causes are a loose assertion, an expected value computed by the code under test, and an assertion on data the test built itself.
- **Change the internals** and keep every observable result. If the test fails, it is pinned to the implementation.
Keep a test that catches the first edit and survives the second. Rewrite or delete the rest. Drive the code through its public interface with a concrete input, and derive the expected value from the requirement. What an external service receives is behavior. A call to an internal helper is not. Pair "this never happens" with a case where it does happen. Assert the error type that callers branch on, not the message.
Community Reviews & Field Notes
Tried this skill on your own project? Share the model you used and the results you got.