头图

What is unit testing

Unit testing is the examination and verification of the smallest testable unit of software

A unit is the smallest functional module under test specified by humans.

Generally speaking, the specific meaning should be determined according to the actual situation. For example, a unit in C language refers to a function, and a 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, we usually call it unit testing. When developing, we also need to write some demos to test a function or a small function in our project.

go test unit test

Unit tests in the GO language use the standard library testing

There are the following simple rules:

  • Import the test standard library
  • Single test file name, followed by _test
  • The function name in the single test file starts with Test, and the parameter must be t *testing.T

Simple example:

Write a simple example, add suffix and prefix

 .
├── cal.go
├── cal_test.go
├── lll
└── sub.go

cal.go

 package main

func Addprefix(str string) string {

    return "hello_"+str
}


func Addsuffix(str string) string {

    return str+"_good"
}

cal_test.go

 package main

import "testing"

func TestAddprefix(t *testing.T) {
        Addprefix("xiaomotong")
}


func TestAddsuffix(t *testing.T) {
        Addsuffix("xiaomotong")
}

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
}

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

Execute unit tests

 go test -v
  • -v

-v is a parameter that will display the test results of each use case, display the executed single test function, whether it passed and when the single test

The running 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.002s

Execute go test in the package directory, it will execute all the unit test files in the package

Only run the specified single test function

We can use it like this:

go test -run TestMyAdd -v

The result is as follows:

 === RUN   TestMyAdd
10
6
--- PASS: TestMyAdd (0.00s)
PASS
ok      my_new_first/golang_study/later_learning/gotest 0.002s

subtest

That is, in the single test function we wrote, call the Run method in the testing package to run the sub-test

Let's transform the above 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)

}

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 ")
                }
        })
}

Call the sub-test function separately and execute 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.003s

Generate reports, calculate coverage

  • Generate coverage report file

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

  • Convert to html format using go tool

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

Open the html file in the browser, you can view the following report

The green part in the figure is covered, the red part is not covered, our example has covered all the specific functions

For the instructions after go test, we can also see the help documentation

Many companies have begun to develop efficiency, single testing, automated testing, CI/CD are all to be done quickly, it is best to make one-click release and one-click rollback. I envy these places where the basic settings are very perfect, hahaha~

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

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

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~


阿兵云原生
192 声望37 粉丝