Skip to content
Back to skills

Principle Test Behavior Not Implementation

BSecurity

Use when writing or changing a test, or deciding whether one should stay. Check what callers can see through the interface they call, so the test breaks when behavior breaks and survives changes to internals alone.

  • 1,531 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 7, 2026
ai-agentsgogitapidatabasesecurity

Works with

  • cli
  • api

Security analysis

B75/100
  • criticalAccesses sensitive system or user directories

Pro shows the line behind each finding and how to fix it

Scanned November 7, 2026

npx -y skills add hashgraph-online/awesome-codex-plugins --skill principle-test-behavior-not-implementation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Principle Test Behavior Not Implementation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Principle Test Behavior Not Implementation
[![Security: B — Skills Directory](https://www.skillsdirectory.com/api/skills/hashgraph-online-principle-test-behavior-not-implementation/badge)](https://www.skillsdirectory.com/skills/hashgraph-online-principle-test-behavior-not-implementation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: principle-test-behavior-not-implementation
description: Use when writing or changing a test, or deciding whether one should stay. Check what callers can see through the interface they call, so the test breaks when behavior breaks and survives changes to internals alone.
disable-model-invocation: true
---

# Test behavior, not implementation

A test should check results visible to the code's users via the interface they call. It should fail once that visible behavior breaks and stay green when nothing but the internals move.

## When it applies

Writing a test, changing one, or deciding whether to keep one.

## Terms

- **Users**: callers, end users, and other services.
- **Behavior**: whatever users can observe. That covers return values, thrown errors, state they can read back afterwards, and effects on external systems (an outgoing request, a database row, a published message).
- **Implementation**: everything they cannot see, meaning private helpers, the order of internal calls, intermediate data, and which internal component performed the work.

## What to do

1. Run two thought experiments on each test.
   - **Break the behavior.** Return a wrong value, drop an effect, or duplicate one. If the test stays green, it shows nothing. Usual reasons: the assertion is weaker than the contract, the expected value came from the code being tested, the test restates a constant or prompt string without running the code that uses it, or it asserts on data the test built itself.
   - **Restructure the internals** while keeping every observable result. If the test fails, it is pinned to implementation. It will need rewriting on every refactor and catches no bugs.
2. Keep a test only if it catches the first change and tolerates the second. Rewrite or remove the others.
3. To write a good test, exercise the code through the interface its users call, using a concrete input. Work out the expected result by reading the requirement, never by running the code, and assert precisely what the contract guarantees.
4. Let the contract, not assertion style, decide what counts:
   - If the promise is that a request without a valid token never reaches the database, asserting the database client received no query is behavior. Pair it with a positive case where a valid token does produce a query, so the test demonstrably can observe calls.
   - The payload sent to a mocked external service is behavior, because that service receives it. Calling an internal helper is an implementation detail.
   - An empty list is a precise expected value when nothing should match. It is a hollow assertion when the test only needed some result.
   - Callers do not read the exact wording of an error message. They branch on the error type, so the type is the contract. For an auth failure, assert the `Unauthorized` type and the 401 status, not the sentence.
5. A test can pin an invariant rather than a call: for instance, a key required in both of two tables, or a type assertion checked at compile time. Here the invariant is the behavior.

Security tests are where hollow assertions do the most damage. A test named `test_rejects_path_traversal` that sends `../etc/passwd` and asserts "an exception was raised" passes when the code crashes for an unrelated reason. Assert the specific rejection and add a positive case that reads a legitimate file through the same path.

## The failure it prevents

Tests that never catch bugs, tests that break on every refactor, and expected values that confirm themselves.

## Done when

Each kept test passes both thought experiments.

Related: [principle-migrate-callers-then-delete-legacy-apis](../principle-migrate-callers-then-delete-legacy-apis/SKILL.md), [principle-foundational-thinking](../principle-foundational-thinking/SKILL.md), [tdd](../tdd/SKILL.md).

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.