Unit testing

Unit testing is defined in Baidu Encyclopedia as the examination and verification of the smallest testable units in the software. For the meaning of unit in unit test, generally speaking, to determine its specific meaning according to the actual situation, such as C language unit refers to a function, Java unit refers to a class, graphical software can refer to a window or a menu, etc.. In general, a unit is the smallest artificially defined functional module under test. Unit testing is the lowest level of testing activity to be performed during software development, where individual units of software are tested in isolation from the rest of the program.

Go unit testing tool

Tests in the Go language rely on the Go test command. The process of writing test code is similar to writing regular Go code, with no new syntax, rules, or tools to learn.

The go test command is a driver that tests code by convention and organization. In the package directory, all source files with the suffix _test.go are part of the Go test test and will not be compiled into the final executable by the Go build.

There are three types of functions in the *_test.go file, unit test functions, benchmark functions, and sample functions. Test functions: function names prefixed with Test; A Benchmark function is used to test the logical behavior of a program. Function: The function name is prefixed with Example. The go test command iterates through all the functions in the *_test.go file that conform to the above naming conventions, generates a temporary main package that calls the corresponding test function, builds and runs it, reports the test results, and finally cleans up the temporary files generated during the test. If you don’t understand, refer to Python’s PyTest.

Golang unit test file name and method name specification: 1. The file name must be named xx_test.go, 2. The method must start with Test[^a-z]. 3. The method parameter must be t *testing. Unit test with Go test

Go Test is a test tool of go language, which contains two types, unit test and performance test

Go help test go help test

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

Parameter interpretation:

-c: Compiles go test into an executable binary, but does not run the test.

-i: installs the package on which the test package depends, but does not run the test.

For build Flags, call Go Help Build. These are the parameters used during the build run and are generally set to null

For Packages, call Go Help Packages. These are about managing packages and are generally set to empty

For flags for test binary, call Go Help testFlag. These parameters are often used during go test

-test.v: indicates whether to output all unit test cases (whether they succeed or fail). The value is not added by default, so only the failed unit test cases are output.

-test.run pattern: Indicates which unit test cases are run only

-test.bench patten: Runs only those performance test cases

-test.benchmem: Specifies whether to output memory information during performance tests

-test.benchtime t: performance test run time. The default value is 1s

-test.cpuprofile cpu.out: indicates whether to output CPU performance analysis files

-test.memprofile mem.out: indicates whether to output memory performance analysis files

-test.blockprofile block.out: specifies whether to output performance analysis files blocked by internal Goroutine

-test.memprofilerate n: Records the amount of memory allocated during memory performance analysis. This parameter sets the memory allocation interval, that is, the memory size represented by sample in the profile. The default value is 512 x 1024. If you set it to 1, each memory block allocated will have a dot in the profile, and the resulting profile will have a lot of samples. If you set it to 0, you’re not doing any typing.

You can turn off memory reclamation by setting memprofilerate=1 and GOGC=off, and observe how each block is allocated.

-test.blockprofilerate n: Controls the number of nanoseconds that a Goroutine blocks. Blockprofilerate =1 if the default value is not set to -test.blockprofilerate=1 and is logged every nanosecond

-test.parallel: indicates the number of parallel cpus for a performance test. The default value is GOMAXPROCS.

-test.timeout t: If the test case runs for longer than t, panic is thrown

-test. CPU 1,2,4: the CPU on which the program is running is represented by the binary 1 bit, the same as nginx’s nginx_worker_CPU_affinity

-test.short: Shortens the running time of test cases that take a long time

The benchmark

Benchmarking is a way to test application performance under a certain workload.

A Benchmark test is prefixed with a *testing.B parameter B. The Benchmark test must be executed for b.N times, so that the test can be compared.