是否可以从另一个文件调用测试功能以开始测试 [英] Is it possible to call a test Func from another file to start the testing

查看:45
本文介绍了是否可以从另一个文件调用测试功能以开始测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从非测试go文件中调用测试函数以开始执行测试?

Is it possible to call a test Function from a non test go file to start the execution of tests?

例如,我有一个测试功能:

e.g., I have a test function:

package API

import "testing"

func TestAPI(t *testing.T) { 
...
}

我需要从非测试版文件中调用此文件.

I need to call this from a non test go file.

package main

import "../API"

API.TestAPI()

我可以这样做吗?

推荐答案

Go中有一些很棒的测试模式.不幸的是,您尝试进行测试的方式无法正常工作.

There are some great testing patterns in Go. Unfortunately, the way that you're trying to test is not going to work.

您应该使用命令 go test< pkg 运行单元测试.绝对不要在代码内部调用测试.

Your unit tests should be run using the command go test <pkg. Tests should never be called from within your code itself.

Go中的单元测试通常有两种主要形式-黑盒和白盒单元测试.

Generally there are two main forms of unit testing in Go - black and white box unit testing.

白盒测试:从内部测试未导出功能包装本身.

黑盒测试:从外部测试已导出功能程序包,模拟其他程序包将如何与之交互.

Black-box testing: Testing exported functions from outside the package, emulating how other packages will interact with it.

如果我有要测试的软件包, example .有一个简单的导出功能,可以对提供的数字列表进行求和. Sum 函数还使用了 unexported 实用程序功能.

If I have a package, example, that I'd like to test. There is a simple exported function that sums a list of numbers that are provided. There is also an unexported utility function that's used by the Sum function.

example.go

package example

func Sum(nums ...int) int {
    sum := 0
    for _, num := range nums {
        sum = add(sum, num)
    }
    return sum
}

func add(a, b int) int {
    return a + b
}

example_test.go :黑盒测试

请注意,我正在测试 example 包,但测试位于 example_test 包中.添加 _test 可以使Go编译器满意,并告知它这是 example 的测试包.在这里,我们只能访问导出的变量和函数,这些变量和函数可以让我们测试外部软件包在导入和使用我们的 example 软件包时将遇到的行为.

Notice that I'm testing the example package, but the test sits in the example_test package. Adding the _test keeps the Go compiler happy and lets it know that this is a testing package for example. Here, we may only access exported variables and functions which lets us test the behaviour that external packages will experience when importing and using our example package.

package example_test

import (
    "testing"

    "example"
)

func TestSum(t *testing.T) {
    tests := []struct {
        nums []int
        sum  int
    }{
        {nums: []int{1, 2, 3}, sum: 6},
        {nums: []int{2, 3, 4}, sum: 9},
    }

    for _, test := range tests {
        s := example.Sum(test.nums...)
        if s != test.sum {
            t.FailNow()
        }
    }
}

example_internal_test.go :白盒测试

请注意,我正在测试 example 包,该测试也位于 example 包中.这样,单元测试就可以访问未导出的变量和函数.

Notice that I'm testing the example package, and the test also sits in the example package. This allows the unit tests to access unexported variables and functions.

package example

import "testing"

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

    for _, test := range tests {
        s := add(test.a, test.b)
        if s != test.sum {
            t.FailNow()
        }
    }
}

我希望这对您如何建立和使用Go的测试框架有更好的了解.为了简单起见,我没有在示例中使用任何外部库,尽管一个非常流行且功能强大的软件包可以帮助进行单元测试,但 github.com/stretchr/testify .

I hope that this you a better understanding on how Go's testing framework is set up and should be used. I haven't used any external libraries in the examples for simplicity sake, although a very popular and powerful package to help with unit testing is github.com/stretchr/testify.

这篇关于是否可以从另一个文件调用测试功能以开始测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆