Skip to content

Modules in namespace packages are not imported correctly #856

@craig-reahl

Description

@craig-reahl

I have found that Nose 1.3.4(and 1.3.7) does not import tests,for installed packages, that are in namespace packages correctly.

Versions:
nose==1.3.4
python 3.4
Ubuntu 14.04.1 LTS

Consider this directory structure:

├── my_tests
│   ├── __init__.py
│   └── my_package
│        ├── __init__.py
│        └── some_test.py
│
├── my_tests_with_namespace
│   ├── __init__.py
│   └── my_package_in_ns
│        ├── __init__.py
│        └── some_test_ns.py
└── setup.py

The my_tests and my_tests_with_namespace directories(which are both python packages since they have __init__.py)
both contain tests inside them. The __init__.py of my_tests_with_namespace however declares a namespace.

-----TEST SNIPPET(some_test.py) BEGIN---------

class MyTestClass:
    pass

def other_test_no_ns():
        import my_tests.my_package.some_test
        assert MyTestClass is my_tests.my_package.some_test.MyTestClass, \
               '%s is not %s' % (MyTestClass, my_tests.my_package.some_test.MyTestClass)

-----TEST SNIPPET(some_test_ns.py) END---------

-----TEST SNIPPET(some_test_ns.py) BEGIN---------

class MyTestClassNS:
    pass

def other_test():
        import my_tests_with_namespace.my_package_in_ns.some_test_ns
        assert MyTestClassNS is my_tests_with_namespace.my_package_in_ns.some_test_ns.MyTestClassNS, \
               '%s is not %s' % (MyTestClassNS, my_tests_with_namespace.my_package_in_ns.some_test_ns.MyTestClassNS)

-----TEST SNIPPET(some_test_ns.py) END---------

Running these commands from the source directory works fine, as the tests are executed and pass:

:~/develop$ nosetests my_tests
:~/develop$ nosetests my_tests_with_namespace

Now I create an sdist(?egg?)and create and activate into a new virtualenv elsewhere.
Next, I install my project (myproject) using pip.

-----SAMPLE of commands BEGIN---------

python setup.py sdist --dist-dir=/tmp/
cd /tmp
virtualenv cs_virtualenv --python=/usr/bin/python3.4
source cs_virtualenv/bin/activate
pip install --no-index --find-links=/tmp/ myproject
pip install nose
nosetests my_tests
nosetests my_tests_with_namespace

-----SAMPLE of commands END---------

When running the same nosetest commands, but this time against the installed package, the test for the other_test() fails:

======================================================================
FAIL: some_test_ns.other_test
----------------------------------------------------------------------
AssertionError: <class 'some_test_ns.MyTestClassNS> is not <class 'my_tests_with_namespace.my_package_in_ns.some_test_ns.MyTestClassNS'>

The issue I am raising has to do with the fact that for installed tests, the same module gets imported twice with different names, which my test illustrate.

(Usecase: wanting to run test with devpi and tox for installed packages)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions