Questions tagged [go-testing]
121 questions
1
vote
0 answers
Checking external private errors
I have a package which does some error checking for specific error types. One of the errors is from an external package, its fields are all unexported as well as the relevant creation func, so I cant instantiate it in my test, like below.
type…

Person1
- 109
- 2
- 10
1
vote
1 answer
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 application logs (only for the duration of my test…

Karthic.K
- 416
- 5
- 15
1
vote
0 answers
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 it goes, but what I'd really rather have is constraints such that the…

YoureIt
- 21
- 1
1
vote
1 answer
net/rpc server stay registered when running test more than once with the 'count' flag
The program creates a rpc server and a client and expose several methods via the rpc interface.
Several test functions testing one of the methods each.
The first test function register the rpc server:
RPCServer := new(RPCServer)
…

sivan shani
- 87
- 1
- 8
1
vote
1 answer
How to handle json syntax error in a go test case?
I'm testing a scenario where json.Unmarshall fails and returns
&json.SyntaxError{msg:"unexpected end of JSON input", Offset:0}
the code is like this:
err = json.Unmarshal(input, &data)
if err != nil {
return nil, err
}
test case is expecting…

jimmy
- 457
- 5
- 20
1
vote
2 answers
Check if the header has been assigned to the request in Go unit-testing
I am trying to test the following line of code:
httpReq.Header.Set("Content-Type", "application/json")
I am mocking the request to an external api in this way:
httpmock.RegisterResponder(http.MethodPost, "do-not-exist.com",
…

unitSphere
- 241
- 3
- 17
1
vote
1 answer
How to implement unit test with a code generated by go swagger
I have two questions on this go swagger generated codes, firstly I made my first api with go swagger but my employer asked me to implement the unit (go test) but trying to perform the usual http test does not work,
here is my test code bellow
//…

Anachunam Michael
- 23
- 5
1
vote
1 answer
First golang test run is slow
I'm running a very simple test adding two numbers.
package internal
import "testing"
func TestAddingNumbers(t *testing.T) {
if add(1, 5) != 6 {
t.Errorf("Failed Adding numbers")
}
}
First
go test -v file.go file_test.go runs in =>…

nono
- 2,262
- 3
- 23
- 32
1
vote
2 answers
go test fails when providing -coverpkg parameter
I'm trying to get the test coverage across all packages in my project.
tests are getting executed successfully and report the coverage when I execute the following command.
go test -cover ./...
but all the tests are failing when I execute go test…

srimaln91
- 1,176
- 10
- 21
1
vote
1 answer
Why my test with Gin and Go is not passing?
I am trying to test my API.
In this case, I have this main file:
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := getRouter()
r.Run(":8080")
}
func getRouter() *gin.Engine {
// We…

kike
- 658
- 1
- 8
- 26
1
vote
1 answer
Why is the response body empty when running a test of mux API?
I am trying to build and test a very basic API in Go to learn more about the language after following their tutorial. The API and the four routes defined work in Postman and the browser, but when trying to write the test for any of the routes, the…

Ash
- 3,030
- 3
- 15
- 33
1
vote
0 answers
using Golang context.WithValue in testing for http request
I am trying to do unit testing, I am using GraphQL gqlgen for this case with Echo, I have been struggle to do test with my context, I can't add header in my testing due the GraphQL not using the server endpoint
here is my code to set the context…

Weq Iou
- 233
- 4
- 13
1
vote
1 answer
Mock function without receiver
I have the file util.go:
func Foo(service *SomeService) error {
return helper(service)
}
func helper(service *SomeService) error {
...
}
I'm writing unit tests using testify, starting with Foo. I want to:
mock helper
assert mocked helper…

onepiece
- 3,279
- 8
- 44
- 63
1
vote
1 answer
Issue with go package declaration containing more than 2 words separated by underscore
everyone, I'm confused by what I'm seeing; I have the following tree:
├── go.mod
├── main.go
└── server
├── server.go
└── server_integration_test.go
Let's say my module name (mod.go) is gotest. Content of server.go:
package server
type…

Farzad
- 1,770
- 4
- 26
- 48
1
vote
1 answer
Go: How to unit test a piece of code that works with Mongo collections?
I am using the official Mongo driver for Go. My code looks like this (omitted error handling in order to make the example simpler):
type DB struct {
collection *mongo.Collection
}
func (db DB) GetUsers() []*User {
res, _ :=…

Sasha Shpota
- 9,436
- 14
- 75
- 148