Zum Inhalt springen

Binding requirements to code and tests and checking acceptance

A pull request changes an accepted test, and every test stays green. A pinned acceptance in CodeCharter reports exactly that change.

by CodeCharter
  • requirements
  • traceability
  • testing

A tech lead at a service provider is responsible for a .NET application whose specification contains two security promises. A password reset link may work only once, and it expires 30 minutes after it was issued. The customer has accepted both promises together with their tests. Now a developer opens a pull request that extends the lifetime to 60 minutes and adjusts the test to match:

-    private static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(30);
+    private static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(60);
…
-        clock.Advance(TimeSpan.FromMinutes(30));
+        clock.Advance(TimeSpan.FromMinutes(60));
$ dotnet test Shop.slnx --nologo

Passed!  - Failed:     0, Passed:     2, Skipped:     0, Total:     2, Duration: 112 ms - Shop.Tests.dll (net10.0)

The tests now check a different promise from the one that was accepted, and the build is green. The tech lead has to decide how the team binds requirements to tests and code, and which command in the PR gate reports a change like this. The solution in this article is constructed; the outputs shown come from real runs with CodeCharter v1.7.0 and the .NET SDK 10.0.402.

The requirement as a file

CodeCharter reads requirements from Markdown files with YAML front matter in the .codecharter/req directory. The file REQ-2.req.md describes the lifetime and names the test that checks it:

---
id: REQ-2
title: A password reset link expires 30 minutes after it was issued
priority: must
contractual: true
status: implemented
kind: security
source: Lastenheft, Abschnitt 4.2
criteria:
  - id: AC-1
    test: Shop.Tests.Security.PasswordResetTests.TokenExpiresAfterThirtyMinutes
anchors:
  - attribute_expected: Shop.Security.PasswordResetService
---
Ein Reset-Link ist bis einschließlich 30 Minuten nach der Ausstellung gültig
und wird danach abgelehnt.

The criterion AC-1 binds the requirement to a test by its fully qualified method name. The code is bound by an attribute from the NuGet package CodeCharter.Annotations, which the developer puts on the implementing method. REQ-1, the single-use promise, hangs on the same method:

[Satisfies("REQ-1")]
[Satisfies("REQ-2")]
public bool Redeem(string token)
{
    if (!_issued.Remove(token, out var issuedAt))
        return false;

    return clock.GetUtcNow() - issuedAt <= Lifetime;
}

In v1.7.0 the attribute alone is not enough. If the file lacks the attribute_expected entry, codecharter req check reports the error req-without-anchor for status: implemented, even when the code carries the attribute. v1.7.0 does not check the type name behind attribute_expected; in the example, Nonsense.Type passed without an error as well. A third requirement, REQ-3, demands a token that cannot be guessed and binds the method Issue directly via symbol_id. There CodeCharter expects the compiler's declaration ID with its prefix, that is M:Shop.Security.PasswordResetService.Issue(System.String). With Shop.Security.PasswordResetService.Issue, the run with --with-code answers:

$ codecharter req check --with-code --no-color
…
[ERROR] req-anchor-not-found .codecharter/req/REQ-3.req.md:13: symbol id 'Shop.Security.PasswordResetService.Issue' was not found in the code model
…

The file format reference describes every field and anchor kind.

Acceptance with a pin

A person accepts each criterion with codecharter req accept. The command analyzes the solution and computes a hash over the test method and a fingerprint over the bound method. It writes both into the file together with the approver and the date:

$ codecharter req accept REQ-2 AC-1 --by l.hoffmann
Accepted 'REQ-2/AC-1': approved_by=l.hoffmann, approved_on=2026-11-12, test_hash=v2:5b7db71772da2739ed1fa7084911b1a8, fingerprint=v2:9476e69c771fc9f4fcef2692d955f44b
 criteria:
   - id: AC-1
     test: Shop.Tests.Security.PasswordResetTests.TokenExpiresAfterThirtyMinutes
+    approved_by: l.hoffmann
+    approved_on: 2026-11-12
+    test_hash: "v2:5b7db71772da2739ed1fa7084911b1a8"
+    fingerprint: "v2:9476e69c771fc9f4fcef2692d955f44b"

These four lines are called the pin. The hash is built from the tokens of the declaration, so whitespace and comments do not count. Acceptance exists only on the command line and deliberately not as an MCP tool, so that a coding agent cannot approve its own work. CodeCharter does not verify the identity behind --by. It comes from the option, from CODECHARTER_REQ_APPROVER or from git config user.name, and the pull request review shows who wrote the lines.

CodeCharter never runs the bound test. A criterion "holds" when the test binding, test hash and fingerprint match the pin. The verdict HOLDS therefore means accepted and unchanged since, not green. Whether the test passes is still up to the test run.

The pull request in the gate

For the PR with 60 minutes, the two variants of req check produce different results. Without an option, the command reads only the files and opens no solution:

$ codecharter req check --no-color
Requirement corpus is valid: .codecharter/req (3 requirements).

Criterion verdicts:
[UNK  ] REQ-1/AC-1 (Test): NoCodeModel
[UNK  ] REQ-2/AC-1 (Test): NoCodeModel
[UNK  ] REQ-3/AC-1 (Manual): NoCodeModel
EXIT=0

Without a code model, the command cannot compare a pin; it reports UNK and exits with 0. A gate with this call lets the PR through. With --with-code, CodeCharter analyzes the solution once and compares every pin:

$ codecharter req check --with-code --no-color
Requirement check: .codecharter/req (3 requirements, 3 criteria, 3 anchors)
[INFO ] req-anchor-overlapping-scope .codecharter/req/REQ-1.req.md: Requirement 'REQ-1' and 'REQ-2' share 1 justified method(s) in their anchor scope (e.g. Shop.Security.PasswordResetService.Redeem(string)).

Found 0 error(s), 0 warning(s).

Criterion verdicts:
[STALE] REQ-2/AC-1 (Test): TestHashChanged
EXIT=1

The test for REQ-2 has changed since acceptance, so the run exits with 1. A STALE or an error diagnostic leads to exit code 1; OPEN and UNK never fail the run. One run on the two-project example took about 2.5 seconds and, like codecharter analyze, needs a .NET SDK on the runner.

REQ-1 does not appear in the list, although the class changed. The field Lifetime lies outside Redeem, and the fingerprint covers only the bound method. If a PR changes only the field and leaves the test untouched, the pin reports nothing. In the example, only the test run would then catch the change, because the test checks the boundary at 30 minutes. A second anchor symbol_id: F:Shop.Security.PasswordResetService.Lifetime does not help. For fields CodeCharter hashes only the ID, and the pin stayed valid after the change from 30 to 60 minutes. The refactor in the next section shows that every token change in Redeem breaks the pin. A team therefore puts values that a promise depends on into the bound method. The PR gate calls codecharter req check --with-code; the CLI reference lists all exit codes.

Refactoring, formatting and renaming

The pin reports every token change to the test or the bound method, including one that does not change behavior. A second PR collapses Redeem into a single expression, and the tests stay green:

-        if (!_issued.Remove(token, out var issuedAt))
-            return false;
-
-        return clock.GetUtcNow() - issuedAt <= Lifetime;
+        return _issued.Remove(token, out var issuedAt)
+            && clock.GetUtcNow() - issuedAt <= Lifetime;
$ codecharter req check --with-code --no-color
…
Criterion verdicts:
[STALE] REQ-1/AC-1 (Test): RegionFingerprintChanged
[STALE] REQ-2/AC-1 (Test): RegionFingerprintChanged
EXIT=1

Both requirements hang on Redeem and fall together. After two new calls to req accept, only the fingerprint line changes in each file, and the gate reports All pinned criteria hold. with exit code 0. A third PR only adds a comment in Redeem and joins two lines. Here the pin stays valid because no token changes.

If a PR renames the test TokenCannotBeReused to TokenIsSingleUse, --with-code reports [STALE] REQ-1/AC-1 (Test): BindingUnresolved with exit code 1. Before acceptance, by contrast, a wrong test name goes unnoticed. A criterion without a pin is always OPEN … PinMissing, and a typo in the name produced exit code 0 in the example. The approver therefore checks at the first req accept that the named test exists and checks the promise.

Queries from a coding agent

An agent can query the corpus over MCP, for example with req_affected for a diff. Without git_ref, the tool compares the working tree with HEAD. For the PR with 60 minutes, with git_ref: "main..HEAD", it returns an empty list:

…"requirements":[],"unmatchedPaths":["src/Shop/Security/PasswordResetService.cs","tests/Shop.Tests/Security/PasswordResetTests.cs"],…

In v1.7.0, req_affected compares files only. A requirement matches when its own .req.md or the file of a file anchor changes. It does not pick up bindings via [Satisfies], symbol_id or a test name, and the code lists method-level matching as deferred work. req_verify with the same git_ref checks exactly this list in its default scope and therefore reports zero criteria. Only with scope: "all" does REQ-2/AC-1 appear as stale with the reason test-hash-changed. An agent that is meant to check its own change therefore calls req_verify with scope: "all". The MCP reference describes the other tools.

A convention with xUnit traits

Without a dedicated tool, a team writes the requirement ID directly on the test. xUnit has traits for this, and dotnet test filters by them:

[Fact]
[Trait("Requirement", "REQ-2")]
public void TokenExpiresAfterThirtyMinutes()
$ dotnet test Shop.slnx --nologo --filter Requirement=REQ-2 --logger "console;verbosity=normal"
  Passed Shop.Tests.Security.PasswordResetTests.TokenExpiresAfterThirtyMinutes [34 ms]
Total tests: 1
     Passed: 1
 Total time: 1,8338 Seconds
EXIT=0

This output comes from the same PR with 60 minutes. The filter shows which test belongs to REQ-2 and that it is green. It does not show that the test checks a different time since acceptance. A grep over the diff does not help much either. The changed lines contain no ID because the trait line stays the same, and a grep over the changed files names REQ-1 and REQ-2 alike.

A comparison of the IDs against the list of requirements finds gaps:

$ for id in $(ls .codecharter/req | sed -n 's/\.req\.md$//p'); do grep -rq --include='*.cs' "\"$id\"" tests || echo "kein Test: $id"; done
kein Test: REQ-1
kein Test: REQ-3

This output comes from a branch on which the test for REQ-1 was deleted. REQ-3 deliberately has only a manual check. On the same branch, the filter run --filter Requirement=REQ-1 only reports "No test matches the given testcase filter" and exits with 0. Without --include='*.cs', grep also finds the ID in a DLL under bin/ and no longer reports REQ-1 as missing.

The convention is robust against renames, because the trait moves with the method. Where CodeCharter reports BindingUnresolved, the filter still finds TokenIsSingleUse under REQ-1. The convention is enough as long as the team only needs to know which tests belong to a requirement and whether they are green. As soon as someone binds an acceptance to a specific state, it does not show that exactly this check has changed.

Evidence in regulated projects

In a regulated environment, the regulator expects specific evidence. For medical devices in the United States, the FDA guidance on premarket submissions for device software functions from June 2023 describes what a premarket submission should contain. According to section VI.D, each requirement should be traceable, and the submission should describe the method the manufacturer uses to identify and track requirements. According to section VI.H, it should state the software version tested and the pass/fail results for the tests, and after a change also a regression analysis. The guidance is a recommendation and, by its own statement, does not establish legally enforceable responsibilities. It predates the QMSR, which has applied since February 2, 2026, and still refers to the QSR in VI.D. The older guidance General Principles of Software Validation from 2002 recommends in section 4.7 re-establishing the validation status of the software after every change.

Both approaches deliver building blocks for this evidence. The trait convention links an ID to a test run. In the example, however, the TRX file from dotnet test contained only the test name and result, no traits. Anyone who needs pass/fail per requirement therefore puts the ID into the test name or runs the filter per ID. The pin in CodeCharter records who accepted which state. After that it reports every criterion whose test or bound method has changed since acceptance. That is an input for the regression analysis, not a test result. A team in this environment therefore runs both: dotnet test with a result file per version and codecharter req check --with-code as the gate. The team writes the method description and the regression analysis itself. Whether the documents are sufficient is decided by the review of the submission, not by a tool.

Research supports the benefit for changes but rarely measures it. Tian et al. analyzed 63 papers from 2000 to 2020; the numbers here come from the preprint version on arXiv. Change management was the activity traceability supported most often. The most frequent cost type was the effort of creating and maintaining links, named in 22 of the 63 papers. 57% of the papers supported their claims only with a demonstration or a toy example, and the authors call for more industrial evidence.

If the customer requires an acceptance per promise, the team pins it with req accept and runs codecharter req check --with-code in the pull request. Every refactor of Redeem then costs a new acceptance. Without such an acceptance, a trait with a test filter and a grep check on *.cs are enough. The pins then cause an acceptance on every refactor that nobody requires.