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

Maciek Fijalkowski fijal at genesilico.pl
Thu Aug 30 10:37:25 CEST 2007


Immanuel Scholz wrote:
> Hi list,
>
> I am rather new to py library and just started using py.test.
>
> You may already have noticed, that the "assert" in magic-mode will
> evaluate its arguments (e.g. the parts of an equality check) more than
> once. This will not work, if the arguments have side effects, as in:
>
> 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?
>
>
> Ciao, Imi.
>
>   

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 :)

Cheers,
fijal


:.




More information about the Pytest-dev mailing list