0

Using python 3.6 (on a Mac Mojave; no virtualenv involved) I have the following setup: A folder containing the two folders

mymodule1
tests

The folder mymodule1 contains two files, an empty file __init__.py and a file myfunctions.py:

def func1(x):
    return 2*x

and the folder test contains one file test1.py:

from mymodule1 import myfunctions

def test1():
    assert myfunctions.func1(21) == 42

Since I want to run a py.test within a tox environment, I also have the following two files in the main folder:

First the tox.ini file (according to numerous examples):

[base]
name = mymodule1

[tox]
envlist =
    py36

[testenv]
deps =
    pytest

commands = py.test

which has the command py.test and not python -m pytest!! Then the setup.py file:

from setuptools import setup

setup(
    name="mymodule1",
    author="me",
    description="Short description",
    long_description="long description",
)

Running the command

py.test 

on the command line gives the following error: ========================================================================================== test session starts =========================================================================================== platform darwin -- Python 3.6.9, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 rootdir: /Users/me/mymodule1 collected 0 items / 1 error

================================================================================================= ERRORS =================================================================================================
____________________________________________________________________________________ ERROR collecting tests/test1.py _____________________________________________________________________________________
ImportError while importing test module '/Users/me/mymodule1/tests/test1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test1.py:1: in <module>
    from mymodule1 import myfunctions
E   ModuleNotFoundError: No module named 'mymodule1'
======================================================================================== short test summary info =========================================================================================
ERROR tests/test1.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================ 1 error in 0.17s ===========================================================================================

and running tox -e py36 gives the following output:

GLOB sdist-make: /Users/me/mymodule1/setup.py
py36 inst-nodeps: /Users/me/mymodule1/.tox/.tmp/package/1/mymodule1-0.0.0.zip
py36 installed: attrs==19.3.0,importlib-metadata==1.7.0,more-itertools==8.4.0,mymodule1 @ file:///Users/me/mymodule1/.tox/.tmp/package/1/mymodule1-0.0.0.zip,packaging==20.4,pluggy==0.13.1,py==1.9.0,pyparsing==2.4.7,pytest==5.4.3,six==1.15.0,wcwidth==0.2.5,zipp==3.1.0
py36 run-test-pre: PYTHONHASHSEED='3789759274'
py36 run-test: commands[0] | py.test
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
cachedir: .tox/py36/.pytest_cache
rootdir: /Users/me/mymodule1
collected 0 items / 1 error                                                                                                                                                                              

================================================================================================= ERRORS =================================================================================================
____________________________________________________________________________________ ERROR collecting tests/test_1.py ____________________________________________________________________________________
ImportError while importing test module '/Users/me/mymodule1/tests/test_1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_1.py:2: in <module>
    from mymodule1 import myfunctions
E   ModuleNotFoundError: No module named 'mymodule1'
======================================================================================== short test summary info =========================================================================================
ERROR tests/test_1.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================ 1 error in 0.15s ============================================================================================
ERROR: InvocationError for command /Users/me/mymodule1/.tox/py36/bin/py.test (exited with code 2)
________________________________________________________________________________________________ summary _________________________________________________________________________________________________
ERROR:   py36: commands failed

What am I missing? How is it possible that other people can use a tox.ini file invoking py.test directly?

Additional information:

$ which python
/Users/me/.pyenv/shims/python
$ which py.test
/Users/me/.pyenv/shims/py.test
Alex
  • 41,580
  • 88
  • 260
  • 469
  • 2
    Have you added empty `__init__.py` in your mymodule1 folder? – SEYED BABAK ASHRAFI Jul 22 '20 at 15:52
  • Yes I have. I forgot to add that to the question – Alex Jul 22 '20 at 15:54
  • Have you tried `python -m pytest -s` from the root directory? This adds the current directory to the Python path. – MrBean Bremen Jul 22 '20 at 16:01
  • Ah that seems to work now. But I want to run the test directly with `py.test ...` as explained/written in the documentation. – Alex Jul 22 '20 at 16:09
  • This invocation is also described in the [documentation](https://docs.pytest.org/en/stable/usage.html#calling-pytest-through-python-m-pytest). – MrBean Bremen Jul 22 '20 at 18:56
  • I have see the invocation using just `py.test ...` in many circumstances, like e.g. related to tox. Why is that not working for my case? I want to know! – Alex Jul 23 '20 at 05:19

0 Answers0