16 Pruebas de software

1. Pruebas unitarias

Según go testel comando, en el directorio del paquete, todos los archivos de código fuente test.gocon el sufijo son go testparte de la prueba

tipo Formato efecto
función de prueba El nombre de la función tiene el prefijo Prueba Probar si algún comportamiento lógico del programa es correcto
función de referencia El nombre de la función tiene el prefijo Benchmark Rendimiento de la función de prueba
función de ejemplo El nombre de la función tiene el prefijo Ejemplo Proporcione documentación de ejemplo para la documentación.

1. Prueba de rutina

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. Grupo de prueba

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. Subpruebas

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. Prueba comparativa

Un método para probar el rendimiento del programa bajo cierta carga.

package calc

import (
	"testing"
)

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

go test -bench=AddResultado de salida:

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. Ajuste de rendimiento

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 utiliza:

# 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)

extensión:

1. La graphvizherramienta de instalación se puede mostrar gráficamente

2. go-torch y FlameGraph pueden dibujar gráficos de llama

Herramienta de medición de presión 3.wrk

Supongo que te gusta

Origin blog.csdn.net/pointerz_zyz/article/details/123649036
Recomendado
Clasificación