This is a creation in Article, where the information may have evolved or changed.
Read the unit tests on Golang on the go official GitHub, and now summarize the key points.
Single Test
- The file name must be the end of _test.go so that it executes to the appropriate code when the go test is executed.
- You have to import testing this bag.
- All test case functions must be test start tests are executed in the order in which they were written in the source code
- Test format: Func testxxx (t *testing. T), the xxx section can be any combination of alphanumeric, but the first letter cannot be a lowercase letter [a-z], for example testintdiv is the wrong function name.
- function, by calling testing. T's error, Errorf, Failnow, Fatal,fatalif method, indicates that the test does not pass, and calls the log method to log the information of the test.
As an example,
PackageGotestImport("Testing")funcDivision (A, Bfloat64) (float64, error) {ifb = =0{return0, errors. New ("Divisor cannot be 0") }returnA/b,Nil}funcTest_division_1 (t *testing. T) {ifI, E: = Division(6,2); I! =3|| E! =Nil{//Test functionT.error ("The Division function test did not pass")//If the error is not as expected}Else{T.Log ("The first Test passed.")//Record some information you wish to record}}funcTest_division_2 (t *testing. T) {T.error ("Just not through.")}
We execute go test below the project directory and the following information is displayed
-fail:test_division_2 (0.00 seconds)
Gotest_test.go:16: Just don't Pass
FAIL
Exit Status 1
FAIL gotest 0.013s
Pressure test
Stress tests are used to detect the performance of a function (method), similar to the method of writing unit functional tests.
- The stress test case must follow the following format, where xxx can be a combination of any alphanumeric, but the first letter cannot be a lowercase letter
Func benchmarkxxx (b *testing. B) {...} go
- Test does not default to the function of the stress test, if you want to perform a stress test with parameter-test.bench, syntax:-test.bench= "Test_name_regex", such as Go test-test.bench= ". *" Means testing all the stress test functions
- In the stress test case, remember to use testing in the loop body. B.N to allow the test to run normally
- The file name must also end with _test.go
Example
PackageGotestImport("Testing")funcBenchmark_division (b *testing. B) { forI: =0; i < B.N; i++ {//use B.N for loopingDivision(4,5) }}funcBenchmark_timeconsumingfunction (b *testing. B) {B.stoptimer ()//Call this function to stop the time count of the stress test //Do some initialization work, such as reading file data, database connection, etc. //So these times do not affect the performance of our test function itselfB.starttimer ()//re-start time forI: =0; i < B.N; i++ {Division(4,5) }}
We execute the command go test-file webbench_test.go-test.bench= ". *", you can see the following results:
PASSBenchmark_Division 500000000 7.76ns/opBenchmark_TimeConsumingFunction500000000 7.80ns/opok gotest 9.364s
The above results show that we did not perform any TESTXXX unit test functions, the results show only the pressure test function, the first shows that the benchmark_division executed 500 million times, The average execution time for each time is 7.76 nanoseconds, and the second shows that the benchmark_timeconsumingfunction executes 500000000, and the average execution time for each time is 7.80 nanoseconds. The last bar shows the total execution time.
GItHub Original Address: https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/11.3.md