The Go Language test

Source: Internet
Author: User
Tags how to write test cases
This is a creation in Article, where the information may have evolved or changed.

Read a book before, said: "All the great God is to write the unit test cases before they write code." I keep it in mind all the time. Today, at last, I looked at Golang's test package testing .

Xie Da's book and Golang Official document, Golang provides two test methods: use case test and stress test.

# # #1. Use case Testing

The rules for use-case testing I was copying Xie da:

  • The file name must be _test.go at the end so that it executes go test to the appropriate code at execution time
  • You have to import testing this bag.
  • All test case functions must be at the Test beginning
  • The tests are executed in the order in which they were written in the source code.
  • The parameter of the test function TestXxx() is testing.T that we can use that type to log an error or test state
  • Test format: func TestXxx (t *testing.T) , Xxx part can be any combination of alphanumeric, but the first letter can not be lowercase letters [a-z], such as the Testintdiv wrong function name.
  • The function in the call,,, testing.T Error Errorf FailNow Fatal FatalIf method, means that the test does not pass, and the calling Log method is used to record the test information.

As an example of the sort of heap I've written before, the test needs to be in package units. For Golang, the folder is the unit. You need to put the heap sort file quicksort.go and the test file quicksort_test.go together.

Verify the heap sequencing test case:

func TestHeapSort(t *testing.T) {test0 := []int{49, 38, 65, 97, 76, 13, 27, 49}test1 := []int{13, 27, 38, 49, 49, 65, 76, 97}heapSort(BySortIndex(test0), 0, len(test0))for i := 0; i < len(test0); i++ {if test0[i] != test1[i] {t.Fatal("error")}}}$ go test -v

# # #2. Pressure test

Use case testing is used testing.T , and stress testing is required testing.B . Its prototype is as follows:

type B struct {commonN                intpreviousN        int           // number of iterations in the previous runpreviousDuration time.Duration // total duration of the previous runbenchmark        InternalBenchmarkbytes            int64timerOn          boolshowAllocResult  boolresult           BenchmarkResultparallelism      int // RunParallel creates parallelism*GOMAXPROCS goroutines// The initial states of memStats.Mallocs and memStats.TotalAlloc.startAllocs uint64startBytes  uint64// The net total of this test after being run.netAllocs uint64netBytes  uint64}

There is a property inside this structure that N represents the number of times the stress test was performed. You can b.N = 1234 set the number of pressures by.

func BenchmarkHeapSort(b *testing.B) {b.StopTimer()  //调用该函数停止压力测试的时间计数b.StartTimer() //重新开始时间b.N = 1234for i := 0; i < b.N; i++ {test0 := []int{49, 38, 65, 97, 76, 13, 27, 49}test1 := []int{13, 27, 38, 49, 49, 65, 76, 97}heapSort(BySortIndex(test0), 0, len(test0))for i := 0; i < len(test0); i++ {if test0[i] != test1[i] {b.Fatal("error")}}}}$ go test -v -test.bench=".*"=== RUN TestHeapSort--- PASS: TestHeapSort (0.00 seconds)PASSBenchmarkHeapSort           1234              1620 ns/opok      go_code/test    0.051s

Here I'm just trying to write a little bit. In the future write code, I have to like the great God to write use cases. We encourage each other, hahaha.

Please refer to the complete source code in this article.

###### References + "1" go how to write test Cases-Astaxie

The original link: Go language test, reproduced please indicate the source!

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.