[py-dev] Really simple question about py.test
Ian Bicking
ianb at colorstudy.com
Thu Jul 28 22:03:44 CEST 2005
Sean Leach wrote:
> Why does this execute the same test twice (it only happens on a failed test):
>
> % cat test_twice.py
> def fail():
> print 'running failed'
> return False
>
> class TestTwice:
> def test_twice(self):
> assert fail()
>
> % py.test test_twice.py
>
> It will print 'running failed' twice (and of course run fail() twice).
> Is this a feature I am missing in the docs? I really don't want my
> failed tests to run twice.
py.test reinterprets the expressions in an assert statement (not the
entire test), to give you more detail on why it failed (e.g., if you do
"assert foo == bar" it'll reinterpret "foo" and "bar" to help you
understand why it failed).
As a general rule you shouldn't have side effects in your assertions, so
you wouldn't really notice it was interpreted twice. If you take the
side effect out like:
value = fail()
assert value
Then fail() won't be run twice.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the py-dev
mailing list