问题
How do I make use of the -short
flag given in go test -short
?
Is it possible to combine the -short
and -benchmark
flags?
I am quite new to the Go language but I am trying to adapt myself to some of its common practises. Part of this is to try and ensure my code has not only Unit Tests added in a way that the go test
system works but that go test -benchmark
also functions in a useful manner.
At the moment I have a benchmark test that includes a series of sub-tests based on varying sized of input data. Running the 15 permutations takes a long time, so it would be nice to given an option of shortening that test time.
The next set of tests I plan to write could include a series of data input examples. I expect running a single one of these could work as a sanity check for a short test but having the option to run several on longer (or normal) test runs would be good.
When I look at the GoLang documentation for the testing flags it says "Tell long-running tests to shorten their run time." which sounds like what I want, but I cannot work out how to pick up this flag in the test code.
回答1:
How do I make use of the -short flag given in go test -short?
Using the short flag on the command line causes the testing.Short()
function to return true
. You can use this to either add test cases or skip them:
if testing.Short() == false {
// Extra test code here
}
The above is perhase a little unusal, it may be more common to see:
func TestThatIsLong(t *testing.T) {
if testing.Short() {
t.Skip()
}
}
Be sure to have enough test cases for your -short
runs to do at least a bare minimal check. Some people suggest using the -short
run for the main continuous integration and pre-commit checks while keeping the longer test runs for scheduled daily or weekly builds or merges.
The Documents section of The Go Programming Language site mentions how to write test code briefly but the bulk of information about the topic is in the Package Documentation for the Go Testing Package. For most topics, the majority of documentation will be found in the package rather than separate. This may be quite different from other languages where the class and package documents are often poor.
Is it possible to combine the -short and -benchmark flags?
It is possible as the testing.Short()
is global in scope. However, it is recommended that Benchmark tests do not make extensive use of the -short
flag to control their behaviour. It is more common for the person running the benchmarking to alter the -benchtime
permitted for each benchmark test case.
By default, the benchtime is set to one second. If you have 60 benchmark test cases, it will take at least sixty seconds to complete the run (setup time + execution time). If the benchtime is set to less:
go test -benchmem -benchtime 0.5s -bench=. <package_names>
the overall execution time will go down proportionality.
The various testing clags are described in the go command documentation's Testing Flags section (not the package documentation which does not mention benchtime). You do not need to do anything different in your benchmark code to have the benchtime be effective, just use the standard for i := 0; i < b.N; i++ {
and the framework will adjust the value of N as needed.
Although it is not recommended, I have used -short
within a benchmark to reduce the number of test cases when varying input to a function to give an indication of its Big O notation. For all benchmark runs (-short and normal), I keep a representative data size for the input to track long term trends. For the longer runs, I include several small and larger sized data sets to allow an approximation of the functions resource requirements. As with the unit test cases I choose to run the -short version always in the CI and the longer version on a schedule.
Whatever your questions are with Go, it is strongly recommended that you try thoroughly reading both the https://golang.org/doc/ and relevant https://golang.org/pkg/ documents. Frequently the most useful documentation is in the package documentation.
来源:https://stackoverflow.com/questions/55180613/how-do-i-write-test-in-go-that-make-use-of-the-short-flag-and-can-it-be-combine