golang 示例测试example_Golang基础入门11 | Testing
软件测试
编写软件,在发布软件之前或之后都可能发现错误。 软件中发生的错误可能是错误,漏洞和其他类似的东西。 可以通过测试软件本身来发现软件中的许多潜在错误。 软件测试不仅要确保软件的正确性,而且还要确保软件符合用户的要求。
Go中的测试
在Go中测试软件可以通过创建扩展名为_test.go的文件来完成。 该文件必须与需要测试的对象位于同一程序包中。 要在测试文件中创建测试,请使用Test ..(t * testing.T)创建一个函数。 要运行测试,请使用go test命令。
// main.go
package main
import "fmt"
func main() {
fmt.Println("The result of 12 14 = ", sum(12, 14))
}
//create a sum function
func sum(x, y int) int {
return x y
}
// main_test.go
package main
import "testing"
//create a test
func TestSum(t *testing.T) {
result := sum(12, 14) //get the result
expected := 26
if result != expected {
t.Error("Expected", expected, "Got", result)
}
}
// output (go test)
// PASS
// ok review-again/uji 4.023s
下面是个测试失败的例子
// main_test.go
package main
import "testing"
func TestSum(t *testing.T) {
result := sum(12, 14)
expected := 36 //change the expected value
if result != expected {
t.Error("Expected", expected, "Got", result)
}
}
// output (go test)
// --- FAIL: TestSum (0.00s)
// main_test.go:9: Expected 36 Got 26
// FAIL
// exit status 1
// FAIL review-again/uji 4.670s
可以通过创建包含测试用例和预期结果的自定义struct来自定义测试。
package main
import "testing"
func TestSum(t *testing.T) {
//create a custom struct
type testSample struct {
data1 int
data2 int
answer int
}
//create a testcases that consist of testSamples
testCases := []testSample{
testSample{12, 14, 26},
testSample{5, 5, 10},
testSample{45, 45, 90},
}
//run a test for each test case
for _, v := range testCases {
result := sum(v.data1, v.data2)
if result != v.answer {
t.Error("Expected: ", v.answer, "Got: ", result)
}
}
}
// output (go test)
// PASS
// ok review-again/uji 4.055s
Benchmarking in Go
基准测试基本上是衡量软件的性能,以确保可以有效地使用软件。 Go中的基准测试可以通过使用Benchmark ...(b * testing.B)创建一个函数来完成。
这是Go中基准测试的示例,在这种情况下,SquareRoot()和AnotherSquareRoot()函数用于基准测试示例。
// main.go
package simplesqrt
import "math"
//SquareRoot returns square root of number
func SquareRoot(f float64) float64 {
return math.Pow(f, 0.5)
}
//AnotherSquareRoot return square root of number using math.Sqrt()
func AnotherSquareRoot(f float64) float64 {
return math.Sqrt(f)
}
// main_test.go
package simplesqrt
import (
"fmt"
"testing"
)
//The usage of SquareRoot Function
func ExampleSquareRoot() {
fmt.Println("The result of square root of 16 = ", SquareRoot(16))
//Output: The result of square root of 16 = 4
}
//Benchmark for SquareRoot() function
func BenchmarkSquareRoot(b *testing.B) {
for i := 0; i < b.N; i {
SquareRoot(16)
}
}
//Benchmark for AnotherSquareRoot() function
func BenchmarkAnotherSquareRoot(b *testing.B) {
for i := 0; i < b.N; i {
AnotherSquareRoot(16)
}
}
// output (go test -bench .)
// goos: windows
// goarch: amd64
// pkg: review-again/simplesqrt
// BenchmarkSquareRoot-4 200000000 7.63 ns/op
// BenchmarkAnotherSquareRoot-4 2000000000 0.44 ns/op
// PASS
// mok review-again/simplesqrt 9.401s
根据输出,有两个基准结果:
- BenchmarkSquareRoot-4已完成2亿次操作,每次操作的速度为7.63纳秒
- BenchmarkAnotherSquareRoot-4已完成20亿次操作,每次操作的速度为0.44纳秒
资料来源
- Go单元测试文档
这是golang基本教程系列的最后一部分,我希望本文对帮助学习Go编程语言有所帮助。 如果您有任何想法或反馈,可以在下面的评论留言。
关注公众号【技术全沾】学习更多有趣的编程知识。
Golang基础入门系列:
- 简介
- 常量
- 条件选择
- 循环
- Array,Slice 和 Map
- Function
- Struct 和 Interface
- 错误处理
- 协程
- Channel
- Testing
您可能感兴趣的文章: