go test command Detailed

Foreword

go test parameters have a very rich, some of the parameters used to control compilation tests, other control parameters to perform the test.

About test coverage, vet and pprof parameters related to skip the first, we'll introduce in detail when discussing the relevant content.

Control parameters compiled

-args

Indicating the back of the -args go test parameters go to the test. Specific test function with parameters accordingly to control the testing process.

-Args can be attached to the back of a number of parameters, all parameters will be passed in a string, each parameter as a string, the string and place the slices.

// TestArgs used to demonstrate how to parse parameters -args

func TestArgs(t *testing.T) {

if !flag.Parsed() {

flag.Parse()

}

argList: = flag.Args () // flag.Args () Returns all parameters -args later, represents a slice, each element represents a parameter

for _, arg: = range argList {

if arg == "cloud" {

t.Log("Running in cloud.")

}else {

t.Log("Running in other mode.")

}

}

}

Into the parameters when performing the test:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -run TestArgs -v -args "cloud"

TestMain setup.

=== RUN TestArgs

--- PASS: TestArgs (0.00s)

unit_test.go:28: Running in cloud.

PASS

TestMain tear-down.

ok gotest 0.353s

Test parameters passed to the specified parameter -args.

-json

-json go test parameter indicates the conversion result is output to a json format to facilitate the use of automated analytical tests.

Examples are as follows:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -run TestAdd -json

{"Time":"2019-02-28T15:46:50.3756322+08:00","Action":"output","Package":"gotest","Output":"TestMain setup.\n"}

{"Time":"2019-02-28T15:46:50.4228258+08:00","Action":"run","Package":"gotest","Test":"TestAdd"}

{"Time":"2019-02-28T15:46:50.423809+08:00","Action":"output","Package":"gotest","Test":"TestAdd","Output":"=== RUN TestAdd\n"}

{"Time":"2019-02-28T15:46:50.423809+08:00","Action":"output","Package":"gotest","Test":"TestAdd","Output":"--- PASS: TestAdd (0.00s)\n"}

{"Time":"2019-02-28T15:46:50.423809+08:00","Action":"pass","Package":"gotest","Test":"TestAdd","Elapsed":0}

{"Time":"2019-02-28T15:46:50.4247922+08:00","Action":"output","Package":"gotest","Output":"PASS\n"}

{"Time":"2019-02-28T15:46:50.4247922+08:00","Action":"output","Package":"gotest","Output":"TestMain tear-down.\n"}

{"Time":"2019-02-28T15:46:50.4257754+08:00","Action":"output","Package":"gotest","Output":"ok \tgotest\t0.465s\n"}

{"Time":"2019-02-28T15:46:50.4257754+08:00","Action":"pass","Package":"gotest","Elapsed":0.465}

-O

-o parameter specifies the resulting binary executable program, and perform the test, the test does not delete the end of the program.

Without this parameter, go test the resulting binary executable program stored in the temporary directory, then delete the end of execution.

Examples are as follows:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -run TestAdd -o TestAdd

TestMain setup.

PASS

TestMain tear-down.

ok gotest 0.439s

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>TestAdd

TestMain setup.

PASS

TestMain tear-down.

In this example, use the -o parameter to specify the generated binaries "TestAdd" and place the current directory, after the end of the test execution, you can still perform the binary program directly.

Control parameters of the test

-bench regexp

The default does not go test performance tests, using -bench parameter before you can run, and run only performance test function.

Regular expressions for which screening performance test to be performed. If you want to perform all of the performance tests, using the parameter "-bench." Or "-bench =.".

Regular expression here is not strictly canonical sense, but kind of containment relationship.

There are three performance tests such as the following:

  • func BenchmarkMakeSliceWithoutAlloc(b *testing.B)

  • func BenchmarkMakeSliceWithPreAlloc(b *testing.B)

  • func BenchmarkSetBytes(b *testing.B)

Use parameters "-bench = Slice", then the first two tests because both contain "Slice", it will be executed, and the third test will not be performed.

For a scenario comprising subtest, the matching is matched in layers. As an example of the sub-test comprising:

func BenchmarkSub(b *testing.B) {

b.Run("A=1", benchSub1)

b.Run("A=2", benchSub2)

b.Run("B=1", benchSub3)

}

Test function naming convention, the name of the sub-tests need to test the parent name as a prefix and a "/" connection, the above example actually contains four tests:

  • Sub

  • Sub/A=1

  • Sub/A=2

  • Sub/B=1

If you want to perform the three sub-tests, using the parameter "-bench Sub". If you want to execute "Sub / A = 1", the parameter "-bench Sub / A = 1". If you want to execute "Sub / A = 1" and "Sub / A = 2", the parameter "-bench Sub / A =".

-benchtime s

-benchtime specify the execution time of each performance test, if not specified, the default time 1s.

For example, each of the performance test execution set execution 2s, the parameters are: "go test -bench Sub / A = 1 -benchtime 2s".

-cpu 1,2,4

After -cpu parameter provides a list of the number of CPU, providing the list, then the test specified in accordance with the list and the number of CPU provided GOMAXPROCS were tested.

For example "-cpu 1,2", then each test will be performed twice, once executed by a CPU, 2 is a CPU execution. For example, using the command "go test -bench Sub / A = 1 -cpu 1,2,3,4" test is performed:

BenchmarkSub / A = 1 1000 1256835 ns / on

BenchmarkSub / A = 1-2 2000 912109 ns / on

BenchmarkSub / A = 1-3 2000 888671 ns / on

BenchmarkSub / A = 1-4 2000 894531 ns / on

The test results in subsequent tests were -2, -3, -4, respectively, when executed on behalf GOMAXPROCS value. If GOMAXPROCS is 1, it is not displayed.

-count n

-count specified number of times each test performed, the default execution time.

For example, specify the test execution twice:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -bench Sub/A=1 -count 2

TestMain setup.

goos: windows

goarch: amd64

pkg: gotest

BenchmarkSub / A = 1-4 2000 917968 ns / on

BenchmarkSub / A = 1-4 2000 882812 ns / on

PASS

TestMain tear-down.

ok gotest 10.236s

The results can be seen in the test results will be presented twice.

If you use -count specified number of executions also specify -cpu list, then count the number of tests will be performed in the specified quantity of each CPU.

Note that the example test does not care about -count and -cpu parameters, it is always executed once.

-failfast

By default, go test will be performed to test all match, and finally print the test results, whether successful or not.

-failfast specify if there is a test failure occurs, immediately stop the test. This is when there is a large number of tests need to be performed, it can identify problems faster.

-list regexp

-list just a list of matching successful test function, does not actually perform. Moreover, not listed subroutine.

For example, using the parameter "-list Sub" then lists only three sub-tests include testing, but does not list subtests:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -list Sub

TestMain setup.

TestSubParallel

TestSub

BenchmarkSub

TestMain tear-down.

ok gotest 0.396s

-parallel n

Specifies the maximum number of concurrent tests.

When tested using t.Parallel () method to test into the concurrent, will be limited by the maximum number of concurrent, concurrent with up GOMAXPROCS test by default, other tests can only be blocked waiting.

-run regexp

According to the regular expression with unit testing and test examples. Regular matching rules and -bench similar.

-timeout d

By default, test execution over 10 minutes will time out and exit.

When cases, we set the timeout 1s, will quit because of a timeout would have been required by the test of 3s:

E:\OpenSource\GitHub\RainbowMango\GoExpertProgrammingSourceCode\GoExpert\src\gotest>go test -timeout=1s

TestMain setup.

panic: test timed out after 1s

Timeouts can be set in seconds, and by time points:

  • Set in seconds: -timeout xs or -timeout = xs

  • By sub-set: -timeout xm or -timeout = xm

  • Set time: -timeout xh or -timeout = xh

-v

By default, simply print the test results of the test results, -v parameter can print detailed log.

Under performance testing, always print the log, the log because sometimes affect performance results.

-benchmem

By default, the performance test results print only the number of runs, each time-consuming operation. -Benchmem can be printed using the number of bytes allocated to each operation, the number of objects assigned to each operation.

// do not use -benchmem

BenchmarkMakeSliceWithoutAlloc-4 2000 971191 ns / at

// use -benchmem

BenchmarkMakeSliceWithoutAlloc-4 2000 914550 ns / 4654335 on B / 30 ALLOCS / in

Here, the meaning of each operation into the operation cycle, as shown in the following example:

func BenchmarkMakeSliceWithoutAlloc(b *testing.B) {

for i := 0; i < b.N; i++ {

gotest.MakeSliceWithoutAlloc () // a single operation

}

}

Published 75 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43778179/article/details/104802892