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
0
votes
1 answer

Unit Testing Go Functions having ORM interactions

I have written a function: func AllItems(w http.ResponseWriter, r *http.Request) { db, err := gorm.Open("sqlite3", "test.db") if err != nil { panic("failed to connect database") } defer db.Close() var items [] Item …
FlyingAura
  • 1,541
  • 5
  • 26
  • 41
0
votes
2 answers

Cannot run individual tests in a test suite inside GoLand IDE?

Am using testify's test suite support to write unit tests. This results in my test file having a single TestFooBar(t *testing.T) that kicks of the suite.Run while all my individual tests become a part of my test suite struct with method signatures…
amitsaurav
  • 531
  • 1
  • 5
  • 18
0
votes
1 answer

Custom testify output for failing test of xml / strings

I'm testing XML marshaling with testify and using strings.Contains to check if lines I expect to be included in the XML are in fact there. However, I want to diff the actual vs. desired xml. Currently, my code looks something like: func (suite…
daino3
  • 4,386
  • 37
  • 48
-1
votes
1 answer

Run http.ListenAndServe() On Tests using stretchr/testify suite Stop Test From Proceed

I'm trying to create integration tests for my REST API application which I made using gorilla/mux, gorm.io and golang-migrate/v4 For the tests I'm using testify. My SetupSuite() in my integration_Test.go is like this: func (s…
-1
votes
1 answer

Testify failing assertion for mocks when passing the correct args

Golang beginner here. I am trying to write tests for my struct which eventually mocks out the stats interface. The relevant part of my code looks below as far as the tests go. // Test code type MockStats struct { mock.Mock } func (m *MockStats)…
nerdy_darkknight
  • 615
  • 1
  • 8
  • 16
-1
votes
1 answer

How to compare two timestamps - differing only because one has a timezone?

I'm trying to test that the time.Time value I inserted into a postgres database is the same one I am querying out. Postgres drops the timezone though, so I'm wondering how can I get this testify/assert test to pass? s.Equal(want.Date, got.Date) Both…
user9503053
  • 107
  • 3
  • 15
-1
votes
1 answer

Mock functions in golang for testing

I want to write unittest case for CreateData() function, by mocking FetchAllData() and SaveData() which are in someother package(package2), please help me to mock the function with example, Thanks in advance func CreateData(input…
sp007
  • 1
  • 2
-1
votes
1 answer

how to mock database layer in golang using testify/mock

I am trying to run unit test on a server, and using "github.com/stretchr/testify/mock" for mocking database layer. Putting all the code here will make it very messy so i created a small project which will give the idea of my code structure and what…
utkarsh tyagi
  • 635
  • 1
  • 9
  • 27
-1
votes
2 answers

Create a mock function

Hi I would like to test or Mock a certain function and return a Mock response for this. To demonstrate below is my code Sample.go package main import ( "fmt" log "github.com/sirupsen/logrus" ) var connectDB = Connect func Sample() { …
MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
-2
votes
2 answers

Cannot find package when running golang test

When I run go tests I get the error: cannot find package "github.com/stretchr/testify/assert" in any of: /usr/local/Cellar/go/1.17.6/libexec/src/github.com/stretchr/testify/assert (from $GOROOT) …
Andrew Smith
  • 483
  • 1
  • 3
  • 16
-2
votes
1 answer

Is there a way to AssertCalled every call when a function is called multiple times

I am trying to unit test with stretchr/testify for code like the following: func (c *MyClient) upsertData(data MyObject) { upsertToDatabase(data) } func doSomething(c *MyClient) { data1, data2 := getSomeData() c.upsertToDatabase(data1) …
3tbraden
  • 667
  • 1
  • 10
  • 21
-2
votes
1 answer

Understanding Test Coverage

I have a simple package in my Go program to generate a hash ID. I've also written a test for it but not able to understand why I'm only getting 83% of statements covered. Below is my package function code: package hashgen import ( "math/rand" …
Luca Brasi
  • 681
  • 2
  • 12
  • 28
-2
votes
3 answers

Error `The code you are testing needs to make 1 more call(s)` in testify package

I am using the testify package for the unit testing in golang. My code contains mocking. while running the test it is getting passed for sometimes and showing error The code you are testing needs to make 1 more call(s) for sometimes, I am unable to…
1 2 3 4 5 6
7