Go language-Test Cases

Source: Internet
Author: User

Writing Unit Test Cases

    • File name must end With ' _test.go '
    • Must import "testing" this package
    • The tests are executed in the order in which they were written in the source code.
    • Test the function ' testxxx (t *testing. T) ' parameter is ' testing. T ', we can use this type to log errors or test status
    • Test format: ' Func testxxx (t *testing. T) ', ' Xxx ' section can be any combination of alphanumeric, but the first letter cannot be a lowercase letter [a-z]
    • function, by calling ' testing. T ' Error ', ' Errorf ', ' failnow ', ' Fatal ', ' fatalif ' method, indicating that the test case does not pass, call ' log ' method to record the information of the test

Write stress 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.a) {...}
    • Go test does not perform the function of the stress test by default, and takes parameters if you want to perform a stress test. -test.bench, Syntax:-test.bench= "Test_name_regex", such as Go test-test.bench= ". *" means testing all the stress test functions
    • In stress test cases, it is necessary to use testing in the circulation body. B.N to enable test cases to run properly
    • The file name must also end with _test.go

Unit Test Cases

Source

    package gotest    import(    )    func Add(a, b float64) float64 {        return a +  b    }

Test Cases

package gotestimport (    "testing")func Test_Add_1(t *testing.T) {    if i := Add(6,2); i != 8 {        t.Error("fail")    } else {        t.Log("pass")    }}func Test_Add_2(t *testing.T) {    if i := Add(6,2); i == 8 {        t.Log("pass")    } else {        t.Error("fail")    }}

Run test Cases

    go test -v

Test Case Execution Results

=== RUN   Test_Add_1--- PASS: Test_Add_1 (0.00s)    gotest_test.go:11: pass=== RUN   Test_Add_2--- PASS: Test_Add_2 (0.00s)    gotest_test.go:17: passPASSok      github.com/shadowsocks/shadowsocks-go/sample-config/gotest  0.001s

Stress test Cases

Source

package gotestimport(    "testing")func Benchmark_Add(b *testing.B) {    for i := 0; i < b.N ; i++ {        Add(3,4)    }}func Benchmark_TimeConsumingFunction(b *testing.B) {    b.StopTimer() //调用该函数停止压力测试的时间计数    //做一些初始化工作,这些时间不影响我们测试函数本身的性能    b.StartTimer()    for i := 0; i < b.N; i++ {        Add(5,6)    }}

Run results

Benchmark_Add-4                         2000000000           0.38 ns/opBenchmark_TimeConsumingFunction-4       2000000000           0.38 ns/opPASSok      github.com/shadowsocks/shadowsocks-go/sample-config/gotest  1.594s第一条显示Benchmark_add-4执行了20亿次,每次执行时间为0.38纳秒第二条也一样

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.