### Define and Run a Simple Go Test Group Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown Demonstrates how to create a `testgroup` in Go, defining a struct with methods as subtests and using `testgroup.RunSerially` as the entry point. It includes an example `AbsInt` function and its corresponding test group `AbsIntTests`. ```Go package testgroup_test import ( "testing" "github.com/bloomberg/go-testgroup" ) func AbsInt(n int) int { if n < 0 { return -n } return n } // This function is the entry point to the test group. // Because it starts with "Test" and accepts a *testing.T argument, // it is detected and called by the Go testing package when you run "go test". func TestAbsInt(t *testing.T) { testgroup.RunSerially(t, &AbsIntTests{}) } type AbsIntTests struct { // Group-wide data/state can be stored here. } func (grp *AbsIntTests) DoesNotChangeNonNegativeNumbers(t *testgroup.T) { t.Equal(0, AbsInt(0)) t.Equal(1, AbsInt(1)) t.Equal(123456789, AbsInt(123456789)) } func (grp *AbsIntTests) MakesNegativeNumbersPositive(t *testgroup.T) { t.Equal(1, AbsInt(-1)) t.Equal(123456789, AbsInt(-123456789)) } ``` -------------------------------- ### Run Go Test Group from Top-Level `testing.T` Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This example shows how to integrate a `testgroup` into a standard Go `testing`-style test. The `testgroup.RunSerially` function is used to execute the subtests defined within `MyGroup` from a top-level `TestMyGroup` function, passing the `*testing.T` instance. ```Go func TestMyGroup(t *testing.T) { testgroup.RunSerially(t, &MyGroup{}) } ``` -------------------------------- ### Console Output for Go Test Group Execution Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown Shows the expected terminal output when running the `AbsInt` test group example using `go test -v`, illustrating how Go's testing package reports the execution of the main test function and its subtests. ```console $ go test -v example_absint_test.go === RUN TestAbsInt === RUN TestAbsInt/DoesNotChangeNonNegativeNumbers === RUN TestAbsInt/MakesNegativeNumbersPositive --- PASS: TestAbsInt (0.00s) --- PASS: TestAbsInt/DoesNotChangeNonNegativeNumbers (0.00s) --- PASS: TestAbsInt/MakesNegativeNumbersPositive (0.00s) PASS ok command-line-arguments 0.014s ``` -------------------------------- ### Execute Go Test Group Subtests in Parallel Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This Go example demonstrates running `testgroup` subtests concurrently using `testgroup.RunInParallel`. It defines a `MyGroup` with methods A, B, and C, showing that `testgroup` handles `t.Parallel()` automatically and wraps subtests in a parent test for hook management. ```Go func TestParallel(t *testing.T) { testgroup.RunInParallel(t, &MyGroup{}) } type MyGroup struct{} func (*MyGroup) C(t *testgroup.T) {} func (*MyGroup) A(t *testgroup.T) {} func (*MyGroup) B(t *testgroup.T) {} ``` -------------------------------- ### Implement Go Test Group Hooks (Pre/Post Group/Test) Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This Go code illustrates the use of special hook methods within a `testgroup` struct. `PreGroup` and `PostGroup` run before and after all subtests, respectively, while `PreTest` and `PostTest` execute before and after each individual subtest. All hooks accept a single `*testgroup.T` argument. ```Go func (*MyGroup) PreGroup(t *testgroup.T) { // code that will run before any of MyGroup's subtests have started } func (*MyGroup) PostGroup(t *testgroup.T) { // code that will run after all of MyGroup's subtests have finished } func (*MyGroup) PreTest(t *testgroup.T) { // code that will run at the beginning of each subtest in MyGroup } func (*MyGroup) PostTest(t *testgroup.T) { // code that will run at the end of each subtest in MyGroup } ``` -------------------------------- ### Execute Go Test Group Subtests Serially Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This snippet demonstrates running `testgroup` subtests in lexicographical order using `testgroup.RunSerially`. It defines a `MyGroup` with methods A, B, and C, showing how `RunSerially` ensures they execute in alphabetical order. ```Go func TestSerial(t *testing.T) { testgroup.RunSerially(t, &MyGroup{}) } type MyGroup struct{} func (*MyGroup) C(t *testgroup.T) {} func (*MyGroup) A(t *testgroup.T) {} func (*MyGroup) B(t *testgroup.T) {} ``` -------------------------------- ### Console Output for Serial Go Test Execution Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This console output illustrates the execution flow when `testgroup.RunSerially` is used. It shows that subtests A, B, and C are run sequentially and pass, confirming the lexicographical order of execution. ```Console $ go test -v serial_test.go === RUN TestSerial === RUN TestSerial/A === RUN TestSerial/B === RUN TestSerial/C --- PASS: TestSerial (0.00s) --- PASS: TestSerial/A (0.00s) --- PASS: TestSerial/B (0.00s) --- PASS: TestSerial/C (0.00s) PASS ok command-line-arguments 0.014s ``` -------------------------------- ### testgroup.T API Reference Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This section details the methods and embedded types available on `testgroup.T`, providing convenience wrappers for package-level functions and direct access to standard Go testing utilities and `testify` assertion libraries. ```APIDOC testgroup.T: RunSerially(): Wraps the package-level `testgroup.RunSerially` function. RunInParallel(): Wraps the package-level `testgroup.RunInParallel` function. Embeds *testing.T: Provides direct access to standard `testing.T` methods like `Skip`, `Logf`, and `Failed`. Embeds *assert.Assertions: Allows direct calls to `testify/assert` methods (e.g., `Equal`, `NoError`). Require *require.Assertions: A field providing access to `testify/require` methods (e.g., `NotNil`), which immediately stop test execution upon failure. ``` -------------------------------- ### Define Basic Go Test Group with Subtest Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This snippet demonstrates how to define a basic test group in Go using a struct. Exported methods of the struct, like `Subtest`, are automatically recognized as subtests by the `testgroup` library. Subtests must accept a `*testgroup.T` argument and return nothing. ```Go type MyGroup struct{} func (*MyGroup) Subtest(t *testgroup.T) { // ... } ``` -------------------------------- ### Using testing.T Methods with testgroup.T in Go Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown The `testgroup.T` type embeds a `*testing.T`, allowing users to directly call standard `testing.T` methods such as `Skip` and `Logf` within their `testgroup` subtests. This ensures compatibility and leverages existing Go testing utilities. ```go func (*MyGroup) MySubtest(t *testgroup.T) { if testing.Short() { t.Skip("skipping due to short mode") } t.Logf("Have we failed yet? %v", t.Failed()) } ``` -------------------------------- ### Asserting with testify/require using testgroup.T in Go Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown `testgroup.T` includes a `Require` field, which is a `*Assertions` from `testify/require`. This enables assertions that immediately stop test execution upon failure, providing a stricter failure mechanism compared to `testify/assert`. ```go func (*MyGroup) MySubtest(t *testgroup.T) { const expectedValue = 42 result, err := somethingThatMightError(input) t.NoError(err) // testify/assert assertion -- continues test execution if it fails t.Require.NotNil(result) // testify/require assertion -- stops test execution if it fails t.Equal(expectedValue, result) } ``` -------------------------------- ### Console Output for Parallel Go Test Execution Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This console output demonstrates the execution flow when `testgroup.RunInParallel` is used. It shows subtests A, B, and C being paused and then continued, indicating parallel execution, and confirms the successful completion of all tests. ```Console $ go test -v parallel_test.go === RUN TestParallel === RUN TestParallel/_ === RUN TestParallel/_/A === PAUSE TestParallel/_/A === RUN TestParallel/_/B === PAUSE TestParallel/_/B === RUN TestParallel/_/C === PAUSE TestParallel/_/C === CONT TestParallel/_/A === CONT TestParallel/_/C === CONT TestParallel/_/B --- PASS: TestParallel (0.00s) --- PASS: TestParallel/_ (0.00s) --- PASS: TestParallel/_/B (0.00s) --- PASS: TestParallel/_/C (0.00s) --- PASS: TestParallel/_/A (0.00s) PASS ok command-line-arguments 0.014s ``` -------------------------------- ### Run Nested Subtests with `testgroup.T.Run` Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown This Go snippet illustrates how to use `testgroup.T.Run` to define nested subtests within a `testgroup` method, similar to `testing.T.Run`. It enables organizing test cases into a table-driven format, with each entry running as a distinct subtest. ```Go func (*MyGroup) MySubtest(t *testgroup.T) { // set up a table of testcases type testcase struct{ input, output int } table := []testcase{ // ... } for _, tc := range table { tc := tc // local copy to pin range variable t.Run(fmt.Sprintf("%d", tc.input), func (t *testgroup.T) { t.Equal(tc.output, someCalculation(tc.input)) }) } } ``` -------------------------------- ### Asserting with testify/assert using testgroup.T in Go Source: https://github.com/bloomberg/go-testgroup/blob/main/README.markdown Similar to `testify/suite`, `testgroup.T` embeds `*Assertions` from `testify/assert`. This allows direct invocation of `testify/assert` member functions (e.g., `Equal`) on the `testgroup.T` instance for standard test assertions. ```go func (*MyGroup) MySubtest(t *testgroup.T) { const expectedValue = 42 result := callSomeFunction(input) t.Equal(expectedValue, result) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.