如何在指定的文件中运行测试用例? [英] How to run test cases in a specified file?

查看:104
本文介绍了如何在指定的文件中运行测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行 go test< package_name> ,它将运行包中的所有测试用例,我的包测试用例分散在多个文件中。


$ b

虽然没有必要全部运行它们。有没有一种方法可以指定 go test 运行的文件,以便它只运行文件中定义的测试用例?

解决方案

有两种方法。简单的方法是使用 -run 标志并提供一个匹配要运行的测试名称的模式。



<例如: $ go test -run NameOfTest 。有关更多信息,请参见文档



另一种方法是命名包含要运行的测试的特定文件:

  $ go test foo_test .go 

但有一个问题。如果


  • foo.go是包foo

  • foo_test.go是包foo_test,如果'foo_test.go'和'foo.go'是同一个软件包(一种常见的情况),那么这个软件包就是一个'foo'。那么你必须命名构建'foo_test'所需的所有其他文件。在这个例子中,它是:

      $ go test foo_test.go foo.go 

    我建议使用名称模式。或者,在/可能的情况下,始终运行所有包装测试。


    My package test cases are scattered across multiple files, if I run go test <package_name> it runs all test cases in the package.

    It is unnecessary to run all of them though. Is there a way to specify a file for go test to run, so that it only runs test cases defined in the file?

    解决方案

    There are two ways. The easy one is to use the -run flag and provide a pattern matching names of the tests you want to run.

    ex: $ go test -run NameOfTest. See the docs for more info.

    The other way is to name the specific file, containing the tests you want to run:

    $ go test foo_test.go
    

    But there's a catch. This works well if

    • foo.go is package foo
    • foo_test.go is package foo_test and imports 'foo'.

    If 'foo_test.go' and 'foo.go' are the same package (a common case), then you must name all other files required to build 'foo_test'. In this example it would be:

    $ go test foo_test.go foo.go
    

    I'd recommend to use the name pattern. Or, where/when possible, always run all package tests.

    这篇关于如何在指定的文件中运行测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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