[py-dev] py.test: broken assert evaluate twice

Immanuel Scholz immanuel.scholz at tu-dresden.de
Thu Aug 30 10:58:56 CEST 2007

>> i = 1
>> def foo():
>>   global i
>>   i += 1
>>   return i
>> def test_foo():
>>   assert foo() == 3
>> Is there a common solution to this, other than the annoying
>>   foo_result = foo()
>>   assert foo_result == 3
>> thingie? Maybe py.test could preemptive cache all calls to assert
>> beforehand? Is this possible?
> It's sophisticated, as long as it's lazy - ie when assert works (is not 
> broken) there is no magic at all. There are few solutions. One is the 
> above solution, and we do this all the time in pypy. Other one is to use 
> --nomagic, but you don't get all nice info out of that :)

--nomagic isn't a cute solution either, as you get no information at all
about the problem, and when people unaware of this run the test code
without the option, they still get false information.

I was thinking of a more comfortable solution. Is it possible in python
to capture the assert call before it is made and cache the argument and
then call to the original "assert"? This way, you don't have to remember
to cache the call everytime yourself.

The stuff is most annoying when testing functions that are some kind of
generator and spits out another value on each call.

Ciao, Imi.

DF0E A04F 7063 69C5 A76B  7557 0A77 608F 9172 3535

More information about the Pytest-dev mailing list