Examples of Go language functional testing and performance testing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. F:./SRC
└─hello
Hello.go

Hello_test.go


Hello.go

=========

Package Hello

Import"FMT"
FuncAdd (A, b int) (result int) {
  returnA + b
}
FuncMain (){
  FMT. Println ("Hello, World.  ") Hello, world! ")
  C: = ADD (1, 2)
  FMT. Println ("c=", c)
}

Hello_test.go

Package Hello

Import(
  "FMT"
  "Testing"
  "Time"
)
FuncTestAdd1 (t *testing. T) {
  FMT. Println ("Add Test")
  r: = ADD (1, 2)
  ifr! = 2 {//  this should be 3, deliberately changed to 2 test error scenario
  T.errorf ("Add (1, 2) failed. Got%d, expected 3. ", R)
    }
  FMT. PRINTLN ("Other test Tests")//Add additional tests
}
FuncBenchmarkAdd1 (b *testing. B) {
  FMT. Println ("Perform the Add Performance test")
  b.stoptimer ()  //Pause Timer
 time . Sleep (3)  //A lengthy preparatory work, such as reading a file
  B.starttimer ()//Turn on the timer, before the preparation time is not counted in the total time spent
For i: = 0; i < B.N; i++ {   
  Add (1, 2)
    }
   Fmt. PRINTLN ("Other test Tests")//Add additional tests
}
You can compile and run directly from the menu directly under Liteide.
The following is the form of the command line

C:/go/bin/go.exe test [F:/golang/test/src/hello]

C:/go/bin/go.exe test-test.bench=.* [F:/golang/test/src/hello]


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.