随时进行单一测试

I have two tests in my project, I would like to build a single test, place the resulting binary in a container, run it, and then attach a debugger.

Is this possible?

package dataplatform

import "testing"

func TestA(t *testing.T) {

    // test A
}

func TestRunCommand(t *testing.T) {

    // Test B
}

You may use -run <regexp> to limit (filter) the tests to run. So for example if you want to run only the TestA() test, you may do it like this:

go test -run TestA

Actually the above will run all tests whose names contain TestA, so to be explicit, it would be:

go test -run ^TestA$

To not run the tests but generate the test binary, you may use the -c option:

go test -c

This won't run the tests, but compile a binary which when executed will run the tests.

The problem is that you can't combine these options, e.g. running

go test -c -run TestA

Will generate a binary which when executed will run all tests.

The truth is that the generated binary accepts the same parameters as go test, so you may pass -run TestA to the generated binary, but you must prefix the params with test:

Each of these flags is also recognized with an optional 'test.' prefix, as in -test.v. When invoking the generated test binary (the result of 'go test -c') directly, however, the prefix is mandatory.

So if the name of the generated test binary is my.test, run it like:

./my.test -test.run TestA

For more options and documentation, run go help test, or visit the official documentation:

Command Go

And the relevant section:

Command Go: Testing flags