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
4
votes
2 answers

How can I mock Stripe in Go?

I am trying to mock Stripe for some tests. //testify mock type Backend struct { mock.Mock } func (s Backend) Call(method, path, key string, params stripe.ParamsContainer, v interface{}) error { args := s.Called(params) return…
mryan
  • 382
  • 1
  • 4
  • 18
4
votes
1 answer

Go unit tests - call to database transaction Begin, was not expected error

I'm trying to write model unit tests in Go using Data Dog's go-sqlmock and testify. I have the following code for that: type Suite struct { suite.Suite DB *gorm.DB mock sqlmock.Sqlmock repository Repository user…
Gambit2007
  • 3,260
  • 13
  • 46
  • 86
4
votes
1 answer

Testing a method that loads JSON config files in Golang

In the Golang project under test, there's a method that loads a JSON config file into a variable. Its code is like this: // Load the JSON config file func Load(configFile string, outputObj interface{}) *errors.ErrorSt { var err error //…
Mike Warren
  • 3,796
  • 5
  • 47
  • 99
4
votes
1 answer

Table driven tests with testify mock

Are there any examples of writing clean table driven tests using testify. A table driven test for input and expected output works well but having to test the output from a dependency seems to be really hard to do. The below example uses one mocked…
nitimalh
  • 919
  • 10
  • 26
4
votes
1 answer

Go: Run test from multiple package with DB initialization

I have a GO project with this project structure (multiple couples of this kind of files in each package). - api - userHandler.go - userHandler_test.go - database - user.go - user_test.go Inside user.go I have the User struct and the…
Alessio
  • 2,018
  • 25
  • 26
3
votes
1 answer

Writing Tests in Go by Receiving Interfaces and Returning Structs

I'm having some issues implementing interface substitution for the Go code. Previously the code was not written with unit tests in mind and I am going back and adding support for testing (Testify and Mockery) and writing all of the Go tests for my…
3
votes
2 answers

Check dynamic value for field in struct in go using testify

I have a user service that validates the user data and formats it and then calls Firebase service which creates a firebase user and return firebase id and then it pass that user data to repository layer. My user struct has an ID field is populated…
Amal Jose
  • 75
  • 8
3
votes
2 answers

Cannot pass Testify Mock object error

Hi I'm trying to mock a struct in GO. I'm using testify to do this. But I can't seem to get it to work and don't now what I'm doing wrong. Below is the sample main.go and main_test.go file I have // Arithmetic ... type Arithmetic interface { …
MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
3
votes
2 answers

Testify Mock a function return inside a function

I would like to Mock the response of a function. But this function is located or called inside another function. Let say I have this function // main.go func TheFunction() int { // Some code val := ToMockResponse() return val } func…
MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
3
votes
3 answers

How to validate that a method is called in a separate go routine

While writing unit test for a method in go, I am stumped at a problem. First, code snippet under test: func MehodToBeTested(e Entity) { go saveAudit(e) //do something on which assertions can be done } Entity can be mocked. in the saveAudit…
zaRRoc
  • 345
  • 1
  • 7
  • 18
3
votes
1 answer

Golang testing of functions declared as variable (testify)

I have a problem firing a function declared as variable in golang with testify. Test and function both declared in same package. var testableFunction = func(abc string) string {...} now i have a different file with unit test calling…
ArkadyB
  • 1,265
  • 1
  • 19
  • 37
3
votes
2 answers

Testify is seemingly running test suites concurrrently?

Basically I created a new test file in a particular package with some bare bones test structure - no actual tests...just an empty struct type that embeds suite.Suite, and a function that takes in a *testing.T object and calls suite.Run() on said…
Zhuiguang Liu
  • 510
  • 5
  • 8
2
votes
0 answers

How to write unit test case for a Golang function in which one or more function calls are there and they have to be mocked?

I am new to Golang. I am trying to write unit test cases for the functions that I implemented. Here is one such, func SaveProjectInfo(projectInfo processor.ProjectInfo, configMap map[string]string, client *mongo.Client) error { collection :=…
Pramit Pakhira
  • 135
  • 1
  • 8
2
votes
1 answer

Asserting That Code Should Panic With Logrus Error

When using logrus, how do you assert for panic using an assertion library like testify? Take the following code for an example: var mylogger = logrus.WithContext("MyLogger") func LoadPreparedStatements() { db := psql.PsqlClient() var err…
PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
2
votes
1 answer

How to use custom flag in tests (with `testify/suite`)

I want to add custom flags for my Go tests that use testify/suite. It looks like from this thread that it can only be in TestMain() (init() if it is before Go 1.13). However, with the testify/suite pacakge, TestMain() is not quite an option. I have…
cxc
  • 201
  • 2
  • 10