<10> Go Test Unit Testing

Source: Internet
Author: User

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

    1. The file name must be the end of _test.go so that it executes to the appropriate code when the go test is executed.
    2. You have to import testing this bag.
    3. All test case functions must be test start tests are executed in the order in which they were written in the source code
    4. 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.
    5. 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
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.

    1. 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

    1. 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
    2. In the stress test case, remember to use testing in the loop body. B.N to allow the test to run normally
    3. The file name must also end with _test.go


 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: