Can I run a single test in a suite?
Asked Answered
M

2

16

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?

Macrography answered 5/11, 2016 at 1:4 Comment(3)
Does -m do what you expect? From the documentation: Regular expression to select the methods of test suites specified command-line argument "-m" maybe combined with -run to specify the suite?Biosynthesis
According to help, "run is to - Run only those tests and examples matching the regular expression." The content of the file only has one test that calls the suite method. So go test after analyzing files may just not find matches if it looks for something like func TestBlah(t *testing.T)...Macrography
yes, -run picks the Test* function to run, which starts a particular suite, and the -m flag will filter which suite methods to execute.Biosynthesis
S
21

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

Slumber answered 9/4, 2017 at 22:8 Comment(2)
Now the flag has been changed to -m (go version go1.9)Genocide
It's a shame that you can't just mark a test with .only or something like you can do in other test frameworks.Graniela
D
1

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
Disfrock answered 7/11, 2016 at 20:20 Comment(1)
Interesting. Perhaps TestSuite2/third is the key. I'll try. Thank you!Macrography

© 2022 - 2024 — McMap. All rights reserved.