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, Very simple to use, the test code only needs to be placed in the file ending with ' _test.go '. Golang tests are divided into unit tests and performance tests, test cases with unit tests begin with ' test ', performance tests begin with ' Benchmark ' # # # For example, to achieve the unit test and performance test of the permutation combination function # # # # # # # Implementation of the permutation combination function ' 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} "# # # to implement unit test and Performance test '//Combination_test.gopackage Hmathimport (" Math/rand "" testing ") Unit test//test global function to TestFunction name//test class member function to Testclass_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}, {10, 1, 10}, {10, 3, 120}, {10, 7, 120},} {//Call permutation combination function, match the expected result, if inconsistent output error if actually : = Combination (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 test ' go ' combination_test.go combination.go # unit tests go Test--cover combination _test.go combination.go # Unit test coverage Go test-bench=. Combination_test.go Combination.go # Performance Test "# # # Setup and Teardownsetup and teardown are the actions that need to be performed before and after each case, Golang no direct implementation, can By using this method to achieve the global setup and teardown, the setup and teardown of each case need to implement their own "Func Testmain (M *testing. M) {//Setup code ... os. Exit (M.run ())//teardown code ...} "# # # Goconvey This third-party tool will automatically help us run the test, and with a very friendly visual interface to help us show the results of the test, including the reasons for the test failure, test coverage and so on, the internal also provides a lot of friendly assertions, can improve the readability of the test code and use method" Go get Github.com/smartystreets/goconvey "then use the terminal to run the ' goconvey ' command in the directory under test code to test the example ' 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 is greater by one", func () {So (x, Shouldequal, 2)})})} "# # # Reference Link-Go testing: < http://docs.studygolang.com/pkg/testing/>-Goconvey:
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.