Home > Backend Development > Golang > How to test Golang functions using unit testing framework?

How to test Golang functions using unit testing framework?

WBOY
Release: 2024-04-16 11:00:01
Original
861 people have browsed it

Use the unit testing framework for unit testing in Go: import the testing package. Write unit test functions prefixed with Test. Use assertion functions such as assertEqual() to verify test results. Run the unit test (go test) to verify the correctness of the function.

如何使用单元测试框架对 Golang 函数进行测试?

How to use the unit testing framework to test Go functions

Unit testing is essential to verify the correctness of a single function or method Important software development practices. In Go, unit testing is very simple using a unit testing framework such as the testing package.

Install the unit testing framework

#testing package is part of the Go standard library. To use it, import the testing package into your project:

import "testing"
Copy after login

Writing Unit Tests

Each unit test is a Test is the function prefixed. It accepts a pointer *testing.T as a parameter, which is used to report test results and record failure information.

The basic unit testing function is as follows:

func TestAdd(t *testing.T) {
    result := Add(1, 2)
    if result != 3 {
        t.Errorf("Add(1, 2) = %d; want 3", result)
    }
}
Copy after login

Assertion

##testing The package provides a series of assertion functions, Used to verify test results. Commonly used assertions include:

  • assertEqual()
  • assertNotEqual()
  • assertNil()
  • assertError()

Practical case

Consider a function that calculates the sum of two numbers

Add() Function:

func Add(a, b int) int {
    return a + b
}
Copy after login

We can write a unit test to verify this function:

import "testing"

func TestAdd(t *testing.T) {
    tests := []struct {
        a, b, want int
    }{
        {1, 2, 3},
        {0, 0, 0},
        {-1, -1, -2},
    }

    for _, test := range tests {
        result := Add(test.a, test.b)
        if result != test.want {
            t.Errorf("Add(%d, %d) = %d; want %d", test.a, test.b, result, test.want)
        }
    }
}
Copy after login

Run the test

To run unit tests, use the

go test command. It will find and run all Test* functions in the project.

go test
Copy after login

If the test passes, it will output the following results:

PASS
ok  my_project/my_package  1.730s
Copy after login
Otherwise, it will report the failed test, including an error message.

The above is the detailed content of How to test Golang functions using unit testing framework?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template