Skip to content

Commit 7017c73

Browse files
mrsheepukb0ch3nski
andauthored
Provide a useful implementation of something compatible with testing.T (#571)
* Attempting to provide a useful implementation of something compatible with testing.T * Handle Fail calls on the TestingT in the right place * Provide as much of testing.T as possible + tidy up * Add initial tests for testingT support * Check compatibility with testing.T and friends Co-authored-by: Piotr Bocheński <[email protected]> * Update assert-godogs example to show new usage. Rename 'GetTestingT(ctx)' to 'T(ctx)' * Update changelog and readme with new usage * Improve test coverage * Review updates --------- Co-authored-by: Piotr Bocheński <[email protected]>
1 parent 10407bc commit 7017c73

File tree

12 files changed

+562
-90
lines changed

12 files changed

+562
-90
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
88

99
## Unreleased
1010

11+
### Added
12+
- Provide testing.T-compatible interface on test context, allowing usage of assertion libraries such as testify's assert/require - ([571](https://github.com/cucumber/godog/pull/571) - [mrsheepuk](https://github.com/mrsheepuk))
13+
1114
## [v0.14.0]
1215
### Added
1316
- Improve ErrSkip handling, add test for Summary and operations order ([584](https://github.com/cucumber/godog/pull/584) - [vearutop](https://github.com/vearutop))

README.md

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -495,31 +495,12 @@ If you want to filter scenarios by tags, you can use the `-t=<expression>` or `-
495495
A more extensive example can be [found here](/_examples/assert-godogs).
496496

497497
```go
498-
func thereShouldBeRemaining(remaining int) error {
499-
return assertExpectedAndActual(
500-
assert.Equal, Godogs, remaining,
501-
"Expected %d godogs to be remaining, but there is %d", remaining, Godogs,
502-
)
503-
}
504-
505-
// assertExpectedAndActual is a helper function to allow the step function to call
506-
// assertion functions where you want to compare an expected and an actual value.
507-
func assertExpectedAndActual(a expectedAndActualAssertion, expected, actual interface{}, msgAndArgs ...interface{}) error {
508-
var t asserter
509-
a(&t, expected, actual, msgAndArgs...)
510-
return t.err
511-
}
512-
513-
type expectedAndActualAssertion func(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool
514-
515-
// asserter is used to be able to retrieve the error reported by the called assertion
516-
type asserter struct {
517-
err error
518-
}
519-
520-
// Errorf is used by the called assertion to report an error
521-
func (a *asserter) Errorf(format string, args ...interface{}) {
522-
a.err = fmt.Errorf(format, args...)
498+
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
499+
assert.Equal(
500+
godog.T(ctx), Godogs, remaining,
501+
"Expected %d godogs to be remaining, but there is %d", remaining, Godogs,
502+
)
503+
return nil
523504
}
524505
```
525506

_examples/assert-godogs/godogs_test.go

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"testing"
87

@@ -36,31 +35,22 @@ func thereAreGodogs(available int) error {
3635
return nil
3736
}
3837

39-
func iEat(num int) error {
40-
err := assertExpectedAndActual(
41-
assert.GreaterOrEqual, Godogs, num,
42-
"You cannot eat %d godogs, there are %d available", num, Godogs,
43-
)
44-
if err != nil {
45-
return err
38+
func iEat(ctx context.Context, num int) error {
39+
if !assert.GreaterOrEqual(godog.T(ctx), Godogs, num, "You cannot eat %d godogs, there are %d available", num, Godogs) {
40+
return nil
4641
}
47-
4842
Godogs -= num
4943
return nil
5044
}
5145

52-
func thereShouldBeRemaining(remaining int) error {
53-
return assertExpectedAndActual(
54-
assert.Equal, Godogs, remaining,
55-
"Expected %d godogs to be remaining, but there is %d", remaining, Godogs,
56-
)
46+
func thereShouldBeRemaining(ctx context.Context, remaining int) error {
47+
assert.Equal(godog.T(ctx), Godogs, remaining, "Expected %d godogs to be remaining, but there is %d", remaining, Godogs)
48+
return nil
5749
}
5850

59-
func thereShouldBeNoneRemaining() error {
60-
return assertActual(
61-
assert.Empty, Godogs,
62-
"Expected none godogs to be remaining, but there is %d", Godogs,
63-
)
51+
func thereShouldBeNoneRemaining(ctx context.Context) error {
52+
assert.Empty(godog.T(ctx), Godogs, "Expected none godogs to be remaining, but there is %d", Godogs)
53+
return nil
6454
}
6555

6656
func InitializeScenario(ctx *godog.ScenarioContext) {
@@ -74,34 +64,3 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
7464
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
7565
ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)
7666
}
77-
78-
// assertExpectedAndActual is a helper function to allow the step function to call
79-
// assertion functions where you want to compare an expected and an actual value.
80-
func assertExpectedAndActual(a expectedAndActualAssertion, expected, actual interface{}, msgAndArgs ...interface{}) error {
81-
var t asserter
82-
a(&t, expected, actual, msgAndArgs...)
83-
return t.err
84-
}
85-
86-
type expectedAndActualAssertion func(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool
87-
88-
// assertActual is a helper function to allow the step function to call
89-
// assertion functions where you want to compare an actual value to a
90-
// predined state like nil, empty or true/false.
91-
func assertActual(a actualAssertion, actual interface{}, msgAndArgs ...interface{}) error {
92-
var t asserter
93-
a(&t, actual, msgAndArgs...)
94-
return t.err
95-
}
96-
97-
type actualAssertion func(t assert.TestingT, actual interface{}, msgAndArgs ...interface{}) bool
98-
99-
// asserter is used to be able to retrieve the error reported by the called assertion
100-
type asserter struct {
101-
err error
102-
}
103-
104-
// Errorf is used by the called assertion to report an error
105-
func (a *asserter) Errorf(format string, args ...interface{}) {
106-
a.err = fmt.Errorf(format, args...)
107-
}

features/formatter/events.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: event stream formatter
1313
"""
1414

1515
Scenario: should process simple scenario
16-
Given a feature path "features/load.feature:26"
16+
Given a feature path "features/load.feature:27"
1717
When I run feature suite with formatter "events"
1818
Then the following events should be fired:
1919
"""
@@ -34,7 +34,7 @@ Feature: event stream formatter
3434
"""
3535

3636
Scenario: should process outline scenario
37-
Given a feature path "features/load.feature:34"
37+
Given a feature path "features/load.feature:35"
3838
When I run feature suite with formatter "events"
3939
Then the following events should be fired:
4040
"""

features/formatter/pretty.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Feature: pretty formatter
350350
Scenario: Should scenarios identified with path:line and preserve the order.
351351
Given a feature path "features/load.feature:6"
352352
And a feature path "features/multistep.feature:6"
353-
And a feature path "features/load.feature:26"
353+
And a feature path "features/load.feature:27"
354354
And a feature path "features/multistep.feature:23"
355355
When I run feature suite with formatter "pretty"
356356
Then the rendered output will be as follows:
@@ -363,7 +363,7 @@ Feature: pretty formatter
363363
Scenario: load features within path # features/load.feature:6
364364
Given a feature path "features" # suite_context_test.go:0 -> *godogFeaturesScenario
365365
When I parse features # suite_context_test.go:0 -> *godogFeaturesScenario
366-
Then I should have 13 feature files: # suite_context_test.go:0 -> *godogFeaturesScenario
366+
Then I should have 14 feature files: # suite_context_test.go:0 -> *godogFeaturesScenario
367367
\"\"\"
368368
features/background.feature
369369
features/events.feature
@@ -378,6 +378,7 @@ Feature: pretty formatter
378378
features/run.feature
379379
features/snippets.feature
380380
features/tags.feature
381+
features/testingt.feature
381382
\"\"\"
382383
383384
Feature: run features with nested steps
@@ -407,7 +408,7 @@ Feature: pretty formatter
407408
As a test suite
408409
I need to be able to load features
409410
410-
Scenario: load a specific feature file # features/load.feature:26
411+
Scenario: load a specific feature file # features/load.feature:27
411412
Given a feature path "features/load.feature" # suite_context_test.go:0 -> *godogFeaturesScenario
412413
When I parse features # suite_context_test.go:0 -> *godogFeaturesScenario
413414
Then I should have 1 feature file: # suite_context_test.go:0 -> *godogFeaturesScenario

features/lang.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Savybė: užkrauti savybes
88
Scenarijus: savybių užkrovimas iš aplanko
99
Duota savybių aplankas "features"
1010
Kai aš išskaitau savybes
11-
Tada aš turėčiau turėti 13 savybių failus:
11+
Tada aš turėčiau turėti 14 savybių failus:
1212
"""
1313
features/background.feature
1414
features/events.feature
@@ -23,4 +23,5 @@ Savybė: užkrauti savybes
2323
features/run.feature
2424
features/snippets.feature
2525
features/tags.feature
26+
features/testingt.feature
2627
"""

features/load.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Feature: load features
66
Scenario: load features within path
77
Given a feature path "features"
88
When I parse features
9-
Then I should have 13 feature files:
9+
Then I should have 14 feature files:
1010
"""
1111
features/background.feature
1212
features/events.feature
@@ -21,6 +21,7 @@ Feature: load features
2121
features/run.feature
2222
features/snippets.feature
2323
features/tags.feature
24+
features/testingt.feature
2425
"""
2526

2627
Scenario: load a specific feature file

0 commit comments

Comments
 (0)