stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.15k stars 1.59k forks source link

How can I get coverage in the Suite package #1530

Closed mkuznetsov2 closed 7 months ago

mkuznetsov2 commented 7 months ago

I used this part of the testify https://github.com/stretchr/testify?tab=readme-ov-file#suite-package And my test look like

func TestRun(t *testing.T) {
    ctx := context.Background()
    connection := src.PrepareDB(t, ctx)

    suite.Run(t, &Suite1{DBConnection: connection})
    suite.Run(t, &Suite2{DBConnection: connection})
}
type Suite1 struct {
    suite.Suite
    DBConnection *pgsql.TxManager
}

func (s *Suite1) SetupTest() {}

func (s *Suite1) TearDownSuite() {}

func (s *Suite1) TestSuite1() {
    // DO SOMTHING HELPFUL

    assert.Equal(s.T(), ...)
}

And I try to fetch coverage tests by command go test -v -coverprofile=coverage.txt -covermode count But I have just empty coverage.txt and message coverage: [no statements] Output run of tests

--- PASS: TestRun (4.77s)
    --- PASS: TestRun/TestSuite1 (0.03s)
    --- PASS: TestRun/TestSuite2 (0.05s)
PASS
coverage: [no statements]
ok      myProject/tests 5.809s

Can you explain, how get coverage tests?

brackendawson commented 7 months ago

I can't reproduce this behaviour, using go test -v -coverprofile=coverage.txt -covermode count I get coverage information in coverage.txt.

What does go env show for you and what version of testify are you using?

mkuznetsov2 commented 7 months ago
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/m.kuznetsov/Library/Caches/go-build'
GOENV='/Users/m.kuznetsov/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/m.kuznetsov/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/m.kuznetsov/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.6/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.6/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.6'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/0b/1yz4ln6s3_9dlxnk8qxyg5d81c7vwk/T/go-build2343961692=/tmp/go-build -gno-record-gcc-switches -fno-common'

But in CI/CD it doesn't work too. I use GitlabCI and image golang:1.21-bookworm

brackendawson commented 7 months ago

What testify version are you using?

Using (mostly) your code example with go 1.21.6 on darwin/arm64 I get coverage:

% go version
go version go1.21.6 darwin/arm64
% go test -v -coverprofile=coverage.txt -covermode count
=== RUN   TestRun
=== RUN   TestRun/TestSuite1
--- PASS: TestRun (0.00s)
    --- PASS: TestRun/TestSuite1 (0.00s)
PASS
coverage: 100.0% of statements
ok      github.com/brackendawson/kata   0.593s
% cat coverage.txt
mode: count
github.com/brackendawson/kata/kata.go:3.21,5.2 1 1

Does your code actually have any statements?

mkuznetsov2 commented 7 months ago

github.com/stretchr/testify v1.8.4 (last version)

>go version
go version go1.21.6 darwin/arm64

>go test -v -coverprofile=coverage.txt -covermode count
=== RUN   TestRun
=== RUN   TestRun/TestISuite1
--- PASS: TestRun (0.05s)
    --- PASS: TestRun/TestISuite1 (0.00s)
PASS
coverage: [no statements]
ok      mytests/tests   0.586s

>cat coverage.txt
mode: count

Does your code actually have any statements?

Yes, I have code which work with DB. But I tried to write "test code" and find the problem. I got the same empty coverage.

brackendawson commented 7 months ago

Can you share a compilable recreate?

brackendawson commented 7 months ago

Please comment, or start a discussion (tab up top), if you still have this issue.