Home >Backend Development >Golang >How to test packages in Go?

How to test packages in Go?

WBOY
WBOYOriginal
2024-06-01 11:50:56987browse

By using the Go language’s built-in testing framework, developers can easily write and run tests for their code. The test file ends with _test.go and contains test functions starting with Test, where the *testing.T parameter represents the test instance. Error messages are logged using t.Error(). You can run tests by running the go test command. Subtests allow test functions to be broken down into smaller parts and created via t.Run(). A practical example includes a test file written for the IsStringPalindrome() function in the utils package, which tests the correctness of the function using a series of input strings and expected output.

如何在 Go 语言中测试包?

How to test packages in Go language?

The Go language provides a powerful built-in testing framework that allows developers to easily write and run tests for their code. Here's how to use the Go test package to test your program.

Writing tests

In Go, test files end with _test.go and are placed in the same directory as the package to be tested. Test files contain one or more test functions that begin with Test followed by the functionality to be tested.

The following is a sample test function:

import "testing"

func TestAdd(t *testing.T) {
    if Add(1, 2) != 3 {
        t.Error("Add(1, 2) returned an incorrect result")
    }
}

*testing.T The parameters represent the test instance. Error messages are logged using t.Error().

Run the test

You can run the test by running the following command:

go test

If the test is successful, a message such as "PASS" will be displayed. If an error occurs, an error message will be displayed.

Using subtests

Subtests allow a test function to be broken down into smaller parts. This helps organize test code and improve readability.

The following is how to write a subtest:

func TestAdd(t *testing.T) {
    t.Run("PositiveNumbers", func(t *testing.T) {
        if Add(1, 2) != 3 {
            t.Error("Add(1, 2) returned an incorrect result")
        }
    })

    t.Run("NegativeNumbers", func(t *testing.T) {
        if Add(-1, -2) != -3 {
            t.Error("Add(-1, -2) returned an incorrect result")
        }
    })
}

Practical case

Suppose we have a package named utils, which contains a IsStringPalindrome() Function, used to check whether a string is a palindrome string.

Here's how to write a test file to test this function:

package utils_test

import (
    "testing"
    "utils"
)

func TestIsStringPalindrome(t *testing.T) {
    tests := []struct {
        input    string
        expected bool
    }{
        {"", true},
        {"a", true},
        {"bb", true},
        {"racecar", true},
        {"level", true},
        {"hello", false},
        {"world", false},
    }

    for _, test := range tests {
        t.Run(test.input, func(t *testing.T) {
            if got := utils.IsStringPalindrome(test.input); got != test.expected {
                t.Errorf("IsStringPalindrome(%s) = %t; want %t", test.input, got, test.expected)
            }
        })
    }
}

In this test file:

  • ##tests The array is defined A sequence of input strings and expected output.
  • for Loop through the tests array and create subtests using t.Run().
  • Each subtest calls the
  • utils.IsStringPalindrome() function and compares its results to the expected results. If the results are inconsistent, it logs an error using t.Errorf().

The above is the detailed content of How to test packages in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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