Golang Test uses

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

1. Create a test folder MySQL, the package for the go file under the folder must match the folder name (otherwise it will not be recognized)

2. Create the file to be tested Mysql.go (use Github.com/go-sql-driver/mysql package)

Package Mysqlimport (    "Database/sql"    _ "Github.com/go-sql-driver/mysql") func findbypk (pk int) int {    var num int = 0    db, err: = SQL. Open ("MySQL", "Root: @tcp (127.0.0.1:3306)/plugin_master?charset=utf8")    if err! = Nil {        Panic (err. Error ())    }    defer db. Close ()    stmtout, err: = db. Prepare ("Select ID from t_admin where id=?")    If err! = Nil {        Panic (err. Error ())    }    defer stmtout.close ()    err = Stmtout.queryrow (PK). Scan (&num)    if err! = Nil {        Panic (err. Error ())    }    return num}

View Code

3. Create the Unit test case file Mysql_test.go (the file name must be of type *_test.go, * represents the file name to be tested, the function name must start with test such as: Testxxx or test_xxx)

Package Mysqlimport (    "testing") Func TEST_FINDBYPK (t *testing. T) {    num: = FINDBYPK (1)    t.Log (num)}

View Code

Testing all the files go test will compile all the *_test.go files in the current directory and run the tests automatically.
Test a file using the "-file" parameter. Go test–file *.go. For example: Go test-file mysql_test.go, "-file" parameter is not necessary, can be omitted, if you enter Go Test b_test.go will also get the same effect.
Test a method Go test-run= ' test_xxx ' "-V" parameter go test-v ... Indicates that the result is displayed regardless of whether the use case is tested or not, and no "-V" means that only the failed use case results are displayed

4. Create the benchmark performance test case file Mysql_b_test.go (the file name must be of type *_b_test.go, * represents the file name to be tested, the function name must start with benchmark such as: BENCHMARKXXX or benchmark _XXX)

Package Mysqlimport (    "testing") func BENCHMARK_FINDBYPK (b *testing. B) {for    I: = 0, i < B.N; i++ {//use b.n for looping        findbypk (1)    }}

View Code

Benchmark test Go test-bench= ". *" or go test for all go files. -bench= ". *"
A go file is benchmark tested go Test mysql_b_test.go-bench= ". *"

5. Generating a CPU status graph with performance tests (not currently tested for use)

Use the command:
Go test-bench= ". *"-cpuprofile=cpu.prof-c
Cpuprofile is the CPU profile that represents the build
-C is the generation of an executable binary, which is required to generate a state diagram, which generates an executable file in this directory Mysql.test
Then use the Go tool pprof tools
Go tool pprof Mysql.test cpu.prof
Call the Web (you need to install Graphviz) to generate an SVG file, and then use your browser to view the SVG file reference http://www.cnblogs.com/yjf512/archive/2013/01/18/2865915.html
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.

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.