Questions tagged [go-testing]
121 questions
2
votes
1 answer
Where is the discoveryfake defined in k8s discovery interface?
While I'm reading how to test on k8s with the fake client on this link, I noticed this function, which, IIUC, assigns a faked server version to the faked k8s cluster.
k8s.clientset.Discovery().(*discoveryfake.FakeDiscovery).FakedServerVersion =…

Mike
- 1,841
- 2
- 18
- 34
2
votes
3 answers
How test functions are called in Go?
I'm studying Go language and I have a question now.
I was just testing buffered and unbuffered channels.
While writing the test code for it, a situation occurred that I couldn't understand the result.
func BenchmarkUnbufferedChannelInt(b *testing.B)…

didnlie23
- 101
- 7
2
votes
1 answer
Why do I get "second argument to errors.As should not be *error" build error in test only?
Consider the following test:
import (
"errors"
"fmt"
"testing"
)
func TestError(t *testing.T) {
err := &MyError{}
var target error
fmt.Println(errors.As(err, &target))
}
type MyError struct{}
func (err *MyError) Error()…

Sam Herrmann
- 6,293
- 4
- 31
- 50
2
votes
2 answers
How to calculate total code coverage for tests
I need to calculate code coverage for golang project where source of tests will be integration tests written in Java language . This requires go build to be instrumented first and then run on server so that tests can run and we will get to know…

Shreenivash
- 21
- 4
2
votes
1 answer
Why does -count=1 ignores caching in Go tests?
I understand that in order to avoid cached results in Go tests you can use the -count=1 flag in the go test command, but why?
This is from the docs:
The idiomatic way to disable test caching explicitly is to use -count=1
The explanation for the…

asaf92
- 1,557
- 1
- 19
- 30
2
votes
1 answer
How to use custom flag in tests (with `testify/suite`)
I want to add custom flags for my Go tests that use testify/suite. It looks like from this thread that it can only be in TestMain() (init() if it is before Go 1.13). However, with the testify/suite pacakge, TestMain() is not quite an option. I have…

cxc
- 201
- 2
- 10
2
votes
2 answers
How to run all test cases even if one test case fails
func Test_something(t *testing.T) {
// TEST CASE1: pass an array
// some logic here
// TEST CASE2: pass an EMPTY array --> this will cause test to fail
// some logic here
// TEST CASE3: pass something else
// some…

hmdevno
- 99
- 5
2
votes
1 answer
Why does benbjohnson/clock mock timer not execute when declared inside a goroutine?
This code works as I expect it
import (
"fmt"
"time"
"github.com/benbjohnson/clock"
)
func main() {
mockClock := clock.NewMock()
timer := mockClock.Timer(time.Duration(2) * time.Second)
go func() {
<-timer.C
…

Jatinshravan
- 435
- 3
- 16
2
votes
2 answers
How to test Go Mock Repository inside loop on usecase
I have a problem with testing with Repository Mock Database using testify.
I want to test a service/use case that creates a record database for each iteration. Here is the code:
This code contain mock of the database
mockrepository.go
package…

Christian Mahardhika
- 93
- 1
- 10
2
votes
2 answers
Can I output log messages from TestMain?
I want to write an integration test which tests writing and reading data to and from a database. In order to do this, I need my first test function to connect and prepare a test database; and if that is a success, let the other tests run, which will…

Digital Ninja
- 3,415
- 5
- 26
- 51
2
votes
1 answer
What is the use of test mode in Gin
I've checked the documentation but it does not explain the use of setting test mode for gin
gin.SetMode(gin.TestMode)
What is this test mode provided for? I do not see any difference when setting & not setting this mode in my tests.

Vivek
- 11,938
- 19
- 92
- 127
2
votes
1 answer
Go assert.Equal some interface with time.Time object inside
I am trying to check if the returned data equals the expectation
Here is my function:
func extractData(payload string) (interface{}, time.Time, error) {
eventTime := gjson.Get(payload, "data.eventDateTime").String()
dateTime, err :=…

Jasiuoo
- 41
- 1
- 1
- 2
2
votes
1 answer
Stubbing time.Now() during tests golang?
I'm following another answer here: Is there an easy way to stub out time.Now() globally during test?
So I have this file where I do something like:
var timeNow = time.Now
func GenerateTimestamp() int64 {
now := timeNow() // current local time
…

Valeria Martinez
- 53
- 1
- 7
2
votes
0 answers
SIGBUS error code=0x2 while running go test on CI
I'm not sure what could be the best way to explain this but we constantly observe our CI failing because of this SIGBUS issue. The error looks all
internal to Go and we are clueless.
We have run the test cases multiple times on our local boxes in an…

Noobie
- 461
- 1
- 12
- 34
2
votes
1 answer
Go Benchmark Output Format
My function name is BenchmarkArray1 what does the -12 represent?
BenchmarkArray1-12 1000000000 0.826 ns/op

Walton
- 139
- 1
- 8