132 questions
0
votes
2
answers
236
views
Unable to test internal functions in Golang [closed]
I'm using Go 1.24.3, installed via Homebrew on an Apple M1, running Sequoia 15.5.
I have a main:
//main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}...
0
votes
1
answer
138
views
Context Deadline is not cancelling a go routine fuction unless I reduce deadline
I modified a function from a book I was reading, the function writes "ping" to a writer at a predefined interval. The program runs in a go routine so I appended a context so the function can ...
1
vote
2
answers
293
views
Let web service response fail on io.ReadAll in unit test
I have a program that calls a web service and I want to write a unit test that checks the correct failure handling (nothing fancy). I use net/http/httptest to simulate the web service (backend). But I ...
-1
votes
1
answer
69
views
Achieving partial build for broken Go codebase
I have a generator which generates me code in Go, for example:
foo.go
bar.go
...
After generation, I need to test if my generator is doing good job. For every generated file, I have a test ...
-3
votes
1
answer
347
views
In `go test`, is it possible to pass custom flags?
As far as I tested, go test -- <custom flag(s)> works but I couldn't find any documentation for it.
I have this test code:
package main
import (
"os"
"testing"
)
...
1
vote
0
answers
147
views
How do I prevent testify command line args from leaking into my Cobra application?
This is my test:
// A test of TestSuite
func (ts *TestSuite) TestMyThing() {
cmd := RootCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetErr(b)
err := cmd....
4
votes
1
answer
5k
views
How to avoid timeout in go test after 10m
I'm currently running a suite of tests in Go using the go test ./... command. This suite contains multiple test functions, and the execution time exceeds 10 minutes. Unfortunately, the go test command ...
2
votes
1
answer
41
views
Form variables not available in testing
I am a Go newbie. I have written an API server built on the Echo server, using the DeepMap OpenAPI generator and Postgres using pgxpool. It is working well enough and has been in use for a year, but ...
2
votes
0
answers
611
views
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 ...
-1
votes
1
answer
156
views
hex-arc golang - overcome import cycling error when testing
I am having trouble testing foo_handler in my Go project. My project has the following structure:
├── Makefile
├── cmd
│ └── main.go
├── go.mod
├── go.sum
└── internal
├── api
│ ├── router....
1
vote
1
answer
541
views
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 -...
0
votes
1
answer
1k
views
How to write unit test when a member function is calling another member function of the same object in golang?
In golang, I often see a member function calling another member function of the same object. For example check following golang code:
type Obj struct{}
func (o Obj) First() {
// do something
...
1
vote
0
answers
65
views
`gopls` disagrees with `go test` on interface implementation
Consider the following code:
package ifaces
import (
"fmt"
"testing"
)
type IFace1 interface {
Do1()
}
type IFace2 interface {
...
5
votes
1
answer
1k
views
How do you test filepath.Abs failure in your Golang code?
In my Go code, I have to use filepath.Abs() several times, which may lead to different errors that my method returns.
func (s *service) myFunc(path string) error {
dir := s.Component().Dir()
...
2
votes
0
answers
564
views
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
}...
-1
votes
1
answer
3k
views
How to run a compiled Go test binary?
I compiled a Go test library by running go test -c ./model. According to Go docs it can be run using go run -exec xprog command, however, I keep getting errors while trying to run my generated model....
0
votes
2
answers
2k
views
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....
1
vote
0
answers
188
views
Go test coverage for test files in separate module [duplicate]
I'm running gotestsum --junitfile-testcase-classname=short --junitfile-testsuite-name=relative \ -- -coverprofile=coverage.out ./serviceName/...
The structure looks like this:
serviceName
...
5
votes
0
answers
474
views
Do all tests have to pass to get code coverage in Go?
I am using the -cover to collect coverage in my Go project. However, it seems that the tests have to be 100% success rate in order to get the coverage report. Is there any way I can get the coverage ...
1
vote
1
answer
89
views
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
├──...
1
vote
1
answer
703
views
How to collect k8s pods logs in parallel using golang only for a duration of time
I am new to golang, I have a task to collect the application logs, application is running as deployment in k8s cluster, there are 4 pods in total.
As part of test automation, I need to collect the ...
1
vote
1
answer
181
views
Temporary filesystem only visible to the process in Go
I am writing e2e tests for a command line app where I have to do file manipulation (such as cp, mv, rm, touch, and mkdir). The tests can execute just fine in my local environment. The problem occurs ...
1
vote
0
answers
360
views
Go build constraints to run if none set
I have many classes of test, which are marked with build constraints, e.g.:
//go:build all || (!foo && !bar && !baz && quux)
for the test class quux. This is okay, as far as ...
1
vote
1
answer
3k
views
Function like assert.Contains by stretchr/testify but ignore case and whitespace
For example, I have this test here
assert.Contains(t, "HELLO WORLD", "hello world)
and I want this to return true. Obviously, I can clean up the string with strings.TrimSpace(), ...
7
votes
1
answer
7k
views
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 ...