Reputation: 2315
I'm unable to import my module for testing in the way that I'd like. I'm running all of this in a virtualenv on 2.7.2
I have a directory structure like
/api
/api
__init__.py
my_module.py
/tests
my_module_test.py
I have my PYTHONPATH set to /Path/api/. I CD into /Path/api and run the following
py.test tests/my_module_test.py
It does not work in the following case:
from api.my_module import my_function
It does work in the following case:
from my_module import my_function
Why am I not able to import my module as in case 1?
Upvotes: 4
Views: 4739
Reputation: 457
I created this as an answer to your question and my own confusion. I hope it helps. Pay attention to PYTHONPATH
in both the py.test
command line and in the tox.ini
.
The example project is here, and also below:
mymodule.py
:
import boto3
def stuff():
print "Yep!"
tests/text_syntax_errors.py
:
import boto3
import mymodule
# Define a basic test that actually doesn't do much.
# I just wanted more than zero tests
def test_one_equals_one():
assert 1 == 1
tox.ini
:
[tox]
skipsdist = True
envlist = py27
[flake8]
max-line-length = 119
[testenv]
deps= -r{toxinidir}/requirements.txt
commands=py.test
setenv =
PYTHONPATH = {toxinidir}
requirements.txt
:
boto3
pytest
From my README.md
:
How-to run these examples
My initial motivation for testing my code was that I had misspelled an imported module in a script that I was writing for work.
If you edit
mymodule.py
and remove theb
from "boto3
" you will see the commands below fail. And this is a good thing. Likewise if you would like to see an actual test fail, simple edittests/test_syntax_errors.py
and change1 == 1
to1 == 0
.
py.test
mbp0 pytest_test[master+*] $ PYTHONPATH=. py.test
========================== test session starts ==========================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jmacdonald/w/pytest_test, inifile:
collected 1 items
tests/test_syntax_errors.py .
======================= 1 passed in 0.11 seconds ========================
mbp0 pytest_test[master+*] $
tox
mbp0 pytest_test[master+*] $ tox
py27 installed: boto3==1.3.1,botocore==1.4.37,docutils==0.12,futures==3.0.5,jmespath==0.9.0,py==1.4.31,pytest==2.9.2,python-dateutil==2.5.3,six==1.10.0
py27 runtests: PYTHONHASHSEED='713732044'
py27 runtests: commands[0] | py.test
========================== test session starts ==========================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jmacdonald/w/pytest_test, inifile:
collected 1 items
tests/test_syntax_errors.py .
======================= 1 passed in 0.11 seconds ========================
________________________________ summary ________________________________
py27: commands succeeded
congratulations :)
mbp0 pytest_test[master+*] $
Upvotes: 2
Reputation: 971
From the py.text document,you should install first.:
pip install -e .
Upvotes: 2
Reputation: 166
I use PYTHONPATH as
PYTHONPATH=`pwd` py.test tests/my_module_test.py
Upvotes: 15