Go Language Testing test

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The go language provides automated testing capabilities through the testing package. In-Package testing you can automatically run test functions that match the rules as long as you run the command go test.
Go Language Test Convention rules
1. General test func testxxx (*testing. T
Test line must start with test, XXX is string, first x must be uppercase [A-Z] Caption
In order to test the method and the readability of the method being tested, general xxx is the function name of the method being tested.

2. Performance test func benchmarkxxx (*testing. B
Performance test with benchmark mark, xxx ibid.

3. test File name conventions
Go language test file name convention rules must end in _test.go, placed under the same package, in order to facilitate code reading, the general go source file plus _test
such as source file My.go so test file if Your_test.go,her_test.go,my_test.go can, but the best is my_test.go, easy to read

example, source file My.go

Package MyFunc Add (x, y int) int {return x + y}

Create a My_test.go file that needs to be introduced testing

Package Myimport "Testing" func Testadd (t *testing. T) {If Add (1, 2)! = 3 {t.error ("test foo:addr failed")} else {T.Log ("Test foo:addr Pass")}}func Benchmarkadd (b *testing. B) {//If initialization is required, the more time-consuming operation can be this://B.stoptimer ()//.... A bunch of Operations//B.starttimer () for I: = 0; i < B.N; i++ {Add (1, 2)}}

Run test go, output:

PASS
OK github.com/my 0.010s
To run a performance test, execute the command
Go test-test.bench= ". *"
Output
PASS
Benchmarkadd 2000000000 0.72 Ns/op
OK github.com/my 1.528s

More test names, use Go help test
Go test is not very good very strong! ^_^

article source:  http://blog.gcove.net/go%E8%AF%AD%E8%A8%80%E6%B5%8B%E8%AF%95test.html

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.