bdd - Python Behave sharing data between features -
i'm starting python behave want make api testing thing.
stumbled upon may not valid question is: can share data between features? store in db or files maybe there 'builtin'?
or invalid , shouldn't share data that, or maybe it's possible inside feature?
in practice looks like:
i have make request endpoint, in response number required make request that's needed tested.
yes, can, , trivial. in same directory have feature files can create file named environment.py
. in it, can put:
def before_all(context): context.db = whatever
the before_all
hook run before tests , whatever set there available features. use this, instance, create new selenium instance used tests in test suite. context.db
above database connection. kind of sharing fine.
what share should read-only or resettable known state between tests. not practice share writable resources between tests. makes hard figure out went wrong when test fails , makes tests dependent on each other. if have failure on test c depends on , b cannot ask behave run test c. have ask run a, b , c.
if decide go against best practices , anyway, should aware new values set on context
scoped feature , scenario. if before_all
hook sets context.foo = 1
, feature sets context.foo = 2
. when feature b runs after feature a, see value 1
context.foo
because behave have removed changes made feature (the changes made feature "scoped" feature a.) now, have remember how python stores values. if hook sets context.foo = []
, feature context.foo.append(1)
, feature b see context.foo
has value [1]
because context.foo
contains reference array , calling append
changes array itself. possible work around scoping. still not advisable, though.
last checked, features run in alphabetical order. can force order specifying features on command line: behave b.feature a.feature
Comments
Post a Comment