Questions tagged [go-testing]
121 questions
2
votes
2 answers
Mocking redis server with miniredis fails
Golang newbie here :)
I have a redis client wrapper over go-redis and I want to test that it works properly. I have tried using miniredis to mock the redis server I would be connecting to, but I keep getting an error.
When I have everything in the…

idohu
- 133
- 3
- 7
1
vote
0 answers
How to create Unit test cases for “GetListOfUsers()” function of “UserService.go“ file. Facing issue in creating dummy aerospike.Recordset object
How to create Unit test cases for “GetListOfUsers()” function of “UserService.go“ file. Facing issue in creating dummy aerospike.Recordset object
this is the test file
Sample testfile
func (suite *UserServiceTestSuite) TestGetListOfUsers()…

A_Gour
- 21
- 4
1
vote
0 answers
Is the go test coverage.out file able to show how many tests were run?
I have a project in go lang, I can run tests and sonar can identify the total percentage of test coverage. But I want to know if I can generate any metrics in this coverage.out file that can show how many tests were run so that sonar can display…

Murilo Alves Batista
- 11
- 2
1
vote
2 answers
How to correctly cover all golang packages with all tests?
Assume that some code defined under package pkg/somepkg1.
And there are some tests in this package and some tests are in package tests/integration.
If i call
go test -cover -coverprofile=cover.out ./...
i have got all tests run (including…

comdiv
- 865
- 7
- 26
1
vote
1 answer
How to run `go test` in the parent directory of multiple go modules and return non-zero in case of error
Take a look at this directory structure:
/root
/hoge
go.mod
go.sum
main.go
/test
main_test.go
/unit
sub_test.go
/fuga
go.mod
go.sum
main.go
…

kyoshi
- 31
- 4
1
vote
1 answer
Go test "-run -" flag executed tests much faster
I was looking at some benchmark tests from https://github.com/RoaringBitmap/roaring
When running a specific benchmark using -run - (as mentioned in there comments):
go test -bench BenchmarkNexts -benchmem -run - It seems to execute faster, at least…

Chris
- 476
- 4
- 10
1
vote
1 answer
How to write unit test when using db transaction?
I'm using mysql transaction in my project, I want to know how to write unit test when using db transaction? for example:
func SomeFunc() error {
return db.Transaction(func(tx *gorm.DB) {
// my business logic goes below
tx.Select(xxx)
…

lee3164
- 21
- 2
1
vote
0 answers
`gopls` disagrees with `go test` on interface implementation
Consider the following code:
package ifaces
import (
"fmt"
"testing"
)
type IFace1 interface {
Do1()
}
type IFace2 interface {
Do2()
}
type Inner struct {
}
func (i *Inner) Do2() {
…

caxcaxcoatl
- 8,472
- 1
- 13
- 21
1
vote
0 answers
Testing gRPC server with golang and mockery fails with a timeout when mock fails
I am trying to test a gRPC service locally.
Here is the sample code
type TestGrpcServer struct {
t *testing.T
Server *grpc.Server
Lis *bufconn.Listener
Conn *grpc.ClientConn
}
func (s *TestGrpcServer) Serve() {
go…

Sanath Manavarte
- 84
- 4
1
vote
2 answers
Golang how to test function that return channel type?
I try to test function StartP,
Expect that Start() should be called 1 times, Done() should be called 1 times
but I have trouble that test will block when run this step <-ps.Done()
I expect <-ps.Done() return nil
How can I test function that return…

Y. Ryan
- 21
- 5
1
vote
0 answers
How to measure test coverage difference in Go
Is there a way to get the difference in coverage percentage? My goal is to fail a build when the coverage has decreased.
I can easily get the current coverage percentage and compare it to a threshold number. However I want to compare it to the…

treesan
- 49
- 2
1
vote
0 answers
Go test coverage for test files in separate module
I'm running gotestsum --junitfile-testcase-classname=short --junitfile-testsuite-name=relative \ -- -coverprofile=coverage.out ./serviceName/...
The structure looks like this:
serviceName
normalModule
- file.go
- file_test.go
…

treesan
- 49
- 2
1
vote
1 answer
Test for handler with file upload
I'm trying to write a test for a handler which receives a file. As part of such I'm trying to configure my context so the handler can use it.
My intention is to create a file and use multipart.FileHeader to open it.
f, err := os.CreateTemp("",…

user672009
- 4,379
- 8
- 44
- 77
1
vote
1 answer
handler.ServeHTTP(w,req) and handler(w,req) difference in tests
I'm trying to understand the difference between handler.ServeHTTP(w,req) and handler(w,req) in Go testing. When should I use each of them? Are they exactly the same?
Docs say simply: ServeHTTP calls f(w, r).
for _, tt := range tests {
…

mishkamashka
- 73
- 1
- 6
1
vote
1 answer
Running all tests for a multi-binary project
Consider a multi binary project with the following structure.
.
├── bin1
│ ├── config
│ │ ├── config.go
│ │ └── config_test.go
│ └── utils
│ ├── utils.go
│ └── utils_test.go
├── bin2
│ ├── config
│ │ ├── config.go
│ …

kexic63212
- 19
- 2