How to use the Go standard library for unit testing

王林
Release: 2024-04-30 18:39:02
Original
378 people have browsed it

The Go standard library provides unit testing functionality through the testing package. Just create the _test.go file and write the test function. Test functions use assertion functions, such as AssertEqual and AssertTrue, to compare expected and actual results. Information about whether the test passed or failed will be displayed with the go test command.

如何使用 Go 标准库进行单元测试

How to use the Go standard library for unit testing

The Go standard library provides the testing package, which provides rich functions. For writing and running unit tests. This article will guide you on how to use the testing package to test your Go code.

Set up unit tests

To set up unit tests for your Go program, follow these steps:

  1. Create a file named ## in the root directory of your project #your_package_name_test.go file.
  2. Import
  3. testing Package:
  4. import "testing"
    Copy after login
Writing test functions

Each unit test is written as an independent function named

func TestX(t *testing.T). Where X is the name of the test case, and t is a pointer of type *testing.T, which provides functions for executing tests, reporting results, and logging errors Methods.

func TestPassingTest(t *testing.T) {
    // 测试用例的代码...
}
Copy after login

Assertions

testing The package provides a variety of assertion functions to compare test results with expected values. Commonly used assertion functions include:

  • AssertEqual(t, expected, actual): Check whether expected and actual are equal
  • AssertNotEqual(t, value1, value2): Check whether value1 and value2 are not equal
  • AssertTrue(t , condition): Assert condition condition is true
  • AssertFalse(t, condition): Assert condition condition is false
Practical Case

Let’s write a unit test for a function that calculates the sum of two numbers.

package my_package

import (
    "testing"
)

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

Unit test

import (
    "testing"

    "my_package"
)

func TestSum(t *testing.T) {
    tests := []struct {
        a        int
        b        int
        expected int
    }{
        {1, 2, 3},
        {5, 7, 12},
    }

    for _, tc := range tests {
        result := my_package.Sum(tc.a, tc.b)
        if result != tc.expected {
            t.Errorf("Sum(%d, %d) returned %d, expected %d", tc.a, tc.b, result, tc.expected)
        }
    }
}
Copy after login

In the unit test, we create a list of test cases of type

[]struct which contains the input values and expected output. We use a loop to iterate through the test cases and use the AssertEqual assertion for each case to verify that the calculated result is consistent with the expected result.

Running unit tests

To run unit tests, you can run the following command in the command line:

go test
Copy after login
It will look for all ## in the project directory and its subdirectories #_test.go

file and run the tests in it. Tests that run successfully will display a green pass message, while failed tests will display a red error message.

The above is the detailed content of How to use the Go standard library for unit testing. 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