Using assert or require from stretchr/testify in godog #535
Replies: 5 comments 3 replies
-
One addition that I would like to see is to run every scenario as a separate test, leveraging |
Beta Was this translation helpful? Give feedback.
-
@aslakhellesoy @mattwynne hi, I understand you may have been super busy, but may I get some feedback on the post please? I was wondering if you would have any objections to replace the existing assert-godogs example with something that more closely resembles how the Proposal:
func iEat(t *testing.T, arg1 int) {
// yay, now we can easily use assert or require
assert.Fail(t, "Not implemented")
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I eat (\d+)$`, iEat)
} And with the context: func iEat(t *testing.T, ctx context.Context, arg1 int) {
// yay, now we can easily use assert or require
assert.Fail(t, "Not implemented")
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I eat (\d+)$`, iEat)
} … but as per the post above, this alone won't be enough, so also:
func RunTests(t *testing.T) {
suite := &godog.TestSuite{
Options: &godog.Options{
TestingT: &godog.AssertT{t: t}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Having access to
|
Beta Was this translation helpful? Give feedback.
-
With a great contribution from @mrsheepuk (#571), this discussion can be closed now, |
Beta Was this translation helpful? Give feedback.
-
I guess this is not the cleanest. Works for me though
2 steps (1 passed, 1 failed)
|
Beta Was this translation helpful? Give feedback.
-
Hi all!
This has been bogging me for a while; why we cannot simply use
assert
andrequire
funcs in the BDD steps funcs?TL;DR
It can be done with a very little of boilerplate and it's not difficult at all.
I wish this was built-in into godog and available via some kind of a flag, or property in the suite setup, so that this boilerplate is not needed.
I've put together some analysis with some proposals.
More below…
Why?
I've looked here and here for some ideas.
Happy to be told this has been discussed before, rejected, disregarded, or anything else, but I haven't really found much useful about this topic anywhere. It's kinda big deal to me, possibly a blocker from using
godog
professionally on very large projects.Seems like there are a couple of obvious reasons for this issue:
How to get access to the
*testing.T
instance?This seems to be an easy one. We can use the test struct pattern as described e.g. in the API example:
… and add the
*testing.T
field to it. That way it would be easily accessible in all steps:Note the difference to the original code - it's now much cleaner, less code.
🤔 IMHO, this is the pattern I would like to see being supported Out-Of-the-Box or even suggested by default. When developers are looking at how to use
godog
for the first time, they may run into three ways of accessing common variables inside their step definitions:But there's a gotcha. 🚧
Assertion funcs like
assert.Equal
return a bool and not an error.The
theResponseCodeShouldBe
does not return anerror
anymore and therefore the error from the assertion is somewhat swallowed and this step would actually PASS even in cases where it should FAIL. Thegodog
test statistics would show happily something like 3 scenarios (3 passed, 0 failed) even if a step was failing. It may just log an error in the output. Not good.Workaround: We could put the
error
return type back at the expense of more code:Yuck ;-) This is obviously very naïve, with a duplicated code and should be refactored - producing even more code. Not good…
Another example - loosely following the example from the godog homepage:
With the pattern supporting
assert
(andrequire
), we would save so much code and make it even more readable!From 12 lines of code down to 5 (for the step func, including an empty line). I call that a success!
But it doesn't work…
Yeah. I've tried to sum up and visually show the issues on this branch.
Patterns used:
See
require
) even before it gets to the assert - this is failing but the output is still "green" and not "red" ❓Is there a fix?
You be the judge 😉 With the ideas shared in the links at the top and the above issues, I've tried to come up with a proof-of-concept to fix them.
Patterns used:
bt *BddTesting
which handles failures (fromrequire
) and errors (fromassert
).What is
BddTesting
?t *testing.T
and potential errorst
in assertion funcs, e.g.assert.Equal(t, …)
would becomeassert.Equal(bt, …)
assert
are tracked as errors, so that we get correct statistics and color-codingrequire
are tracked as failures, but unlike the originalt *testing.T
module it's usingt.Fail()
and nott.FailNow()
, so that we always get the overall test suite stats.See
require
) even before it gets to the assert ✅Is it done? Can I use it now?
No, not quite.
There's a little issue with some duplicated output. It's currently too chatty. There's work to be done. If you know how to fix it, please feel free to raise a PR :-)
This is only a PoC and intended as a starting point for a discussion on how to make better use of
assert
andrequire
effectively ingodog
.So why not use assert-godogs?
if error != nil {return error}
conditions and therefore more code.assert.Equal(t, expected, actual, msg)
pattern, it would take a lot of effort to rewrite them to the format suggested inassert-godogs
- in my case at least, it won't be done, too much work, and it's yet another reason why it's so hard to adopt BDD in a large organisation.Thanks.
Beta Was this translation helpful? Give feedback.
All reactions