Questions tagged [testify]

0

votes
0

answer
6

Views

How to test a http function which takes folder as an input?

I have a http handler function(POST) which allows a user to upload a folder from a web browser application. The folder is passed from javascript code as array of files in folder and on backend (golang API) it is accepted as []*multipart.FileHeader. I am struggling in writing a go unit test for this...
akshita babel
1

votes
1

answer
1.1k

Views

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 functions to Create/Get/Update a User (I'm using GORM but this is n...
Alessio
1

votes
1

answer
198

Views

Initialization loop Golang

Hi I'm trying to make my function unit testable. One of the suggestion was to assign the function into a variable and make it globally accessible. I just did that but now I'm experiencing a Initialization loop below is my code //////////////////////// // main.go //////////////////////// func Downloa...
MadzQuestioning
1

votes
1

answer
51

Views

Mock Interface function not getting called

I'm trying to write Go Unit Test using testify mocking library. I was following this blog http://goinbigdata.com/testing-go-code-with-testify/. I have passed the mocked interface to the newCalculator function but still Random1 of Random interface is getting called instead of Random1 function of stru...
Shubham Singh
1

votes
1

answer
150

Views

Is there a way to chain asserts with testify?

I really like what testify brings to go test. However, I dug through the documentation and didn't see anything on how to handle multiple asserts. Does Go handle 'first failure', in the sense it fails at the first bad assert, or will it only concern itself with the last assert in the test method?
18

votes
6

answer
7.6k

Views

Unit testing for functions that use gorilla/mux URL parameters

Here's what I'm trying to do : main.go package main import ( 'fmt' 'net/http' 'github.com/gorilla/mux' ) func main() { mainRouter := mux.NewRouter().StrictSlash(true) mainRouter.HandleFunc('/test/{mystring}', GetRequest).Name('/test/{mystring}').Methods('GET') http.Handle('/', mainRouter) err := htt...
5

votes
2

answer
696

Views

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, 'Bar') } The error I am getting: Error: Should be true Messages: Th...
Buzzy
3

votes
2

answer
4.3k

Views

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 conversion. Bad luck or is there a way?
Schultz9999
3

votes
1

answer
1.1k

Views

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 interface and requires me to write a whole new test...
nitimalh
2

votes
0

answer
405

Views

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 in-order? gomock has *Call.After(*Call) for this, but I can't...
barrucadu
1

votes
1

answer
315

Views

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 // Read the config file jsonBytes, err := ioutil.ReadFile(configF...
Mike Warren
2

votes
1

answer
618

Views

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 testableFunction func TestFunction(t *testing.T){ ... res:=testable...
ArkadyB
1

votes
1

answer
1.7k

Views

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 of: /Users/[username]/go/src/github.com/[group_name]/[project_name]/vendor/github.com/...
jj1111
2

votes
2

answer
923

Views

testify (Golang) seemingly running test suites concurrrently?

Basically I created a new test file in a particular package with some barebone 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 struct. This immediately caused all our other tests to s...
Zhuiguang Liu
2

votes
1

answer
5.9k

Views

How to test Golang channels / go-routines

I have a type that contains a byte of data, and takes a channel to post new data there. Other code can read the last written byte of data using a Read function. Edit: for actual, runnable code, see https://github.com/ariejan/i6502/pull/3 especially files acia6551.go and acia6551_test.go. Tests resu...
Ariejan
57

votes
4

answer
24.6k

Views

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 external resources and thus run slower). So, I want...
Craig Jones
2

votes
1

answer
165

Views

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 and actual have a relative error less than epsilon And...
Carmageddon