Questions tagged [testify]

A sacred extension to the standard go testing package

Testify is a set of golang packages that make testing your Golang code easier.

  • Easy assertions
  • Mocking
  • HTTP response trapping
  • Testing suite interfaces and functions

More info can be found at github.com/stretchr/testify

104 questions
149
votes
6 answers

Separating unit tests and integration tests in Go

Is there an established best practice for separating unit tests and integration tests in GoLang (testify)? I have a mix of unit tests (which do not rely on any external resources and thus run really fast) and integration tests (which do rely on any…
Craig Jones
  • 2,428
  • 2
  • 17
  • 11
52
votes
5 answers

Unit testing for functions that use gorilla/mux URL parameters

TLDR: gorilla/mux used to not offer the possibility to set URL Vars. Now it does, that's why the second-most upvoted answer was the right answer for a long time. Original question to follow: Here's what I'm trying to do : main.go package…
32
votes
2 answers

How to mock for same input and different return values in a for loop in golang

I'm running a test with multiple parameters in a for loop using go lang testing. I ran into a situation where same return value (and first set) is returned every time the mock is called. What I want to be able to do is change the return value for…
TechCrunch
  • 2,924
  • 5
  • 45
  • 79
16
votes
2 answers

Can I run a single test in a suite?

I have setup a test suite for my struct (https://github.com/stretchr/testify#suite-package). Before I was able to run a single test by specifying just a pattern: go test -v ./services/gateways/... -run mytest This approach doesn't work after…
Schultz9999
  • 8,717
  • 8
  • 48
  • 87
13
votes
1 answer

golang test error: cannot find package "github.com/stretchr/testify/assert" in any of:

My import looks like this below: import ( "testing" "github.com/stretchr/testify/assert" ) when I try try running 'go test' I get the error message: cannot find package "github.com/stretchr/testify/assert" in any…
jj1111
  • 607
  • 2
  • 10
  • 20
12
votes
4 answers

Error: suite.go:61: test panicked: reflect: Call with too few input arguments

I'm setting unit test in golang. But now I'm facing error when running go test -v. I want to resolve this error and make test success. article ├ client ├ api │ ├ main.go │ ├ contoroller │ │ ├ contoroller.go │ │ └…
jpskgc.v3
  • 649
  • 5
  • 8
10
votes
4 answers

AssertCalled always fails with testify library

I am using testify to test my code and I want to check if a function was called. I am doing the following: type Foo struct { mock.Mock } func (m Foo) Bar() { } func TestFoo(t *testing.T) { m := Foo{} m.Bar() m.AssertCalled(t,…
Buzzy
  • 2,905
  • 3
  • 22
  • 31
8
votes
1 answer

How to assert that mocked method calls happen in-order with testify?

The documentation for AssertExpectations says "AssertExpectations asserts that everything specified with On and Return was in fact called as expected. Calls may have occurred in any order." What if I want to assert that some calls happen…
barrucadu
  • 556
  • 4
  • 8
6
votes
1 answer

How to assert a partial match with stretchr/testify/mock AssertCalled?

Consider this unit test file in Go. I'm using github.com/stretchr/testify/mock package. type Person struct {Name string; Age int} type Doer struct { mock.Mock } func (d *Doer) doWithThing(arg Person) { fmt.Printf("doWithThing %v\n", arg) …
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
6
votes
1 answer

Mocking via stretchr/testify, different return args

Function below describes how to mock using testify. args.Bool(0), args.Error(1) are mocked positional return values. func (m *MyMockedObject) DoSomething(number int) (bool, error) { args := m.Called(number) return args.Bool(0),…
downvoteit
  • 588
  • 2
  • 7
  • 12
6
votes
1 answer

Difference between InDelta and InEpsilon

From documentation: https://godoc.org/github.com/stretchr/testify/assert#InDelta InDelta asserts that the two numerals are within delta of each other https://godoc.org/github.com/stretchr/testify/assert#InEpsilon InEpsilon asserts that expected…
Carmageddon
  • 2,627
  • 4
  • 36
  • 56
5
votes
1 answer

How to run testify setup/teardown with table tests using stretchr/testify in golang?

I'm using testify for testing. I've set up a suite for my tests. However, I'm having trouble using the set up and tear down features when using table tests. Is this by design? package workflows import ( "testing" log…
Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
5
votes
1 answer

Golang mock context panic

So I'm making unit test in golang using mockery and testify The test code goes like this: const bufSize = 1024 * 1024 var lis *bufconn.Listener var mockAccountService = &mocks.AccountService{} func init() { lis = bufconn.Listen(bufSize) s…
4
votes
1 answer

assert: mock: I don't know what to return because the method call was unexpected Error while writing unit test in Go

I am using testify in Go to write unit tests for my service methods and all the methods are working fine excepted the update method because in the update method I call another method("GetByID") of the same service inside the Update…
Waleem Ali
  • 43
  • 4
4
votes
0 answers

Testify mock ExpectedCall does not match expected call in gin handler, panics

I'm using gin-gonic for a server, and testify for testing and mocks, along with "testing" and "net/http/httptest" The part of the interface that mocks the method: func (m *MockInterface) Method(ctx context.Context, id string, deleted bool)…
Laura
  • 288
  • 1
  • 4
  • 14
1
2 3 4 5 6 7