How to run 'tox' command for 'py.test' for python module?

Viewed 1314

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 test_1.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 = pytest

which has the command pytest 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

pytest 

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/adietz/mymodule
plugins: cov-2.10.0
collected 0 items / 1 error                                                                                                                                                                         

============================================================================================== ERRORS ===============================================================================================
_________________________________________________________________________________ ERROR collecting tests/test_1.py __________________________________________________________________________________
ImportError while importing test module '/Users/adietz/mymodule/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.13s ==========================================================================================

and running tox -e py36 gives the following output:

GLOB sdist-make: /Users/adietz/mymodule/setup.py
py36 create: /Users/adietz/mymodule/.tox/py36
py36 installdeps: pytest
py36 inst: /Users/adietz/mymodule/.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/adietz/mymodule/.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='1211015963'
py36 run-test: commands[0] | pytest
======================================================================================== 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/adietz/mymodule
collected 0 items / 1 error                                                                                                                                                                         

============================================================================================== ERRORS ===============================================================================================
_________________________________________________________________________________ ERROR collecting tests/test_1.py __________________________________________________________________________________
ImportError while importing test module '/Users/adietz/mymodule/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.12s ==========================================================================================
ERROR: InvocationError for command /Users/adietz/mymodule/.tox/py36/bin/pytest (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 pytest/py.test directly?

To be very clear: I am not looking for a workaround. I am looking for an explanation, so that I can use a tox.ini file invoking pytest/py.test directly. I want to know why it is not working for me, when it is working for thousand of other packages. What are they doing differenty?

Additional information:

$ which python
/Users/adietz/.pyenv/shims/python
$ which pytest
/Users/adietz/.pyenv/shims/pytest
1 Answers

The solution to all the problem might be in the setup.py! Just change it to the following code:

from setuptools import setup, find_packages

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

which then seems to 'find all the packages' and it works.

Maybe you might find that tiny, but extremely important aspect when you read all of the documentation for

  • python
  • pytest
  • tox
  • setup.py
Related