My very simple example project contains:
addtest/
setup.py
addtest/
__init__.py
__main__.py
app.py
My app.py
is just:
def main():
raise SystemExit("Command line entry point called.")
My __main__.py
is just:
from addtest.app import main
main()
My setup.py
contains:
from setuptools import setup, find_packages
setup(
name='AddTest',
version='1.0',
packages=find_packages(),
entry_points={
'console_scripts': ['addtest = addtest.app:main']
},
)
I would expect that running python setup.py test
would do nothing, since no unit tests are written. However, running it in a clean virtualenv (Python 3.6.6 on Ubuntu 18.04.1) gives me:
$ python setup.py test
running test
running egg_info
writing AddTest.egg-info/PKG-INFO
writing dependency_links to AddTest.egg-info/dependency_links.txt
writing entry points to AddTest.egg-info/entry_points.txt
writing top-level names to AddTest.egg-info/top_level.txt
reading manifest file 'AddTest.egg-info/SOURCES.txt'
writing manifest file 'AddTest.egg-info/SOURCES.txt'
running build_ext
Command line entry point called.
Note the Command line entry point called.
which means it's invoking the console script it generates from my __main__.py
(or maybe just calling python -m addtest
).
Why is setup.py
calling the console script when I want it to run tests? Further inspection of the script's execution shows that sys.argv
is ['setup.py','test']
- why?
The test scanner in setuptools will look for tests in any *.py
file found in your sub-directories, except for __init__.py
. Yes, this includes __main__.py
, it will call __import__()
on it, causing its main suite to be executed.
If you want to be able to run python -m addtest
and have that run your __main__.py
code, you may want to add the standard 'run only if this is really main' protection:
if __name__ == "__main__":
...
(then, if you do python -m
the code will run, but it won't run if the file is loaded by setup.py test
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With