This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: unittest test discovery can fail when package under test is also installed globally
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 2.7
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: benjamin.peterson, hpk, michael.foord
Priority: normal Keywords:

Created on 2010-04-27 12:23 by michael.foord, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg104313 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-04-27 12:23
When test discovery is invoked on a package on the filesystem which is also installed in site-packages then importing the tests will look in the installed version. This causes discovery to run the wrong tests or fail.

We can detect this (compare the __file__ against the expected location) and also insert the top level path as the first location in sys.path instead of appending it to the end.

If we detect a failed import then we should error out with an appropriate message.

(Nose gets round this by patching __import__ to import from a specific location which seems potentially fragile.)
msg104659 - (view) Author: holger krekel (hpk) Date: 2010-04-30 19:32
FWIW checking if an imported module really comes from a certain location and erroring out is also how py.test does it.
msg104660 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-04-30 19:33
Sounds like the right approach then. :-)
msg105220 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-05-07 18:16
Committed revision 80932. Still needs documenting, so leaving open for the moment.
msg105226 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-05-07 20:46
Reverted this in r80939. See
msg105246 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-05-07 23:46
Committed again revision 80946 after getting the tests to pass on Windows.
Date User Action Args
2022-04-11 14:57:00adminsetgithub: 52793
2010-05-08 18:08:10michael.foordsetstatus: open -> closed
resolution: accepted
stage: needs patch -> resolved
2010-05-07 23:46:14michael.foordsetmessages: + msg105246
2010-05-07 20:46:23benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg105226
2010-05-07 18:16:47michael.foordsetmessages: + msg105220
2010-04-30 19:33:52michael.foordsetmessages: + msg104660
2010-04-30 19:32:15hpksetnosy: + hpk
messages: + msg104659
2010-04-27 12:23:17michael.foordcreate