Questions tagged [go-testing]

121 questions
13
votes
2 answers

Go: Wrong coverage when there is no tests for a package

I have a Go project with the following structure: foo/foo.go foo/foo_test.go main.go As you notice, there is no test for main.go. I collect the coverage report using the following command: go test ./foo ./ -coverprofile=coverage.txt…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
9
votes
1 answer

How to use dynamic location for godotenv.Load() .env file?

Problem I am building a REST API in Go. The godotenv package is used to load the environment variables. Running go run main.go, the project runs the API as expected, the environment variables are loaded. However, when wanting to run the test using:…
Eric Landheer
  • 2,033
  • 1
  • 11
  • 25
6
votes
2 answers

Go mocking with interfaces for testing

I'm pretty new with Go, and I'm coming from OOP languages. Now the concept seems quite different in go of interfaces and classes. I was wondering how mocking would work in case of testing. The confusion I'm having is whether ok to use struct as a…
LeTadas
  • 3,436
  • 9
  • 33
  • 65
5
votes
0 answers

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 despite the failed tests?
cxc
  • 201
  • 2
  • 10
5
votes
1 answer

how to override image registry urls that testcontainer uses

I am getting error [ERROR] when trying to pull image through test container. Reason CI machines in your organisation have access to a common registry server and is not allowed to talk to external web. Testcontainer java has something like for this…
eswaat
  • 733
  • 1
  • 13
  • 31
5
votes
0 answers

Why does Go test with blocked channel not reporting deadlock

I met a strange issue when doing test with channels. In a normal main function, the following code will report the deadlock error. package main import ( "fmt" ) func main() { c := make(chan int) c <- 1 fmt.Println(<-c) } But on…
user163947
4
votes
0 answers

VS Code - Go: Prevent removal of test coverage markings after save

I'm using VS Code for a Go project. I have it configured to show the code coverage with gutter markings after I run the tests. The issue I'm having is that if I make any changes to a file in the package, and then save it, VS Code then removes all of…
Jordan
  • 3,998
  • 9
  • 45
  • 81
4
votes
1 answer

Golang Mocking a call with some parameters in body with values not fixed

I am mocking a method call as follows: tctx.someMock.On("addProd", product.NewAddProductParamsWithContext(ctx). WithID("someid"). WithCreateRequest(pro.CreateProdBody{ creationDate: "someDate" , …
user1892775
  • 2,001
  • 6
  • 37
  • 58
3
votes
1 answer

Difference between differents test-flags of go test

I am planning to run my Cucumber test in go (using Godog) & I came up with the following possibility of commands to run my tests. Can someone point out the differences here? What is the recommended way & what's the use-case of each cover mode…
Pulkit Gupta
  • 53
  • 1
  • 6
3
votes
1 answer

How to spin off a test container for ArangoDB in GO code?

I'm trying to use https://golang.testcontainers.org to setup a docker container from the image that I specify, while the code is in execution. The container spun out, will be cleaned up before the program terminates, as mentioned in the above…
3
votes
0 answers

getting cross-package coverage in golang without init() running

I have inherited a very large, very old golang codebase. I'm trying to get coverage stats and most of the tests are testing across packages. If I run go test ./... the tests pass. If i run go test -coverpkg=./... ./... then the tests fail because…
Shiny
  • 111
  • 1
  • 4
3
votes
0 answers

Unable to read messages from topic if context timeout I set- segmentio/kafka-go reader

I have a Kafka ConsumerGroup (Golang/Segmentio) with one reader as part of unit test cases r := kafka.NewReader(kafka.ReaderConfig{ Brokers: []string{"localhost:9092"}, Topic: "test", CommitInterval:…
3
votes
1 answer

Best practices for external benchmarks when using Go Modules

I have a Go repository, and within it I have some benchmarks (in a _test suffixed package). These benchmarks compare it to, among other things, some third party libraries. I am not using these libraries in my non-benchmark code. I am now migrating…
Javier Zunzunegui
  • 363
  • 1
  • 3
  • 10
2
votes
1 answer

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 that's not to say it's written properly…
Drew
  • 111
  • 1
  • 4
2
votes
1 answer

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() absDir, err := filepath.Abs(dir) if err !=…
Apollo
  • 1,296
  • 2
  • 11
  • 24
1
2 3
8 9