我可以在测试套件中运行单个测试吗?

huangapple go评论85阅读模式
英文:

Can I run a single test in a suite?

问题

我为我的结构体设置了一个测试套件(https://github.com/stretchr/testify#suite-package)。在转换之前,我可以通过指定一个模式来运行单个测试:

go test -v ./services/gateways/... -run mytest

在转换之后,这种方法不起作用了。是运气不好还是有其他方法?

英文:

I have setup a test suite for my struct (https://github.com/stretchr/testify#suite-package). Before I was able to run a single test by specifying just a pattern:

go test -v ./services/gateways/... -run mytest

This approach doesn't work after conversion. Bad luck or is there a way?

答案1

得分: 21

你可以通过指定-testify.m参数来运行单个方法。

要运行此套件方法的命令是:

go test -v github.com/vektra/mockery/mockery -run ^TestGeneratorSuite$ -testify.m TestGenerator

英文:

You can run single methods by specifying the -testify.m argument.

to run this suite method the command is:

go test -v github.com/vektra/mockery/mockery -run ^TestGeneratorSuite$ -testify.m TestGenerator

答案2

得分: 1

我认为你在那个软件包上可能会遇到困难,但是这里有一个类似的方法,使用Go 1.7的默认测试工具:

package main

import "testing"

func TestSuite1(t *testing.T) {
    t.Run("first test", func(t *testing.T) { t.Fail() })
    t.Run("second test", func(t *testing.T) { t.Fail() })
}

func TestSuite2(t *testing.T) {
    t.Run("third test", func(t *testing.T) { t.Fatal("3") })
    t.Run("fourth test", func(t *testing.T) { t.Fatal("4") })
}

一个测试套件的示例输出:

therealplato/stack-suites Ω go test -run TestSuite1       
--- FAIL: TestSuite1 (0.00s)
    --- FAIL: TestSuite1/first_test (0.00s)
    --- FAIL: TestSuite1/second_test (0.00s)
FAIL
exit status 1
FAIL    github.com/therealplato/stack-suites    0.005s

一个单个测试的示例输出:

therealplato/stack-suites Ω go test -run TestSuite2/third 
--- FAIL: TestSuite2 (0.00s)
    --- FAIL: TestSuite2/third_test (0.00s)
        main_test.go:11: 3
FAIL
exit status 1
FAIL    github.com/therealplato/stack-suites    0.005s
英文:

i think you're SOL with that package but here's a similar approach with go 1.7's stock testing tools:

package main

import "testing"

func TestSuite1(t *testing.T) {
	t.Run("first test", func(t *testing.T) { t.Fail() })
	t.Run("second test", func(t *testing.T) { t.Fail() })
}

func TestSuite2(t *testing.T) {
	t.Run("third test", func(t *testing.T) { t.Fatal("3") })
	t.Run("fourth test", func(t *testing.T) { t.Fatal("4") })
}

Example output for one suite:

 therealplato/stack-suites Ω go test -run TestSuite1       
--- FAIL: TestSuite1 (0.00s)
    --- FAIL: TestSuite1/first_test (0.00s)
    --- FAIL: TestSuite1/second_test (0.00s)
FAIL
exit status 1
FAIL    github.com/therealplato/stack-suites    0.005s

Example output for one test:

 therealplato/stack-suites Ω go test -run TestSuite2/third 
--- FAIL: TestSuite2 (0.00s)
    --- FAIL: TestSuite2/third_test (0.00s)
        main_test.go:11: 3
FAIL
exit status 1
FAIL    github.com/therealplato/stack-suites    0.005s

huangapple
  • 本文由 发表于 2016年11月5日 09:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/40433788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定