{"record":{"id":"3bcc32f59e33d97c","repo":"golang/go","slug":"multiple-definitions-of-testmain","errorCode":null,"errorMessage":"multiple definitions of TestMain","messagePattern":"multiple definitions of TestMain","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/test.go","lineNumber":764,"sourceCode":"\t\t\tcontinue\n\t\t}\n\t\tif n.Recv != nil {\n\t\t\tcontinue\n\t\t}\n\t\tname := n.Name.String()\n\t\tswitch {\n\t\tcase name == \"TestMain\":\n\t\t\tif isTestFunc(n, \"T\") {\n\t\t\t\tt.Tests = append(t.Tests, testFunc{pkg, name, \"\", false})\n\t\t\t\t*doImport, *seen = true, true\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\terr := checkTestFunc(n, \"M\")\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif t.TestMain != nil {\n\t\t\t\treturn errors.New(\"multiple definitions of TestMain\")\n\t\t\t}\n\t\t\tt.TestMain = &testFunc{pkg, name, \"\", false}\n\t\t\t*doImport, *seen = true, true\n\t\tcase isTest(name, \"Test\"):\n\t\t\terr := checkTestFunc(n, \"T\")\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tt.Tests = append(t.Tests, testFunc{pkg, name, \"\", false})\n\t\t\t*doImport, *seen = true, true\n\t\tcase isTest(name, \"Benchmark\"):\n\t\t\terr := checkTestFunc(n, \"B\")\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tt.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, \"\", false})\n\t\t\t*doImport, *seen = true, true\n\t\tcase isTest(name, \"Fuzz\"):","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/test.go#L746-L782","documentation":"A Go test binary may declare at most one func TestMain(m *testing.M). While scanning *_test.go functions, the loader appends a TestMain match only if none was recorded; encountering a second valid TestMain (correct name and signature M) returns this error and aborts test compilation.","triggerScenarios":"Two or more *_test.go files in the same Go package each define `func TestMain(m *testing.M)`.","commonSituations":"Copy-pasting a test setup file that already has TestMain; merging test helper modules each carrying their own TestMain; pulling in a testutil package whose example file defines TestMain.","solutions":["Keep a single TestMain in one _test.go file and delete the duplicates.","Consolidate setup from multiple TestMain functions into one, calling shared helper functions.","Move per-file setup into TestMain-less helpers (e.g. t.Helper functions or sync.Once init)."],"exampleFix":"// before: a_test.go and b_test.go both define TestMain\n// after: keep TestMain only in main_test.go;\n//   other files call shared setup() helpers from individual TestXxx","handlingStrategy":"validation","validationCode":"// Fail fast in CI before `go test`:\n//   if [ \"$(grep -rn 'func TestMain' *_test.go | wc -l)\" -gt 1 ]; then\n//     echo \"multiple TestMain\"; exit 1; fi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grep for `func TestMain` whenever you copy a test setup file into a package.","Consolidate setup into a single main_test.go per package."],"tags":["go","testing","build"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}