kostyll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

patch/patch.object as a decorator fails to report the function name with py.test #203

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Reported by Tim Couper.

here is the example of using patch as a decorator (in this case with 
patch.object) where the list of functions returned in py.test replaces the name 
of the function with the name of the module. So I have 3 tests 2 with it as a 
decorator, one of the tests passing and one failing, and finally, 1 using 
patch.object as a context mgr.

If you extract the two attached files into the same directory, and their run:

py.test –v test_patch.py

you should get an output like:

collected 3 items

tests/unit/test_patch.py <- 
/users/is/tcouper/workspace/risk.data/tests/unit/test_patch.py:11: 
tests.unit.test_patch PASSED
tests/unit/test_patch.py <- 
/users/is/tcouper/workspace/risk.data/tests/unit/test_patch.py:17: 
tests.unit.test_patch FAILED
tests/unit/test_patch.py <- 
/users/is/tcouper/workspace/risk.data/tests/unit/test_patch.py:22: 
test_patch_context_mgr PASSED

You’ll see that the 1st 2 functions have the place where their function name 
should be now rather unhelpfully  holds the name of the containing file, and 
one is only guided by the line number in the file to identify the offending 
test(s).

FWIW Nose appears to work correctly ..

I've got to the bottom of why using patch as a decorator confuses pytest about 
the name of tests. The lines that do the damage are lines 1237-1242. They set 
patched.compat_co_firstlineno on the returned patch object..

If you get the mock source and remove those lines everything works again or I 
have a work around (attached) which you can import instead of mock. But all 
this only dodges the question about why patch does this in the first place. 
From a few clues online it might be to support nose. I wonder if there is a fix 
that can be made to mock that would mean that it could work in mock and pytest.

Original issue reported on code.google.com by fuzzyman on 16 Apr 2013 at 10:02

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by fuzzyman on 16 Apr 2013 at 10:33

GoogleCodeExporter commented 9 years ago
Could only look briefly, but i wonder if pytest could help as well.  I've 
prepared a pytest-dev release installable via "pip install -i 
http://pypi.testrun.org -U pytest" and it should give you 2.3.5dev9 and a 
better display without any change in mock. 
Tim, please let me know if that release works nicely for you.

Original comment by holger.k...@gmail.com on 16 Apr 2013 at 10:43

GoogleCodeExporter commented 9 years ago
I've got mock==1.0.1 and pytest==2.4.2 and I still see this problem. I've 
attached a single file that reproduces the error, output is below:

$ py.test -v test_foo.py
============================= test session starts =============================
platform win32 -- Python 2.7.3 -- pytest-2.4.2 -- 
C:\MinGW\msys\1.0\home\Andrew\.virtualenvs\mockerror\Scripts\p

collected 7 items

test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:5: test_foo 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:9: test_foo 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:14: test_foo 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:19: test_foo 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:24: test_foo 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:28: test_six 
PASSED
test_foo.py <- 
c:\Users\Andrew\projects\python_workspace\__scratch\test_foo.py:32: test_seven 
PASSED

========================== 7 passed in 0.06 seconds ===========================

Original comment by burro...@gmail.com on 3 Dec 2013 at 12:10

Attachments:

GoogleCodeExporter commented 9 years ago
it's fixed, you can install a dev candidate via "pip install -i 
https://devpi.net/hpk/dev/ -U pytest" and soon there is going to be a 2.4.3 
release. 

Original comment by holger.k...@gmail.com on 3 Dec 2013 at 10:23

GoogleCodeExporter commented 9 years ago
Thanks for fixing this, looking forward to 2.4.3 - I know a bunch of people 
who'll be glad to see this fix

Original comment by burro...@gmail.com on 5 Dec 2013 at 7:44