Module error Error starting py.test with modules in the path

I cannot import my testing module as I would like. I run it all in 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 PYTHONPATH installed in / Path / api /. I CD in / Path / api and run the following

  py.test tests/my_module_test.py 

It does not work in the following case:

  • When I have the following at the top of my_module_test.py from api.my_module import my_function

It works in the following case:

  • When I have the following at the top of my_module_test.py from my_module import my_function

Why can't I import my module, as in case 1?

+4
source share
3 answers

I use PYTHONPATH as

 PYTHONPATH=`pwd` py.test tests/my_module_test.py 
+14
source

From the py.text document you should install first .:

 pip install -e . 
+2
source

I created this as an answer to your question and my own confusion. I hope this helps. Pay attention to PYTHONPATH on the py.test command line and in tox.ini .

An example project is here , as well as 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 made a mistake with the imported module in the script that I wrote to work.

If you edit mymodule.py and delete b with " boto3 ", you will see that the commands below do not work. And this is good. Similarly, if you want to see the actual test fail, just edit tests/test_syntax_errors.py and change 1 == 1 to 1 == 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+*] $ 

toxicodendron

 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+*] $ 
+2
source

Source: https://habr.com/ru/post/1484681/


All Articles