Questions tagged [go-testing]
121 questions
-1
votes
2 answers
Variable in It spec text always 0 although changed in BeforeEach in ginkgo/gomega
In my code snippet below or https://play.golang.org/p/tLld-zNF2zp, testValue was declared inside Describe block, changed in BeforeEach block. Then, used in.
It
Expect
The test passes as expected. From the debug log, It always shows testValue…

AEWRocks
- 157
- 1
- 2
- 10
-1
votes
1 answer
How to use go test flag to execute test and bench function correctly?
I learn from the go flags from Testing flags
And I write some test files in my project.
This is my service folder:
service
family_limit_settings.go
family_limit_settings_test.go
xxxxxx.go (others go source files)
xxxxxx_test.go (others go test…

Linuxea
- 319
- 1
- 4
- 15
-1
votes
1 answer
How to initialize test data for benchmark test in golang?
When I write benchmark test for my algorithm, I was confused by an problem!
My test code detail was pushed to github and I copy it to here and add some comments.
https://github.com/hidstarshine/Algorithm/blob/master/leet/problem24_test.go
var…

axlis
- 395
- 1
- 3
- 13
-1
votes
1 answer
Spread or unpack struct as input parameter for test
I want to shorten my test code by using a struct for test input and another struct for test desired output for complex tests.
I have my test code like this and it works:
func TestMyTest(*testing.T) {
type Test struct {
n int
…

Viet
- 6,513
- 12
- 42
- 74
-1
votes
1 answer
GO attach stdin in test setup method
I am using the built-in testing module to run some functional tests I have in my GO project. In my project I have external dependencies, which I connect to in my TestMain method. I save these connections to variables which then I use in the tests…

shiznatix
- 11
- 4
-1
votes
3 answers
Mocked method not working in golang while running the test cases
I am trying to mock an struct method in test cases but it is not working.
I want to mock Validate method here:
`
package main
import (
"fmt"
)
type DemoInterface interface {
Inc(int) (int, error)
Validate(int) error
}
type…

Prakash Kumar
- 2,554
- 2
- 18
- 28
-2
votes
1 answer
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.test binary. What is the full command here?
P.S I…

Hacker
- 161
- 2
- 8
-2
votes
1 answer
How to Import https://github.com/go-test/deep into Your Golang Test
I'm new to Golang.
My go version: 1.17.8
Trying write tests to compare strcuts.
Found this lib can show you which fields exactly equivalent.
So thought of using it over reflect.DeepEqual
Here's my test go file,
package main
import (
"os"
…

samme4life
- 1,143
- 2
- 14
- 20
-2
votes
1 answer
How to make the function suit the _test file?
Here was my code
type Queue interface {
Push(key interface{})
Pop() interface{}
Contains(key interface{}) bool
Len() int
Keys() []interface{}
}
type QueueData struct {
size int
data []interface{}
}
func New(size int)…

Panji Tri Wahyudi
- 472
- 1
- 9
-2
votes
1 answer
Unit testing in go
I want to test the API function but arguments give the problem.
func SetAPIConfigHandler(w http.ResponseWriter, r *http.Request) {
var apiConfig model.Configuration
err := json.NewDecoder(r.Body).Decode(&apiConfig)
if err != nil {
…

SEETHARAMU G N
- 11
- 4
-2
votes
1 answer
golang test fails on flags parse for test.v
In my go program, the main method does:
port := flag.Int("port", 8080, "Port number to start service on")
flag.Parse()
I have a dummy test that looks as:
func TestName(t *testing.T) {
fmt.Println("Hello there")
}
when I run my tests (from the…

Marwan Jaber
- 611
- 11
- 27
-2
votes
1 answer
Global var struct updates not available in other testing functions
I'm testing an API where I need to "login" and get an Auth Token that I store in a struct and pass to my functions. I'm trying to write a '_test.go' file, but the Auth Token is not getting passed to the testing functions, and I'm not sure why. All…

JD Allen
- 799
- 5
- 12
-2
votes
1 answer
How to skip a failed test
In go, are you allowed skip a test that has already failed?
Context:
I have a heisenbug which I cannot currently determine the cause of. It causes some tests to sometimes fail.
By examining various logs I can identify the failure mode. I would like…

Bruce Adams
- 4,953
- 4
- 48
- 111
-3
votes
1 answer
Where store Tests (project structure - best practice)?
We have many different ways to implement project structure in GO.
My question is where the best way to store tests implementation:
separately (as Java Maven/Gradle standard)
├── pkg
│ ├── colocator
│ │ ├── some_impl.go
│ │ └── ...
│ ├──…

kozmo
- 4,024
- 3
- 30
- 48
-4
votes
2 answers
Why is WaitGroup.Wait() hanging when using it with go test?
Here's a simple example of what I mean
package main
import (
"sync"
"testing"
"time"
)
func TestWaitGroup(t *testing.T) {
var wg sync.WaitGroup
quitSig := make(chan struct{})
go func(wg sync.WaitGroup, quitChan, chan…

Paul Côté
- 75
- 1
- 10