[TOC]

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Go test unit test is used

What is Unit Testing?

Unit testing refers to checking and verifying the smallest testable units in software

A unit is an artificially defined minimum functional module under test

Generally speaking, the specific meaning should be determined according to the actual situation. For example, unit in C language refers to a function, and unit in Go is also a function

Unit testing is the lowest level of testing activity to be performed during software development, where individual units of software are tested in isolation from the rest of the program.

Unit testing is also called single testing. During development, we also need to write some demo to test the function or some small function in our project

Go Test Unit test

Unit tests in GO use the standard library testing

Here are the simple rules:

  • Import the test library
  • Single test file name, follow_test
  • The function name in the single Test file starts with Test and the parameter must bet *testing.T

A simple example:

Write a simple example of adding suffixes and prefixes

.Heavy Exercises ── heavy exercises ── heavy exercises ── heavy exercisesCopy the code

cal.go

package main

func Addprefix(str string) string {

    return "hello_"+str
}


func Addsuffix(str string) string {

    return str+"_good"
}
Copy the code

cal_test.go

package main

import "testing"

func TestAddprefix(t *testing.T) {
        Addprefix("xiaomotong")}func TestAddsuffix(t *testing.T) {
        Addsuffix("xiaomotong")}Copy the code

sub.go

package main

func MyAdd(a int, b int) int  {

    if a+b > 10{
        return 10
    }

    return a+b
}


func MySub(one int, two int) int{

    if one - two < 0{
        return 1
    }

    return one - two
}

Copy the code

sub_test.go

package main

import "testing"
import "fmt"

func TestMyAdd(t *testing.T) {
    num := MyAdd(4 ,9)
    fmt.Println(num)

    num = MyAdd(4 ,2)
    fmt.Println(num)
}
Copy the code

Executing unit tests

go test -v
Copy the code
  • -v

-v is the argument that displays the test results for each use case, showing the single test function executed, whether it passed, and when it was single tested

The result is as follows

=== RUN TestAddprefix -- PASS: TestAddprefix (0.00s) === RUN TestAddsuffix -- PASS: TestAddsuffix (0.00s) === RUN TestMyAdd 10 6 -- PASS: TestMyAdd (0.00s) PASS OK my_new_first/golang_study/later_learning/gotest 0.002sCopy the code

Executing go Test in the package directory executes all the unit test files in the package

Run only the specified monotest function

We can use it like this:

go test -run TestMyAdd -v

The results are as follows:

=== RUN TestMyAdd 10 6 -- PASS: TestMyAdd (0.00s) PASS OK my_new_first/golang_study/later_learning/gotest 0.002sCopy the code

The child test

We call the Run method in the Testing package in the single test function we wrote to Run the child tests

Let’s modify sub_test.go above

package main

import "testing"
import "fmt"

func TestMyAdd(t *testing.T) {
    num := MyAdd(4 ,9)
    fmt.Println(num)

    num = MyAdd(4 ,2)
    fmt.Println(num)

}

func TestMySub(t *testing.T) {
        t.Run("one".func(t *testing.T) {
                if MySub(2.3) != 1 {
                        t.Fatal("cal error")
                }

        })
        t.Run("two".func(t *testing.T) {
                if MySub(3.1) != 2 {
                        t.Fatal(" error ")}}}Copy the code

Call the child test function separately and run go test-run TestMySub/ one-v

=== RUN TestMySub === RUN TestMySub/one -- PASS: TestMySub (0.00s) -- PASS: TestMySub/one (0.00s) PASS OK my_new_first/golang_study/later_learning/gotest 0.003sCopy the code

Generate reports and calculate coverage

  • Generate coverage report files

go test -v -covermode=count -coverprofile=cover.out

  • Convert to HTML using go Tool

go tool cover -html=cover.out -o cover.html

Open the HTML file in the browser, and you can view the following report

In the figure, the green parts are covered and the red parts are not. Our example has covered all the specific functions

Go test instructions, we can also see the help documentation

Many companies are beginning to engage in efficiency, single test, automated testing, CI/CD are to quickly get up, the best is to make a button release button rollback. Envy these places where the basic setup is very perfect, hahaha ~

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~