{"id":"a3c023d9df83ca16","repo":"stretchr/testify","slug":"require-must-not-be-called-before-run-or-sett","errorCode":null,"errorMessage":"'Require' must not be called before 'Run' or 'SetT'","messagePattern":"'Require' must not be called before 'Run' or 'SetT'","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"suite/suite.go","lineNumber":61,"sourceCode":"\tsuite.mu.Lock()\n\tdefer suite.mu.Unlock()\n\tsuite.t = t\n\tsuite.Assertions = assert.New(t)\n\tsuite.require = require.New(t)\n}\n\n// SetS needs to set the current test suite as parent\n// to get access to the parent methods\nfunc (suite *Suite) SetS(s TestingSuite) {\n\tsuite.s = s\n}\n\n// Require returns a require context for suite.\nfunc (suite *Suite) Require() *require.Assertions {\n\tsuite.mu.Lock()\n\tdefer suite.mu.Unlock()\n\tif suite.require == nil {\n\t\tpanic(\"'Require' must not be called before 'Run' or 'SetT'\")\n\t}\n\treturn suite.require\n}\n\n// Assert returns an assert context for suite. Normally, you can call:\n//\n//\tsuite.NoError(err)\n//\n// But for situations where the embedded methods are overridden (for example,\n// you might want to override assert.Assertions with require.Assertions), this\n// method is provided so you can call:\n//\n//\tsuite.Assert().NoError(err)\nfunc (suite *Suite) Assert() *assert.Assertions {\n\tsuite.mu.Lock()\n\tdefer suite.mu.Unlock()\n\tif suite.Assertions == nil {\n\t\tpanic(\"'Assert' must not be called before 'Run' or 'SetT'\")","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/suite/suite.go#L43-L79","documentation":"suite.Require() (suite/suite.go:57-64) returns the per-suite *require.Assertions handle, which is only allocated inside SetT (suite.go:42-48). The library panics with this message when Require() is invoked on a suite whose SetT has never run, because returning a nil *require.Assertions would just defer the failure to a nil-pointer deref deeper in the test. The panic pins the misuse to the exact call site and tells the author the suite lifecycle wasn't entered.","triggerScenarios":"Calling suite.Require() (or this.Require() from an embedded Suite) before suite.Run(t, &s) or an explicit SetT(t) has executed. Concretely: (a) constructing &MySuite{} and calling its methods in a plain func(t *testing.T) without suite.Run; (b) calling Require() inside SetupSuite/SetupTest of a suite that was registered via t.Run(...) instead of suite.Run(t, ...); (c) calling Require() from a goroutine spawned in BeforeTest that races ahead of SetT, or after manually zeroing the suite; (d) embedding *suite.Suite by value and copying it before Run wires the receiver.","commonSituations":"Migrating a flat *testing.T test to the suite model and forgetting to swap t.Run for suite.Run; subtest registered via t.Run(name, s.MyTest) reading suite.Require() inside MyTest; refactoring SetupAllSuite logic into a helper invoked from TestMain where no T exists; copying a suite struct value instead of passing a pointer (SetT mutates the receiver, so a copy stays uninitialized).","solutions":["Drive the suite through the framework entrypoint: call suite.Run(t, &MySuite{}) once in your top-level TestXxx, and let it invoke SetupSuite/SetupTest/Test*/TearDown — by the time those run, SetT has executed and Require() is safe.","If you construct the suite yourself, call s.SetT(t) explicitly before any Require()/Assert() use.","Always pass a pointer (&MySuite{}) to suite.Run; embedding suite.Suite by value plus copying will leave require nil on the copy.","Move any Require()/Assert() calls out of goroutines spawned before SetT, or pass the already-initialized suite by pointer into the goroutine."],"exampleFix":"// before\nfunc TestFoo(t *testing.T) {\n    s := &MySuite{}\n    s.SetupSuite()        // user-invoked; SetT never ran\n    s.Require().Equal(1, 1) // panic: Require before Run/SetT\n}\n\n// after\nfunc TestFoo(t *testing.T) {\n    suite.Run(t, &MySuite{}) // SetT runs first; Require() safe inside SetupSuite/Test*\n}\n// inside the suite:\nfunc (s *MySuite) SetupSuite() { s.Require().Equal(1, 1) }","handlingStrategy":"validation","validationCode":"// Guard before calling Require() on a suite whose lifecycle you don't control.\nfunc safeRequire(s *suite.Suite) (*require.Assertions, error) {\n    // Suite.require is private; the only public signal is T().\n    if s.T() == nil {\n        return nil, errors.New(\"suite not initialized: call suite.Run or SetT first\")\n    }\n    return s.Require(), nil\n}","typeGuard":"// Type-narrow on the suite interface rather than guarding Require each time:\n// only enter suite-internal helpers when the framework has set T.\nfunc suiteReady(s TestingSuite) bool {\n    type tGetter interface{ T() *testing.T }\n    g, ok := s.(tGetter)\n    return ok && g.T() != nil\n}\n\n// usage: if !suiteReady(s) { t.Skip(\"suite not run via suite.Run\") }","tryCatchPattern":"// Convert the panic into a test failure at a controlled boundary.\nfunc safeRequireCall(t *testing.T, s *MySuite, fn func(*require.Assertions)) {\n    defer func() {\n        if r := recover(); r != nil {\n            t.Fatalf(\"Require misuse: %v\\nEnsure the suite is started with suite.Run(t, &MySuite{}).\", r)\n        }\n    }()\n    fn(s.Require())\n}","preventionTips":["Always enter suites via suite.Run(t, &MySuite{}) and never wrap suite methods in your own t.Run; the framework owns SetT.","Pass the suite by pointer; never copy a *suite.Suite value.","Reserve Require()/Assert() for suite-lifecycle methods (Setup*/Test*/TearDown*) — not for TestMain or package-level helpers.","Lint for `&suite.Suite{}` or `MySuite{}` literals outside suite.Run; treat them as smells."],"tags":["testify","suite","lifecycle","panic","go"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}