[TIP] Nose Importer

Alfredo Deza alfredodeza at gmail.com
Thu Apr 8 06:35:17 PDT 2010


On Thu, Apr 8, 2010 at 8:13 AM, Alfredo Deza <alfredodeza at gmail.com> wrote:

>
>
> On Wed, Apr 7, 2010 at 5:46 PM, Mark Roddy <markroddy at gmail.com> wrote:
>
>> On Wed, Apr 7, 2010 at 5:11 PM, Alfredo Deza <alfredodeza at gmail.com>
>> wrote:
>> > Hi,
>> > I am having some issues with the Nose importer.
>> > My project layout is set as follows:
>> > /project
>> > /project/lib
>> > /project/tests
>> > When I run 'nosetests -v' in /project everything runs OK (tests import
>> from
>> > within 'lib').
>> > But when I am within /project/tests 'nosetests -v' fails with an
>> ImportError
>> > exception saying it cannot import the
>> > files from lib on only *one* of the test files.
>> > However, if I take the same test file that is throwing exceptions and
>> run it
>> > against nose, it completes the tests successfully:
>> > 'nosetests -v test_permissions.py'
>> > It also works fine when calling it via python:
>> > 'python test_permissions.py'
>> > The one file that test_permissions.py is trying to import but is failing
>> is:
>> > 'permissions', and it does so by:
>> > sys.path.append('../')
>> > from lib import hg, permissions, upgrade
>> > I am not sure why it would be able to import 'hg' and 'database', but
>> fail
>> > to import 'permissions', and only when it runs all tests within the
>> tests
>> > directory.
>> > Here is a copy of one of the exceptions:
>> > ======================================================================
>> > ERROR: Failure: ImportError (cannot import name permissions)
>> > ----------------------------------------------------------------------
>> > Traceback (most recent call last):
>> >   File "/usr/local/lib/python2.6/dist-packages/nose/loader.py", line
>> 382, in
>> > loadTestsFromName
>> >     addr.filename, addr.module)
>> >   File "/usr/local/lib/python2.6/dist-packages/nose/importer.py", line
>> 39,
>> > in importFromPath
>> >     return self.importFromDir(dir_path, fqname)
>> >   File "/usr/local/lib/python2.6/dist-packages/nose/importer.py", line
>> 86,
>> > in importFromDir
>> >     mod = load_module(part_fqname, fh, filename, desc)
>> >   File "/home/alfredo/pacha/tests/test_permissions.py", line 10, in
>> <module>
>> >     from lib import hg, permissions, database
>> > ImportError: cannot import name permissions
>> >
>> >
>> > Maybe I need to import differently? But then again... *why* it works
>> when I
>> > call it directly? I basically want
>> > to be able to run from either /project or /project/tests with the same
>> > results.
>> >
>> >
>> > --
>> > Alfredo Deza
>> >
>> >
>> > _______________________________________________
>> > testing-in-python mailing list
>> > testing-in-python at lists.idyll.org
>> > http://lists.idyll.org/listinfo/testing-in-python
>> >
>> >
>>
>> One possibility for why you can import 'hg' from lib but not
>> 'permissions' is that 'hg' module that gets imported is not actually
>> the one you expect (when running from the 'tests' directory).  I ran
>> into this the other day when I had my path messed up, and I could
>> import one module from a package but not another.  Turns out the one I
>> could import was under site-packages as I had an older version
>> installed which did not have the module that the import failed on.
>>
>
> This is not the case, since 'hg' is not able to import at all from anywhere
> on the system.
> The actual Mercurial package is called 'mercurial'.
>
>
>> You can check that the 'hg' module which gets imported is actually the
>> module you expect by looking at it's __file__ attribute.
>>
>
> Yep, this verified that it is actually the one I have.
>
>>
>> -Mark
>>
>
>
> I guess my solution will be to move the tests directory out of the standard
> location (root level) inside 'lib'
>

Actually, before I give closure to this email... I just realized that *ALL*
my tests pass even though Nose is giving
ImportError exceptions.

This is clearly a specific Nose issue not related with either my project or
my tests (otherwise how could I pass tests that are not able to import the
module is testing?)

Weird.

Filing a bug report now



>
>
>
> --
> Alfredo Deza
>
>


-- 
Alfredo Deza
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100408/a80a8d4c/attachment-0001.htm>


More information about the testing-in-python mailing list