16 Softwaretests

1. Unit-Test

Je nach go testBefehl sind im Paketverzeichnis alle Quellcodedateien test.gomit dem Suffix go testTeil des Tests

Typ Format Wirkung
Testfunktion Dem Funktionsnamen wird Test vorangestellt Testen Sie, ob ein logisches Verhalten des Programms korrekt ist
Benchmark-Funktion Dem Funktionsnamen wird Benchmark vorangestellt Testen Sie die Funktionsleistung
Beispielfunktion Funktionsnamen mit dem Präfix „Beispiel“. Stellen Sie zur Dokumentation eine Beispieldokumentation bereit

1. Routinetest

package calc

import (
	"testing"
)

func Test1Add(t *testing.T) {
	ret := Add(1, 2) //程序输出结果
	want := 3        //期望结果

	if ret != want {
		t.Errorf("测试用例执行失败![期望值:%v	实际值:%v]\n", want, ret)
	}
}

func Test2Add(t *testing.T) {
	ret := Add(2, 2) //程序输出结果
	want := 4        //期望结果

	if ret != want {
		t.Errorf("测试用例执行失败![期望值:%v	实际值:%v]\n", want, ret)
	}
}

2. Testgruppe

package calc

import (
	"testing"
)

func TestAdd(t *testing.T) {
    
    
	type testCase struct {
    
    
		a   int
		b   int
		ret int
	}

	testGroup := []testCase{
    
    
		testCase{
    
    1, 2, 3},
		testCase{
    
    -1, 2, 1},
		testCase{
    
    0, 2, 2},
	}

	for _, tc := range testGroup {
    
    
		ret := Add(tc.a, tc.b)
		if ret != tc.ret {
    
    
			t.Errorf("测试用例执行失败![期望值:%v	实际值:%v]\n", tc.ret, ret)
		}
	}
}

3. Untertests

package calc

import (
	"testing"
)

func TestAdd(t *testing.T) {
    
    
	type testCase struct {
    
    
		a   int
		b   int
		ret int
	}

	testGroup := map[string]testCase{
    
    
		"case_1": testCase{
    
    1, 2, 3},
		"case_2": testCase{
    
    -1, 2, 1},
		"case_3": testCase{
    
    0, 2, 2},
	}

	for name, tc := range testGroup {
    
    
		t.Run(name, func(t *testing.T) {
    
    
			ret := Add(tc.a, tc.b)
			if ret != tc.ret {
    
    
				t.Errorf("测试用例执行失败![期望值:%v	实际值:%v]\n", tc.ret, ret)
			}
		})
	}
}

2. Benchmark-Test

Eine Methode zum Testen der Programmleistung unter einer bestimmten Last

package calc

import (
	"testing"
)

func BenchmarkAdd(b *testing.B) {
    
    
	for i := 0; i < b.N; i++ {
    
    
		Add(1, 2)
	}
}

go test -bench=AddAusgabeergebnis:

goos: windows
goarch: amd64
pkg: zyz.test.com/calc
cpu: AMD Ryzen 5 5600X 6-Core Processor
BenchmarkAdd-12         1000000000               0.2230 ns/op
PASS
ok      zyz.test.com/calc       0.741s

3. Leistungsoptimierung

func main() {
    
    
	cpufile, err := os.Create("./cpu.pprof")
	if err != nil {
    
    
		fmt.Println(err)
		os.Exit(1)
	}

	pprof.StartCPUProfile(cpufile)
	pprof.WriteHeapProfile(cpufile)

	defer pprof.StopCPUProfile()
	defer cpufile.Close()
}

pprof verwendet:

# go tool pprof .\cpu.pprof
Type: inuse_space
Time: Mar 21, 2022 at 10:57pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top3
Showing nodes accounting for 2787.64kB, 100% of 2787.64kB total
Showing top 3 nodes out of 16
      flat  flat%   sum%        cum   cum%
 1762.94kB 63.24% 63.24%  1762.94kB 63.24%  runtime/pprof.StartCPUProfile
  512.50kB 18.38% 81.63%   512.50kB 18.38%  runtime.allocm
  512.20kB 18.37%   100%   512.20kB 18.37%  runtime.malg
(pprof)

Verlängerung:

1. Das Installationstool graphvizkann grafisch dargestellt werden

2. go-torch und FlameGraph können Flammendiagramme zeichnen

3. Arbeitsdruckmessgerät

Guess you like

Origin blog.csdn.net/pointerz_zyz/article/details/123649036