Go unit tests and reports

1. Write code

1) punch GoLand, new project named gotest

2) In the new two gotest go file directory, as shown below:

 

Wherein CircleArea.go to calculate the area of ​​a circle go test procedure, as follows:

The gotest Package 

Import "Math"

FUNC GetCircleArea (RADIUS float32) float32 {
return Math.PI * * RADIUS RADIUS
}

CircleArea_test.go for the test program, as follows:
The gotest Package 

Import "Testing"

// functional test
/ *
FUNC TestGetCircleArea (T * testing.T) {
Area: = GetCircleArea (4.0)
! = 50.265484 {IF Area
( "! test fails") t.Error
} the else {
T .log ( "test passed")
}
} * /

// stress tests
const = N. 3

FUNC TestGetCircleArea (T * testing.T) {
for I: = 0; I <N; I ++ {
Area: = GetCircleArea (4.0)
IF Area! = 50.265484 {
t.Error ( "test failed!")
} the else {
t.Log ( "test passed")
}
}
}

2.Perform tests

1) and stress testing function

execution command in the terminal: Go Test -v



2) Coverage Test

Run terminal: go test -cover



3. Generate Report

1) mounted GoReporter

execution command in the terminal: Go GET -u github.com/360EntSecGroup-Skylar/goreporter

2) to generate a report

execution command in the terminal: goreporter -p E: \ GoProject \ gotest -r E: \ GoProject \ gotest

automatically open after the success of the browser, as shown below:





directory also automatically generate reports in html file

Guess you like

Origin www.cnblogs.com/wanyuan/p/12061196.html