Go programming example [GoConvey test]

Introduction to GoGonvey

GoConvey is a test aid development kit for the Go language. On the basis of being compatible with the Go native test, it also expands the convenient syntax and a large number of built-in judgment conditions to reduce the burden on developers.

In addition, the tool also provides a program for real-time monitoring of code compilation and testing, coupled with a comfortable web interface, so that a developer no longer excludes writing unit tests.

main features

  • Elegant and concise test code
  • Directly integrate Go native tests
  • Fully automatic compilation test
  • Detailed display of test results and coverage
  • Highly readable command line output
  • Semi-automatic writing test cases

Download and install

  • Install via gopm:
    gopm get github.com/smartystreets/goconvey
  • Install via go get:
    go get github.com/smartystreets/goconvey
root@192:~/www/test# go get github.com/smartystreets/goconvey
go: downloading github.com/smartystreets/goconvey v1.8.0
go: downloading golang.org/x/tools v0.7.0
go: downloading golang.org/x/sys v0.6.0
go: downloading golang.org/x/mod v0.9.0
go: added github.com/smartystreets/goconvey v1.8.0
go: added golang.org/x/mod v0.9.0
go: added golang.org/x/sys v0.6.0
go: added golang.org/x/tools v0.7.0
root@192:~/www/test# 

write code

We will implement the four functions of addition, subtraction, multiplication and division of the four arithmetic operations.

func Add(a, b int) int {
    
    }
func Subtract(a, b int) int {
    
    }
func Multiply(a, b int) int {
    
    }
func Division(a, b int) (int, error) {
    
    }

The "imported and not used" error message means that the "errors" package is imported in the code, but it is not used in the program.

write tests

Import the corresponding driver package

import (
	"testing"
  . "github.com/smartystreets/goconvey/convey"
)

Mainly use the Convey() and So() functions to complete the test definition and conditional judgment.

The dot (.) in the import statement allows direct access to the exported name in the "github.com/smartystreets/goconvey/convey" package without explicitly prefixing it with the package name.

This can make code more concise and readable, but can also cause name collisions if two packages have exported names for the same identifier.

Note that the dot (.) import syntax is generally not recommended in Go code, as it can make the code less clear and more error-prone.
It is recommended to always use the full package name unless there is a specific reason to use a dot import.

/root/www/test/main.go

package goconvey

import "errors"

func Add(a, b int) int {
    
    
	return a + b
}
func Subtract(a, b int) int {
    
    
	return a - b
}
func Multiply(a, b int) int {
    
    
	return a * b
}
func Division(a, b int) (int, error) {
    
    
	if b == 0 {
    
    
		return 0, errors.New("被除数不能为零!")
	}
	return a / b, nil
}

/root/www/test/main_test.go

insert image description here
shows that the Go module system cannot import the "github.com/smartystreets/goconvey/convey" package because module lookup is disabled by the GOPROXY=offcompiler environment variable.

The GOPROXY environment variable is used to specify the location of the Go module proxy server used for downloading and caching module dependencies.

When the value of GOPROXY is set to "offcompiler", it completely disables module lookup, which prevents Go from being able to locate and download required packages.

To fix this, you can remove the GOPROXY=offcompiler environment variable or set it to a value pointing to a valid Go module proxy server.

You can also try running the "go mod tidy" command to update and download any missing module dependencies.

root@192:~/www/test# go mod tidy
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/smartystreets/assertions v1.13.1
go: downloading github.com/gopherjs/gopherjs v1.17.2
root@192:~/www/test# 
package goconvey

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestAdd(t *testing.T) {
    
    
	Convey("将两数相加", t, func() {
    
    
		So(Add(1, 2), ShouldEqual, 3)
	})
}

func TestSubtract(t *testing.T) {
    
    
	Convey("将两数相减", t, func() {
    
    
		So(Subtract(1, 2), ShouldEqual, -1)
	})
}

func TestMultiply(t *testing.T) {
    
    
	Convey("将两数相乘", t, func() {
    
    
		So(Multiply(3, 2), ShouldEqual, 6)
	})
}

func TestDivision(t *testing.T) {
    
    
	Convey("将两数相除", t, func() {
    
    
		Convey("被除数为0", func() {
    
    
			_, err := Division(10, 0)
			So(err, ShouldNotBeNil)
		})
		Convey("被除数不为0", func() {
    
    
			num, err := Division(10, 2)
			So(err, ShouldBeNil)
			So(num, ShouldEqual, 5)
		})
	})
}

run test

Use the Go native method
$ go test

Install test coverage tools

root@192:~/www/test# go get github.com/smartystreets/goconvey
root@192:~/www/test# go install github.com/smartystreets/goconvey
  • 1. A github.com subdirectory has been added under the $GOPATH/src directory, which contains the library code of the GoConvey framework.
  • 2. The executable program goconvey of the GoConvey framework is added in the $GOPATH/bin directory.

Use the automated compilation test provided by GoConvey:
$ goconvey
Then, enter the browser access address localhost:8080/ to view the test results.

go test

root@192:~/www/test# go test
.
1 total assertion

.
2 total assertions

.
3 total assertions

...
6 total assertions

PASS
ok      goconvey        0.002s


root@192:~/www/test# go test -v
=== RUN   TestAdd

  将两数相加 ✔


1 total assertion

--- PASS: TestAdd (0.00s)
=== RUN   TestSubtract

  将两数相减 ✔


2 total assertions

--- PASS: TestSubtract (0.00s)
=== RUN   TestMultiply

  将两数相乘 ✔


3 total assertions

--- PASS: TestMultiply (0.00s)
=== RUN   TestDivision

  将两数相乘 
    被乘数为0 ✔
    被乘数不为0 ✔✔


6 total assertions

--- PASS: TestDivision (0.00s)
PASS
ok      goconvey        0.002s
root@192:~/www/test# 

Divisor modification error: So(num, ShouldEqual, 4)

root@192:~/www/test# go test -v
=== RUN   TestAdd

  将两数相加 ✔


1 total assertion

--- PASS: TestAdd (0.00s)
=== RUN   TestSubtract

  将两数相减 ✔


2 total assertions

--- PASS: TestSubtract (0.00s)
=== RUN   TestMultiply

  将两数相乘 ✔


3 total assertions

--- PASS: TestMultiply (0.00s)
=== RUN   TestDivision

  将两数相除 
    被除数为0 ✔
    被除数不为0 ✔✘


Failures:

  * /root/www/test/main_test.go 
  Line 36:
  Expected: '4'
  Actual:   '5'
  (Should be equal)


6 total assertions

--- FAIL: TestDivision (0.00s)
FAIL
exit status 1
FAIL    goconvey        0.002s
root@192:~/www/test# 

goconvey test

root@192:~/www/test# go get github.com/smartystreets/goconvey
root@192:~/www/test#
go install github.com/smartystreets/goconvey
root@192:~/www/test# goconvey

insert image description here

root@192:~/www/test# go get golang.org/x/tools/cmd/cover
go: downloading golang.org/x/tools/cmd/cover v0.1.0-deprecated
go: added golang.org/x/tools/cmd/cover v0.1.0-deprecated
root@192:~/www/test#

insert image description here
insert image description here

Toolkit

Unable to download package management tool
https://gopm.io

Application documentation
https://github.com/smartystreets/goconvey/wiki

Guess you like

Origin blog.csdn.net/weiguang102/article/details/130443454