Search code examples
pythonpython-3.xtddscaffolding

When testing, 'python setup.py test' is the only way to test in Python Package?


There is a problem that I have in Python package structure.

Here is what I did in Mac OSX and Python 3.4:

1. mkdir MyProject
2. cd MyProject
3. virtualenv --python=python3.4 venv
4. . venv/bin/activate
5. pip install pytest pyscaffold
6. putup MyProject

Then, the directory structure would look like this :

MyProject/
   MyProject/
      docs/
      myproject/
      tests/
      requirements.txt
      setup.py
      .
      .
   venv/

And I added apple.py in myproject and test_apple.py in tests.

Then, structure would be like this :

MyProject/
   MyProject/
      docs/
      myproject/
         apple.py
      tests/
         test_apple.py
      requirements.txt
      setup.py
      .
      .
   venv/

apple.py

import sys
for path in sys.path:
    print(path)

print("start apple")

def get_apple():
    print("apple!!")
    return 5

When I run apple.py by typing python apple.py in myproject directory, it prints :

/Users/Chois/Desktop/MyProject/MyProject/myproject
/Users/Chois/Desktop/MyProject/venv/lib/python34.zip
/Users/Chois/Desktop/MyProject/venv/lib/python3.4
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/plat-darwin
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/site-packages
start apple

As you can see above, sys.path include /Users/Chois/Desktop/MyProject/MyProject/myproject

So, I made a test_apple.py like this:

test_apple.py

import sys
for path in sys.path:
    print(path)

from myproject.apple import get_apple

def test_apple():
    assert 5 == get_apple()

When I ran this code by typing py.test -s test_apple.py,
(-s option is for showing the result of print statement in the code)

/Users/Chois/Desktop/MyProject/MyProject/tests
/Users/Chois/Desktop/MyProject/venv/lib/python34.zip
/Users/Chois/Desktop/MyProject/venv/lib/python3.4
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/plat-darwin
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin
/Users/Chois/Desktop/MyProject/venv/lib/python3.4/site-packages
Traceback (most recent call last):
  File "test_apple.py", line 5, in <module>
    from myproject.apple import get_apple
ImportError: No module named 'myproject'

However, when I run test by typing python setup.py test, it runs without errors! !!

What I want to know here is how I can run a specific test module using py.test -s test_xxxx.py instead of ALL test modules by python setup.py test.

Need your helps, thanks.


Solution

  • Please read Good Integration Practices.

    make sure that “mypkg” is importable, for example by typing once:

    pip install -e . # install package using setup.py in editable mode