Beispiel
github.com/stretchr/testify v1.11.1: Control testify suite execution and lifecycle hooks through the testify.m regex flag
Verifiziertes Beispiel für golang github.com/stretchr/testify v1.11.1: Control testify suite execution and lifecycle hooks through the testify.m regex flag.…
sha256:4c3a1fafee223038dc33b51481aedf5c31bbd508ee95f5a14e4e0711d58f2484
Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen.
Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen.
MIT-0
Ausführungsbelege
Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.
- Beleggrundlage
- Signierter Vertrag bestanden
- Verifizierungsbelege
- 2
- Signaturschlüssel, die es gebaut haben
- 2
Deklarierte Umgebung
go linux x64 go go gomod
Umgebungen der Verifizierungsläufe
| Umgebung | Contract | Stufen | Lauf |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-19 |
| go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-19 |
Fall
HOW- Ziel
- Control testify suite execution and lifecycle hooks through the testify.m regex flag
- Symbole
-
- suite.Run
- suite.Suite
- Umgebung
- go
- Erstellt
- 2026-08-19T13:31:30Z
Contract
- Setting the testify.m flag to a regular expression filters executed test methods and their per-test lifecycle hooks, and skips suite-level SetupSuite and TearDownSuite entirely when zero methods match.
- assert the suite package registers the testify.m string flag in flag.CommandLine with an empty string default
- assert default execution runs all Test methods and invokes SetupSuite, SetupTest, BeforeTest, AfterTest, TearDownTest, and TearDownSuite in exact lifecycle order
- assert setting testify.m to a matching regex executes only the matched method and skips lifecycle hooks for unmatched methods
- assert setting testify.m to a pattern matching no methods skips all test execution including SetupSuite and TearDownSuite
- assert methods without the Test prefix are never executed by suite.Run regardless of the testify.m filter pattern
Dateien
- NOTES.md
- csx.json
- go.mod
- go.sum
- suite_config_test.go
- tracker.go
Quelltext
# testify Suite Method Filtering and Lifecycle Configuration
## Search Findings
Prior search in the sample registry identified sample `sha256:06e2218bf94b889eb5e1ba6287aa0148d4b894a277afe7b73f5ae6c8c0fdc1aa`, which investigates assertion failure handling between `assert` and `require` packages. This sample investigates a different area: configuration and method filtering in `github.com/stretchr/testify/suite` via the `-testify.m` flag and its effect on the suite lifecycle execution flow.
## Pinned Release
- `github.com/stretchr/testify v1.11.1`
## Observed Behavior
- **Flag Registration**: Importing `github.com/stretchr/testify/suite` registers a global command-line flag named `testify.m` in `flag.CommandLine` with an empty string (`""`) as its default value.
- **Full Lifecycle Flow**: Under default configuration (`testify.m = ""`), `suite.Run` discovers all methods with the `Test` prefix and invokes them in lexical order. For each test method, the lifecycle order is:
1. `SetupSuite()` (invoked once at suite start)
2. `SetupTest()`
3. `BeforeTest(suiteName, testName)`
4. `TestMethod()`
5. `AfterTest(suiteName, testName)`
6. `TearDownTest()`
7. `TearDownSuite()` (invoked once at suite end)
- **Method Filtering**: Setting `testify.m` to a regular expression (e.g., `^TestAlpha$`) filters execution to only matching methods. Unmatched test methods are completely skipped along with their associated `SetupTest`, `BeforeTest`, `AfterTest`, and `TearDownTest` hooks.
- **Suite-Level Hook Suppression on Zero Matches**: When `testify.m` matches zero methods (e.g., `^NonExistentTestPattern$`), `suite.Run` determines that no tests need to be executed and returns immediately. As a result, `SetupSuite()` and `TearDownSuite()` are entirely skipped (recording zero events), contradicting the common assumption that suite-level hooks always run whenever `suite.Run` is invoked.
- **Prefix Requirement**: Methods lacking the `Test` prefix (e.g., helper methods) are never invoked by `suite.Run`, even if their name directly matches the `testify.m` regex pattern.
{"case":{"believed":"A testify suite always executes its SetupSuite and TearDownSuite hooks whenever suite.Run is called, even when the testify.m flag filters out all test methods.","caseId":"case:sha256:ea5d7c5972fd7d27c9c6918677af9992eeb544eadd8732d2daa8163ff8b56a22","contract":["Setting the testify.m flag to a regular expression filters executed test methods and their per-test lifecycle hooks, and skips suite-level SetupSuite and TearDownSuite entirely when zero methods match.","assert the suite package registers the testify.m string flag in flag.CommandLine with an empty string default","assert default execution runs all Test methods and invokes SetupSuite, SetupTest, BeforeTest, AfterTest, TearDownTest, and TearDownSuite in exact lifecycle order","assert setting testify.m to a matching regex executes only the matched method and skips lifecycle hooks for unmatched methods","assert setting testify.m to a pattern matching no methods skips all test execution including SetupSuite and TearDownSuite","assert methods without the Test prefix are never executed by suite.Run regardless of the testify.m filter pattern"],"goal":"Control testify suite execution and lifecycle hooks through the testify.m regex flag","kind":"HOW","packages":["pkg:golang/github.com/stretchr/testify@1.11.1"],"schemaVersion":1,"symbols":["suite.Run","suite.Suite"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","ecosystem":"golang","executionContext":"go","language":"go","os":"linux","packageManager":"gomod","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/stretchr/testify@1.11.1"],"schemaVersion":1,"symbols":["suite.Run","suite.Suite"],"verifierAdapter":"golang@1"}
module codesamplex.dev/sample/gotestifysuite
go 1.24
require github.com/stretchr/testify v1.11.1
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package testifysuite
import (
"flag"
"reflect"
"testing"
"github.com/stretchr/testify/suite"
)
func TestFlagRegistration(t *testing.T) {
f := flag.CommandLine.Lookup("testify.m")
if f == nil {
t.Fatal("expected flag 'testify.m' to be registered on flag.CommandLine")
}
if f.DefValue != "" {
t.Fatalf("expected default value of 'testify.m' to be empty string, got %q", f.DefValue)
}
}
func TestDefaultExecution(t *testing.T) {
if err := flag.CommandLine.Set("testify.m", ""); err != nil {
t.Fatalf("failed to set testify.m flag: %v", err)
}
tracker := new(LifecycleTracker)
suite.Run(t, tracker)
expected := []string{
"SetupSuite",
"SetupTest",
"BeforeTest:TestAlpha",
"TestAlpha",
"AfterTest:TestAlpha",
"TearDownTest",
"SetupTest",
"BeforeTest:TestBeta",
"TestBeta",
"AfterTest:TestBeta",
"TearDownTest",
"TearDownSuite",
}
if !reflect.DeepEqual(tracker.Events, expected) {
t.Fatalf("unexpected lifecycle event sequence.\nGot: %v\nWant: %v", tracker.Events, expected)
}
}
func TestFilteredExecution(t *testing.T) {
if err := flag.CommandLine.Set("testify.m", "^TestAlpha$"); err != nil {
t.Fatalf("failed to set testify.m flag: %v", err)
}
defer func() {
_ = flag.CommandLine.Set("testify.m", "")
}()
tracker := new(LifecycleTracker)
suite.Run(t, tracker)
expected := []string{
"SetupSuite",
"SetupTest",
"BeforeTest:TestAlpha",
"TestAlpha",
"AfterTest:TestAlpha",
"TearDownTest",
"TearDownSuite",
}
if !reflect.DeepEqual(tracker.Events, expected) {
t.Fatalf("unexpected lifecycle event sequence for filtered test.\nGot: %v\nWant: %v", tracker.Events, expected)
}
}
func TestNoMatchingMethodsSkipsAllLifecycleHooks(t *testing.T) {
if err := flag.CommandLine.Set("testify.m", "^NonExistentTestPattern$"); err != nil {
t.Fatalf("failed to set testify.m flag: %v", err)
}
defer func() {
_ = flag.CommandLine.Set("testify.m", "")
}()
tracker := new(LifecycleTracker)
suite.Run(t, tracker)
if len(tracker.Events) != 0 {
t.Fatalf("expected 0 events when no methods match filter, got %v", tracker.Events)
}
}
func TestNonTestPrefixMethodsIgnored(t *testing.T) {
if err := flag.CommandLine.Set("testify.m", "NonTestMethod"); err != nil {
t.Fatalf("failed to set testify.m flag: %v", err)
}
defer func() {
_ = flag.CommandLine.Set("testify.m", "")
}()
tracker := new(LifecycleTracker)
suite.Run(t, tracker)
if len(tracker.Events) != 0 {
t.Fatalf("methods without Test prefix must never execute, got %v", tracker.Events)
}
}
package testifysuite
import (
"github.com/stretchr/testify/suite"
)
// LifecycleTracker records the order and occurrence of suite and test hooks.
type LifecycleTracker struct {
suite.Suite
Events []string
}
func (s *LifecycleTracker) SetupSuite() {
s.Events = append(s.Events, "SetupSuite")
}
func (s *LifecycleTracker) TearDownSuite() {
s.Events = append(s.Events, "TearDownSuite")
}
func (s *LifecycleTracker) SetupTest() {
s.Events = append(s.Events, "SetupTest")
}
func (s *LifecycleTracker) TearDownTest() {
s.Events = append(s.Events, "TearDownTest")
}
func (s *LifecycleTracker) BeforeTest(suiteName, testName string) {
s.Events = append(s.Events, "BeforeTest:"+testName)
}
func (s *LifecycleTracker) AfterTest(suiteName, testName string) {
s.Events = append(s.Events, "AfterTest:"+testName)
}
func (s *LifecycleTracker) TestAlpha() {
s.Events = append(s.Events, "TestAlpha")
}
func (s *LifecycleTracker) TestBeta() {
s.Events = append(s.Events, "TestBeta")
}
func (s *LifecycleTracker) NonTestMethod() {
s.Events = append(s.Events, "NonTestMethod")
}