Goconvey-Lesson 1: Elegant Unit Testing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Precautions

This blog belongs to Goconvey-Lesson 1: Elegant Unit Testing Please note the use of matching.

This post is a companion blog for the Goconvey-go Language Unit test package, which is designed to explain the use and case of the library through text-to-code examples, making it easier for fellow students to use and gain insight.

Library Introduction

Although the Go language comes with unit-testing capabilities, many third-party libraries have appeared before Goconvey was born. But there is no auxiliary library that can write code as elegantly as Goconvey unit tests, and the simple syntax and comfortable interface allow a developer who does not love writing unit tests to fall in love with unit testing.

Download installation

There are two ways you can download the installation Goconvey:

gopm get github.com/smartystreets/goconvey

Or

go get github.com/smartystreets/goconvey

API Documentation

Go Walker, please.

Basic Use Method

Sample code

Writing code

Here is a code that implements the basic arithmetic of integers (add, subtract, multiply, divide):

package goconveyimport (    "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("被除数不能为 0")    }    return a / b, nil}

In the above code, we implemented 4 functions, so we need to write unit tests for each of the 4 functions:

package goconveyimport (    "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() {            num, err := Division(10, 2)            So(err, ShouldBeNil)            So(num, ShouldEqual, 5)        })        Convey("除以 0", func() {            _, err := Division(10, 0)            So(err, ShouldNotBeNil)        })    })}

First, you need to use the officially recommended way to import Goconvey's auxiliary packages to reduce redundant code: . "github.com/smartystreets/goconvey/convey" .

The name of each unit test needs to Test begin with, for example: TestAdd , and you need to accept a *testing.T parameter of type.

Using Goconvey to write unit tests, each test case needs to be wrapped using a Convey function. The first parameter that it accepts is the description of the string type, the second argument is the *testing.T variable T in this example, and the third argument is a function that does not receive any arguments and returns no values (it is customary to write in a closed form).

ConveyStatements can also be nested indefinitely to reflect the relationships between test cases, such as the TestDivision way functions are nested to reflect their relationships. It is important to note that only the outermost Convey layer needs to pass in the variable T, and the inner nesting does not need to be passed in.

Finally, you need to use So a statement to judge the condition. In this example, we use only 3 different types of criteria to judge: ShouldBeNil , ShouldEqual and ShouldNotBeNil , respectively, that the value should be nil, that the value should be equal, and that the value should not be nil. For a detailed list of conditions, refer to the official documentation.

Run Tests

You can now open the command line and enter it go test -v for testing. Since Goconvey is compatible with Go native unit testing, we can use the GO command directly to perform the test.

The following is the output (MAC) of the command line:

=== RUN TestAdd  将两数相加 1 assertion thus far--- PASS: TestAdd (0.00 seconds)=== RUN TestSubtract  将两数相减 2 assertions thus far--- PASS: TestSubtract (0.00 seconds)=== RUN TestMultiply  将两数相乘 3 assertions thus far--- PASS: TestMultiply (0.00 seconds)=== RUN TestDivision  将两数相除    除以非 0 数     除以 0 6 assertions thus far--- PASS: TestDivision (0.00 seconds)PASSok      github.com/Unknwon/go-rock-libraries-showcases/lectures/03-goconvey/class1/sample/goconvey  0.009s

As we can see, the output conditioning is very clear and the code of the unit test is very elegant. So, is that all? Of course not. Goconvey not only supports manually invoking debug commands at the command line, but also provides a very comfortable Web interface for developers to automate the compilation and testing work.

Web interface

To use the Goconvey Web interface feature, you need to execute it in the appropriate directory goconvey (using the go get installation to the $GOPATH/bin directory), then open the browser, Access http://localhost:8080, you can see the following interface:

In the Web interface, you can set up an interface theme, see the full test results, and use the useful features such as browser reminders.

Other Features:

    • Automatically detects code changes and compiles tests
    • Semi-automated writing test cases: http://localhost:8080/composer.html
    • View Test Coverage: http://localhost:8080/reports/
    • Temporarily masking a package's build test

Summary

Here, you should have a basic understanding of how to write test cases using the Goconvey package, and also see how it makes writing test cases a dull work that becomes elegant and fun.

To write a package of test cases using Goconvey

The following packages use Goconvey to write test cases so that you can learn from the examples:

    • Goconfig
    • Macaron
    • Cae

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.