Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Unit Test and performance Test of Go language

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Go language unit test and performance test sample analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Go language unit test and performance test sample analysis"!

Overview

Testing is not unique to Go, it is actually tested in many languages, such as Go, Java, Python...

To be a qualified Daniel, this is a skill that programmers must possess, especially in some big companies, which is a bonus, with the following advantages:

The code can be tested at any time to ensure that the code will not produce errors

Write more efficient code

Unit testing

Format: func TestXXX (t * testing.T)

/ / add.gopackage calfunc Add (num1, num2 int) int {return num1 + num2} / / mul.gopackage calfunc Mul (num1, num2 int) int {return num1 * num2} / / add_test.gopackage calimport ("testing") func TestAdd (t * testing.T) {sum: = Add (10,20) if sum! = 30 {t.Log ("10mm 20 =") Sum)} func TestMul (t * testing.T) {sum: = Mul (10,2) if sum! = 20 {t.Error ("10 * 2 =", sum)}}

The running results are as follows:

/ / Unit test command line $go test add_test.go add.go mul.gook command-line-arguments 0.072s $go test-v add_test.go add.go=== RUN TestAdd--- PASS: TestAdd (0.00s) = RUN TestMul--- PASS: TestMul (0.00s) PASSok command-line-arguments 0.070s// run the specified unit test case $go test-v-run TestAdd add_test.go add.go mul.go=== RUN TestAdd- -- PASS: TestAdd (0.00s) PASSok command-line-arguments 0.072s code is described as follows

Line 1, followed by the add_test.go component after go test, indicates that all test cases in this file are tested.

Line 2 shows the test results, the ok test passed, a package name to be used for the command-line-arguments use case, and 0.069s indicates the time spent on the test.

Line 3 shows that-v has been added to the additional parameters to show the detailed flow during the test.

Lines 4-7 indicate the start of a run called the TestAdd TestMul use case.

Lines 4-7 indicate that the TestAdd TestMul trial case has been run, and PASS indicates that the test is successful.

Line 10, indicating that running the specified unit test case mainly adds-run followed by the function you need to test (TestAdd)

problem

Ok command-line-arguments (cached)

First: clear the cache go clean-testcache

Second: go test sets flags go test add_test.go add.go-count=1

Be careful

Each test file must end with _ test.go, otherwise go test cannot find the test file

Test files are written in the same package to facilitate testing and later maintenance

Each test file must be imported into the testing package

Functional test functions must start with Test, followed by the name of the test function

Testing.T provides several log output methods

The method describes Log printing log, ending test Logf formatting printing log, ending test Error printing error log, ending test Errorf formatting printing error log, ending test Fatal printing fatal log, ending test Fatalf format printing fatal log, and ending test Fatalf format printing fatal log.

Direct printing: Log Error Fatal

Formatted printing: Logf Errorf Fatalf

Basic use of performance testing

Format: func BenchmarkXxx (* testing.B)

/ / benchmark_test.go package cal import ("fmt"testing") func BenchmarkHello (b * testing.B) {for I: = 0; I < b.N; iTunes + {fmt.Sprintf ("hello")}}

The test results are as follows:

$go test-v-bench= ". Benchmark_test.gogoos: windowsgoarch: amd64BenchmarkHello-4 20000000 93.7 ns/opPASSok command-line-arguments 2.061s

The code is as follows:

Line 1-bench= "." It means to run all the tests in the benchmark_test.go file, which is actually the same as-run [- bench regexp can receive a regular, if you want to run all the benchmark tests, use-bench. Or-bench=.'.]

Line 2 goos indicates that the system is windows

Line 3 goarch indicates that the operating system architecture is amd64

Line 4 BenchmarkHello-4 indicates the name of the test, 20000000 tests, and 93.7 ns/op indicates how long each operation took (nanoseconds)

Custom test time

You can customize the test time with the-benchtime parameter, for example:

$go test-v-benchtime=2s-bench=. Benchmark_test.gogoos: windowsgoarch: amd64BenchmarkHello-4 30000000 85.1 ns/opPASSok command-line-arguments 2.714s

When you do not set-benchtime t, the default value is 1 second

Testing.B provides several methods [testing.B has all the interfaces of testing.T]

Method description StartTimer () start timing StopTimer () stop timing ResetTimer reset timing SetBytes () set processing bytes ReportAllocs () report memory information runN (n int) run a benchmark function

There are many flags expressions in go test, such as:-parallel n-cover-vet list-parallel n …

Thank you for reading, the above is the content of "Go language unit test and performance test sample analysis". After the study of this article, I believe you have a deeper understanding of the problem of Go language unit test and performance test sample analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report