This is the 9th day of my participation in Gwen Challenge

If ❤️ my article is helpful, welcome to like, follow. This is the biggest encouragement for me to continue my technical creation. More articles on my blog

Hand on hand to teach you to write Golang unit tests

No nonsense, first code for respect

Let’s start with simple expressions, so let’s take an example:

Default required business files (To be testedThe script)

Write the correspondingThe test script

Then go to the package directory path you want to test and run the go test command

$ go test-v === RUN TestMult -- PASS: TestMult (0.00s) PASS OK demo 0.006sCopy the code

All test cases in the current path (file names ending in _test.go) will be started; Add the -v parameter to view the detailed running information about a test case.

This is a simple but complete unit (functional) test case.

Golang test command

Go test [build/test flags] [packages] [build/test flags & test binary flags]

The go test command will automatically match the package test file named *_test.go in the specified path (default: current path), which will be compiled into a separate package, linked and run with the main test binary.

How to set ignore folders and files

The name can be _ (including _test.go) or. Initial files will be ignored. The go tool will ignore the directory named TestData, making it available to hold auxiliary data needed for testing.

Go two different test modes

Local Directory mode

Local directory mode occurs when go test is invoked without package arguments (for example, ‘go test’ or ‘go test -v’).

In this mode, Go Test compiles the package source and finds the tests in the current directory, then runs the resulting test binary. In this mode, caching (discussed below) is disabled. When the package test is complete, Go Test prints a summary line showing the test status (‘ OK ‘or ‘FAIL’), the package name, and the elapsed time.

Packet list mode

Package list pattern occurs when go test is called with explicit package arguments (e.g. “Go test math”, “Go test./…”) “Or even” Go test.” ). In this mode, Go Test compiles and tests each package listed on the command line.

  • If a package test passes, go Test prints only the final “OK” summary line.
  • If the package tests fail, Go Test prints the complete test output.
  • If called with the -bench or -v flag, go test prints the complete output and even passes the package test to show benchmark results or detailed logging of the request.

Package test cache

Go Test caches successful package tests only in package list mode, avoiding unnecessary rerunning of tests. The results of the test can be recovered from the cache, and Go Test redisplays the previous output instead of running the test binary

Once again. When this happens, go Test prints ‘(cached)’ instead of the spent time in the digest line. The test results will be cached if the command has the following parameters: -cpu, -list, -parallel, -run, -short, and -v. A common way to explicitly disable test caching is to use -count=1.

Why testing is needed

Golang comes with its own Testing package, which automatically completes unit testing, performance testing, and verification results. The complete unit test in the project development process can greatly improve the efficiency of finding, reproducing and locating problems in common business scenarios such as function launching and new environment deployment. Especially in large and complex projects, it is an effective means to reduce the occurrence of online faults and anomalies.

Test file writing essentials

  • The test case files are executed using the go test command. There is no need for the main() function as an entry point in the source code. All functions starting with test in the source code files ending with _test.go are automatically executed.

Test type, test file writing points

The Golang Testing package provides three types of testing: unit (functional) testing, performance (stress) testing, and coverage testing.

Common writing points are:

  • The test file name must be_test.goAt the end
  • The testing package needs to be imported into the test fileimport "testing"
  • The test file can be provided byMultiple test cases (functions)
  • The test fileWill not be involved inNormal source compilation is not included in the executable file

Unit (functional) testinggo test -v

Unit tests start with Test, (t * testing.t) as a parameter, followed by an uppercase letter (for example, TestAbc())

Performance (pressure) testgo test -bench="."

Performance tests start with (t * testing.b) and must begin with a capital letter (for example, TestAbc()).

Coverage testinggo test -cover

Coverage tests tell you how much business code is covered by the test application (that is, how much code is tested in Demo_test.go), preferably 100% coverage.

$ go test- Cover PASS Coverage: 100.0% of statements OK demo 0.006sCopy the code