You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I figured out an easy way to do BDD-style testing with help from nullcontext:
fromcontextlibimportnullcontextasgivenfromcontextlibimportnullcontextaswhenfromcontextlibimportnullcontextasthenfromcontextlibimportnullcontextasand_withgiven('two values 2 and 3'):
x=2y=3withwhen('add the two values'):
result=x+ywiththen('the result is 5'):
assertresult==5withand_('the result is an integer'):
assertisinstance(result, int)
withwhen('get absolute difference between the two values'):
result=abs(x-y)
withthen('the result is 1'):
assertresult==1
Looks nice, but think it could be simpler.
In many cases we need unit testing to test not only the behavior we were looking for but what happens in case of anomalies such as bad input, keyboard interrupt, or I/O error in the middle?
Quick & simple lawyer-speak: What if ...?
I would also like to get rid of the ugly _ character from the and_ alias, if possible.
Came up with this:
fromcontextlibimportnullcontextasWHAT_IFfromcontextlibimportnullcontextasTHENfromcontextlibimportnullcontextasANDwithWHAT_IF('add two values 2 and 3'):
result=2+3withTHEN('the result is 5'):
assertresult==5withAND('the result is an integer'):
assertisinstance(result, int)
withWHAT_IF('get absolute the difference between 2 and 3'):
result=abs(2-3)
withTHEN('the result is 1'):
assertresult==1
I think using ALL-CAPS can be justified as this is using aliases, in a similar fashion to C-style #define or top-level constants, also helps designate important words (or key words with a space, not keywords in terms of parsing) for the reader. In other words, nice clarity here.
Alternatives could be to use "also" instead of "and", only capitalize the first letter ("And"), put underscore at the beginning (with _and(...)). I think using ALL-CAPS is the clearest & easiest.
And one more idea as a bonus encore:
fromcontextlibimportnullcontextWHAT_IF=nullcontextTHEN=nullcontextAND=nullcontextwithWHAT_IF('add two values 2 and 3'):
result=2+3withTHEN('the result is 5'):
assertresult==5withAND('the result is an integer'):
assertisinstance(result, int)
withWHAT_IF('get absolute the difference between 2 and 3'):
result=abs(2-3)
withTHEN('the result is 1'):
assertresult==1
This should probably be published as a library, maybe with some extra fancy features.
The text was updated successfully, but these errors were encountered:
I had discovered some Python BDD-style testing resources but nothing seemed to be maintained:
So I figured out an easy way to do BDD-style testing with help from
nullcontext
:Looks nice, but think it could be simpler.
In many cases we need unit testing to test not only the behavior we were looking for but what happens in case of anomalies such as bad input, keyboard interrupt, or I/O error in the middle?
Quick & simple lawyer-speak: What if ...?
I would also like to get rid of the ugly
_
character from theand_
alias, if possible.Came up with this:
I think using ALL-CAPS can be justified as this is using aliases, in a similar fashion to C-style
#define
or top-level constants, also helps designate important words (or key words with a space, not keywords in terms of parsing) for the reader. In other words, nice clarity here.Alternatives could be to use "also" instead of "and", only capitalize the first letter ("And"), put underscore at the beginning (
with _and(...)
). I think using ALL-CAPS is the clearest & easiest.And one more idea as a bonus encore:
This should probably be published as a library, maybe with some extra fancy features.
The text was updated successfully, but these errors were encountered: