Golang unittest Unit Test (GO test)

Source: Internet
Author: User
# #Golang unittest Unit Test

Golang unit tests have strict requirements on file names and method names, and parameters.
For example:
1, the file name must be named Xx_test.go
2, the method must be test[^a-z] Start
3, the method parameter must t *testing. T
Before because the 2nd did not write right, led to find a half-day error. Now really memorable, ah, little things were not carefully read.
Let's share a bit of the go Test parameter interpretation below. Source

  

Go test is the go language comes with a testing tool, which contains two categories, unit testing and performance testing

Use the Go help test to see the instructions for using GO test: format:

Go test [-c] [-i] [build flags] [packages] [flags for test binary] parameter interpretation:

-C: Compile go test to be an executable binary, but do not run tests.

-I: Installs the package that the test pack depends on, but does not run the test.

For build flags, call go help build, which is the parameters you need to use during compilation, and is generally set to empty

About packages, call go help packages, these are about package management, generally set to empty

For the flags for test binary, call go Help Testflag, which are the parameters that are frequently used during go test

-TEST.V: Whether to output all of the unit test cases (whether successful or unsuccessful), the default is not added, so only the failed unit test cases are output.

-test.run pattern: Which unit test cases run only

-test.bench Patten: Run only those performance test cases

-test.benchmem: Whether to output memory when performance tests are in progress

-test.benchtime T: Time of performance test run, default is 1s

-test.cpuprofile cpu.out: Whether to output CPU profiling files

-test.memprofile mem.out: Whether to output memory profiling files

-test.blockprofile block.out: Whether to output the performance analysis file for internal goroutine blocking

-test.memprofilerate N: Memory performance analysis When there is an allocation of how many times before the issue of records. This parameter sets the memory allocation interval for the dot, which is the memory size represented by a sample in profile. The default is set to 512 * 1024. If you set it to 1, each allocated block of memory will have a dot in the profile, so there will be a lot of sample generated for the profile. If you set it to 0, you are not doing a dot.

You can turn off memory reclamation by setting memprofilerate=1 and Gogc=off, and observe the allocation of each block of memory.

-test.blockprofilerate N: Basic ibid., which controls the number of nanoseconds to be goroutine when blocking. The default is not set to the equivalent of-test.blockprofilerate=1, every nanosecond to record a bit

-test.parallel N: Number of program parallel CPUs for performance testing, default equals Gomaxprocs.

-test.timeout T: Throws panic if the test case runs longer than t

-TEST.CPU 1,2,4: The program runs on which CPU, using the binary 1 is represented, and Nginx nginx_worker_cpu_affinity is a reason

-test.short: Shorten the run time of those test cases that run longer


above example:


Result output:


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.