Skip to content

Commit

Permalink
fix: example tests (#27)
Browse files Browse the repository at this point in the history
This fixes two problems in the example tests:
1. They apparently weren't being picked up because of the underscore
prefix of the `_examples` folder ;
2. On `TestApi`, the test's `ResponseRecorder` instance kept in the
`suite` struct wasn't being reset which was leading to it mixing up
responses from different test cases, hence making tests fail (the
failures weren't being caught by CI because of the underscore issue
outlined above).

This PR fixes above issues by renaming `_examples` to `examples` (1) and
by adding a `Before` hook in the tests to reset the response recorder
(2).
  • Loading branch information
GCrispino committed Jan 8, 2024
1 parent 620b666 commit 8bbcc34
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"regexp"
"testing"

"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"

"github.com/regen-network/gocuke"
)

func TestCustomSteps(t *testing.T) {
gocuke.NewRunner(t, &customStepsSuite{}).
Path("_examples/simple/simple.feature").
Path("examples/simple/simple.feature").
Step(`I have (\d+) cukes`, (*customStepsSuite).A).
Step(regexp.MustCompile(`I eat (\d+)`), (*customStepsSuite).B).
Step(`I have (\d+) left`, (*customStepsSuite).C).
Expand Down
File renamed without changes.
10 changes: 8 additions & 2 deletions _examples/api/api_test.go → examples/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"net/http/httptest"
"testing"

"github.com/regen-network/gocuke"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/regen-network/gocuke"
)

type suite struct {
Expand All @@ -19,7 +20,12 @@ type suite struct {

func TestApi(t *testing.T) {
scope := &suite{TestingT: t, resp: httptest.NewRecorder()}
gocuke.NewRunner(t, scope).
run := gocuke.NewRunner(t, scope)
run.Before(func() {
scope.resp = httptest.NewRecorder()
})

run.
Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, scope.ISendRequestTo).
Step(`^the response code should be (\d+)$`, scope.TheResponseCodeShouldBe).
Step(`^the response should match json:$`, scope.TheResponseShouldMatchJson).
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestSimple(t *testing.T) {
gocuke.NewRunner(t, &simpleSuite{}).Path("_examples/simple/simple.feature").Run()
gocuke.NewRunner(t, &simpleSuite{}).Path("examples/simple/simple.feature").Run()
}

type simpleSuite struct {
Expand All @@ -31,7 +31,7 @@ func (s *simpleSuite) IHaveLeft(a int64) {

// test if a struct that doesn't use a pointer and a global var
func TestSimpleNonPointer(t *testing.T) {
gocuke.NewRunner(t, simpleSuiteNP{}).Path("_examples/simple/simple.feature").Run()
gocuke.NewRunner(t, simpleSuiteNP{}).Path("examples/simple/simple.feature").Run()
}

var globalCukes int64
Expand Down

0 comments on commit 8bbcc34

Please sign in to comment.