Golang Unit Test

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

Unit testing is an important part of quality assurance, good unit testing not only to detect problems in a timely manner, but also to facilitate debugging, improve production efficiency, so many people think that writing unit testing is the need for additional time, will reduce productivity, is the biggest prejudice and misunderstanding unit testing

The Go language natively supports unit testing, which is very simple to use, and the test code only needs to be placed in the _test.go end file. Golang tests are divided into unit tests and performance tests, test cases for unit tests to Test begin with, performance tests to Benchmark begin with

As an example,

Unit testing and performance testing for permutation and combination functions

implementing permutation and combination functions

// combination.gopackage hmathfunc combination(m, n int) int {    if n > m-n {        n = m - n    }    c := 1    for i := 0; i < n; i++ {        c *= m - i        c /= i + 1    }    return c}

Implementing unit Tests and performance tests

Combination_test.gopackage hmathimport ("Math/rand" "testing")//unit test//test global function to TestFunction name//test class member functions to TESTCL Ass_function named Func testcombination (T *testing.        T) {//define a temporary structure here to store the parameters of the test case and the expected return value for _, Unit: = range []struct {m int n int Expected int} {1, 0, 1}, {4, 1, 4}, {4, 2, 6}, {4, 3, 4}, {4, 4, 1}, {1 0, 1, 10}, {10, 3, 120}, {10, 7, 120},} {//Call permutation combination function, match the expected result, if inconsistent output error if actually: = Co Mbination (UNIT.M, UNIT.N); Actually! = unit.expected {T.errorf ("combination: [%v], actually: [%v]", Unit, actually)}}}//performance test Func benchmarkcombination (b *testing. B) {//B.N will take a suitable value depending on the run time of the function for I: = 0; i < B.N; i++ {combination (i+1, Rand. INTN (i+1))}}//concurrency Performance test func benchmarkcombinationparallel (b *testing. B) {//Test an object or function under a multithreaded scenario for safe B.runparallel (func (Pb *testing). PB) {for PB. Next () {m: = RAnd. INTN (+) + 1 N: = rand. INTN (m) combination (M, n)})}

Run Tests

go test combination_test.go combination.go           # 单元测试go test --cover combination_test.go combination.go   # 单元测试覆盖率go test -bench=. combination_test.go combination.go  # 性能测试

Setup and teardown

Setup and teardown are the operations that need to be performed before and after each case execution, Golang does not have a direct implementation, you can implement the global setup and teardown by this method, and the setup and teardown of each case need their own actual Is

func TestMain(m *testing.M) {    // setup code...    os.Exit(m.Run())    // teardown code...}

Goconvey

This third-party tool will automatically run the test for us and show the results of the test in a very friendly visual interface, including the reasons for the test failure, the test coverage, and many more friendly assertions that can improve the readability of the test code.

How to use

go get github.com/smartystreets/goconvey

Then use the terminal to run the command in the test code directory goconvey

Test examples

package package_nameimport (    "testing"    . "github.com/smartystreets/goconvey/convey")func TestIntegerStuff(t *testing.T) {    Convey("Given some integer with a starting value", t, func() {        x := 1        Convey("When the integer is incremented", func() {            x++            Convey("The value should be greater by one", func() {                So(x, ShouldEqual, 2)            })        })    })}

Reference links

    • Go testing:http://docs.studygolang.com/p ...
    • Goconvey:https://github.com/smartystre ...
    • Goconvey Documentation: Https://github.com/smartystre ...
    • Goconvey Standard Assertion: Https://github.com/smartystre ...
Reprint please indicate the source
This article link: http://hatlonely.github.io/20 ...
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.