var initRan … // Init registers testing flags. These flags are automatically registered by // the "go test" command before running test functions, so Init is only needed // when calling functions such as Benchmark without using "go test". // // Init is not safe to call concurrently. It has no effect if it was already called. func Init() { … } var short … var failFast … var outputDir … var chatty … var count … var coverProfile … var gocoverdir … var matchList … var match … var skip … var memProfile … var memProfileRate … var cpuProfile … var blockProfile … var blockProfileRate … var mutexProfile … var mutexProfileFraction … var panicOnExit0 … var traceFile … var timeout … var cpuListStr … var parallel … var shuffle … var testlog … var fullPath … var haveExamples … var cpuList … var testlogFile … var numFailed … var running … type chattyFlag … func (*chattyFlag) IsBoolFlag() bool { … } func (f *chattyFlag) Set(arg string) error { … } func (f *chattyFlag) String() string { … } func (f *chattyFlag) Get() any { … } const marker … func (f *chattyFlag) prefix() string { … } type chattyPrinter … func newChattyPrinter(w io.Writer) *chattyPrinter { … } // prefix is like chatty.prefix but using p.json instead of chatty.json. // Using p.json allows tests to check the json behavior without modifying // the global variable. For convenience, we allow p == nil and treat // that as not in json mode (because it's not chatty at all). func (p *chattyPrinter) prefix() string { … } // Updatef prints a message about the status of the named test to w. // // The formatted message must include the test name itself. func (p *chattyPrinter) Updatef(testName, format string, args ...any) { … } // Printf prints a message, generated by the named test, that does not // necessarily mention that tests's name itself. func (p *chattyPrinter) Printf(testName, format string, args ...any) { … } const maxStackLen … type common … // Short reports whether the -test.short flag is set. func Short() bool { … } var testBinary … // Testing reports whether the current code is being run in a test. // This will report true in programs created by "go test", // false in programs created by "go build". func Testing() bool { … } // CoverMode reports what the test coverage mode is set to. The // values are "set", "count", or "atomic". The return value will be // empty if test coverage is not enabled. func CoverMode() string { … } // Verbose reports whether the -test.v flag is set. func Verbose() bool { … } func (c *common) checkFuzzFn(name string) { … } // frameSkip searches, starting after skip frames, for the first caller frame // in a function not marked as a helper and returns that frame. // The search stops if it finds a tRunner function that // was the entry point into the test and the test is not a subtest. // This function must be called with c.mu held. func (c *common) frameSkip(skip int) runtime.Frame { … } // decorate prefixes the string with the file and line of the call site // and inserts the final newline if needed and indentation spaces for formatting. // This function must be called with c.mu held. func (c *common) decorate(s string, skip int) string { … } // flushToParent writes c.output to the parent after first writing the header // with the given format and arguments. func (c *common) flushToParent(testName, format string, args ...any) { … } type indenter … func (w indenter) Write(b []byte) (n int, err error) { … } // fmtDuration returns a string representing d in the form "87.00s". func fmtDuration(d time.Duration) string { … } type TB … var _ … var _ … type T … func (c *common) private() { … } // Name returns the name of the running (sub-) test or benchmark. // // The name will include the name of the test along with the names of // any nested sub-tests. If two sibling sub-tests have the same name, // Name will append a suffix to guarantee the returned name is unique. func (c *common) Name() string { … } func (c *common) setRan() { … } // Fail marks the function as having failed but continues execution. func (c *common) Fail() { … } // Failed reports whether the function has failed. func (c *common) Failed() bool { … } // FailNow marks the function as having failed and stops its execution // by calling runtime.Goexit (which then runs all deferred calls in the // current goroutine). // Execution will continue at the next test or benchmark. // FailNow must be called from the goroutine running the // test or benchmark function, not from other goroutines // created during the test. Calling FailNow does not stop // those other goroutines. func (c *common) FailNow() { … } // log generates the output. It's always at the same stack depth. func (c *common) log(s string) { … } // logDepth generates the output at an arbitrary stack depth. func (c *common) logDepth(s string, depth int) { … } // Log formats its arguments using default formatting, analogous to Println, // and records the text in the error log. For tests, the text will be printed only if // the test fails or the -test.v flag is set. For benchmarks, the text is always // printed to avoid having performance depend on the value of the -test.v flag. func (c *common) Log(args ...any) { … } // Logf formats its arguments according to the format, analogous to Printf, and // records the text in the error log. A final newline is added if not provided. For // tests, the text will be printed only if the test fails or the -test.v flag is // set. For benchmarks, the text is always printed to avoid having performance // depend on the value of the -test.v flag. func (c *common) Logf(format string, args ...any) { … } // Error is equivalent to Log followed by Fail. func (c *common) Error(args ...any) { … } // Errorf is equivalent to Logf followed by Fail. func (c *common) Errorf(format string, args ...any) { … } // Fatal is equivalent to Log followed by FailNow. func (c *common) Fatal(args ...any) { … } // Fatalf is equivalent to Logf followed by FailNow. func (c *common) Fatalf(format string, args ...any) { … } // Skip is equivalent to Log followed by SkipNow. func (c *common) Skip(args ...any) { … } // Skipf is equivalent to Logf followed by SkipNow. func (c *common) Skipf(format string, args ...any) { … } // SkipNow marks the test as having been skipped and stops its execution // by calling [runtime.Goexit]. // If a test fails (see Error, Errorf, Fail) and is then skipped, // it is still considered to have failed. // Execution will continue at the next test or benchmark. See also FailNow. // SkipNow must be called from the goroutine running the test, not from // other goroutines created during the test. Calling SkipNow does not stop // those other goroutines. func (c *common) SkipNow() { … } // Skipped reports whether the test was skipped. func (c *common) Skipped() bool { … } // Helper marks the calling function as a test helper function. // When printing file and line information, that function will be skipped. // Helper may be called simultaneously from multiple goroutines. func (c *common) Helper() { … } // Cleanup registers a function to be called when the test (or subtest) and all its // subtests complete. Cleanup functions will be called in last added, // first called order. func (c *common) Cleanup(f func()) { … } // TempDir returns a temporary directory for the test to use. // The directory is automatically removed when the test and // all its subtests complete. // Each subsequent call to t.TempDir returns a unique directory; // if the directory creation fails, TempDir terminates the test by calling Fatal. func (c *common) TempDir() string { … } // removeAll is like os.RemoveAll, but retries Windows "Access is denied." // errors up to an arbitrary timeout. // // Those errors have been known to occur spuriously on at least the // windows-amd64-2012 builder (https://go.dev/issue/50051), and can only occur // legitimately if the test leaves behind a temp file that either is still open // or the test otherwise lacks permission to delete. In the case of legitimate // failures, a failing test may take a bit longer to fail, but once the test is // fixed the extra latency will go away. func removeAll(path string) error { … } // Setenv calls os.Setenv(key, value) and uses Cleanup to // restore the environment variable to its original value // after the test. // // Because Setenv affects the whole process, it cannot be used // in parallel tests or tests with parallel ancestors. func (c *common) Setenv(key, value string) { … } // Chdir calls os.Chdir(dir) and uses Cleanup to restore the current // working directory to its original value after the test. On Unix, it // also sets PWD environment variable for the duration of the test. // // Because Chdir affects the whole process, it cannot be used // in parallel tests or tests with parallel ancestors. func (c *common) Chdir(dir string) { … } // Context returns a context that is canceled just before // [T.Cleanup]-registered functions are called. // // Cleanup functions can wait for any resources // that shut down on Context.Done before the test completes. func (c *common) Context() context.Context { … } type panicHandling … const normalPanic … const recoverAndReturnPanic … // runCleanup is called at the end of the test. // If ph is recoverAndReturnPanic, it will catch panics, and return the // recovered value if any. func (c *common) runCleanup(ph panicHandling) (panicVal any) { … } // resetRaces updates c.parent's count of data race errors (or the global count, // if c has no parent), and updates c.lastRaceErrors to match. // // Any races that occurred prior to this call to resetRaces will // not be attributed to c. func (c *common) resetRaces() { … } // checkRaces checks whether the global count of data race errors has increased // since c's count was last reset. // // If so, it marks c as having failed due to those races (logging an error for // the first such race), and updates the race counts for the parents of c so // that if they are currently suspended (such as in a call to T.Run) they will // not log separate errors for the race(s). // // Note that multiple tests may be marked as failed due to the same race if they // are executing in parallel. func (c *common) checkRaces() (raceErrors int64) { … } // callerName gives the function name (qualified with a package path) // for the caller after skip frames (where 0 means the current function). func callerName(skip int) string { … } func pcToName(pc uintptr) string { … } const parallelConflict … // Parallel signals that this test is to be run in parallel with (and only with) // other parallel tests. When a test is run multiple times due to use of // -test.count or -test.cpu, multiple instances of a single test never run in // parallel with each other. func (t *T) Parallel() { … } func (t *T) checkParallel() { … } // Setenv calls os.Setenv(key, value) and uses Cleanup to // restore the environment variable to its original value // after the test. // // Because Setenv affects the whole process, it cannot be used // in parallel tests or tests with parallel ancestors. func (t *T) Setenv(key, value string) { … } // Chdir calls os.Chdir(dir) and uses Cleanup to restore the current // working directory to its original value after the test. On Unix, it // also sets PWD environment variable for the duration of the test. // // Because Chdir affects the whole process, it cannot be used // in parallel tests or tests with parallel ancestors. func (t *T) Chdir(dir string) { … } type InternalTest … var errNilPanicOrGoexit … func tRunner(t *T, fn func(t *T)) { … } // Run runs f as a subtest of t called name. It runs f in a separate goroutine // and blocks until f returns or calls t.Parallel to become a parallel test. // Run reports whether f succeeded (or at least did not fail before calling t.Parallel). // // Run may be called simultaneously from multiple goroutines, but all such calls // must return before the outer test function for t returns. func (t *T) Run(name string, f func(t *T)) bool { … } // Deadline reports the time at which the test binary will have // exceeded the timeout specified by the -timeout flag. // // The ok result is false if the -timeout flag indicates “no timeout” (0). func (t *T) Deadline() (deadline time.Time, ok bool) { … } type testState … func newTestState(maxParallel int, m *matcher) *testState { … } func (s *testState) waitParallel() { … } func (s *testState) release() { … } var errMain … type matchStringOnly … func (f matchStringOnly) MatchString(pat, str string) (bool, error) { … } func (f matchStringOnly) StartCPUProfile(w io.Writer) error { … } func (f matchStringOnly) StopCPUProfile() { … } func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { … } func (f matchStringOnly) ImportPath() string { … } func (f matchStringOnly) StartTestLog(io.Writer) { … } func (f matchStringOnly) StopTestLog() error { … } func (f matchStringOnly) SetPanicOnExit0(bool) { … } func (f matchStringOnly) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error { … } func (f matchStringOnly) RunFuzzWorker(func(corpusEntry) error) error { … } func (f matchStringOnly) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) { … } func (f matchStringOnly) CheckCorpus([]any, []reflect.Type) error { … } func (f matchStringOnly) ResetCoverage() { … } func (f matchStringOnly) SnapshotCoverage() { … } func (f matchStringOnly) InitRuntimeCoverage() (mode string, tearDown func(string, string) (string, error), snapcov func() float64) { … } // Main is an internal function, part of the implementation of the "go test" command. // It was exported because it is cross-package and predates "internal" packages. // It is no longer used by "go test" but preserved, as much as possible, for other // systems that simulate "go test" using Main, but Main sometimes cannot be updated as // new functionality is added to the testing package. // Systems simulating "go test" should be updated to use MainStart. func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) { … } type M … type testDeps … // MainStart is meant for use by tests generated by 'go test'. // It is not meant to be called directly and is not subject to the Go 1 compatibility document. // It may change signature from release to release. func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M { … } var testingTesting … var realStderr … // Run runs the tests. It returns an exit code to pass to os.Exit. func (m *M) Run() (code int) { … } func (t *T) report() { … } func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) { … } // RunTests is an internal function but exported because it is cross-package; // it is part of the implementation of the "go test" command. func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) { … } func runTests(matchString func(pat, str string) (bool, error), tests []InternalTest, deadline time.Time) (ran, ok bool) { … } // before runs before all testing. func (m *M) before() { … } // after runs after all testing. func (m *M) after() { … } func (m *M) writeProfiles() { … } // toOutputDir returns the file name relocated, if required, to outputDir. // Simple implementation to avoid pulling in path/filepath. func toOutputDir(path string) string { … } // startAlarm starts an alarm if requested. func (m *M) startAlarm() time.Time { … } // runningList returns the list of running tests. func runningList() []string { … } // stopAlarm turns off the alarm. func (m *M) stopAlarm() { … } func parseCpuList() { … } func shouldFailFast() bool { … }