How to run py.test and linters in `python setup.py test`

纵饮孤独 提交于 2019-12-08 17:41:39

问题


I have a project with a setup.py file. I use pytest as the testing framework and I also run various linters on my code (pep8, pylint, pydocstyle, pyflakes, etc). I use tox to run these in several Python versions, as well as build documentation using Sphinx.

I would like to run my test suites as well as the linters on my source code with the python setup.py test task. If I acheive this, I will then just use python setup.py test as the command for running tests in my tox.ini file.

So my questions are:

  1. Is it reasonable / good practice to do these actions with python setup.py test? Or should I just use some other tool for that, like writing those command directly in tox?
  2. How do I get setup.py to do these actions in the test task?

I know py.test has integration instructions for setup.py test (here: http://pytest.org/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner), but I'm looking for a more "arbitrary CLI commands" route, since I want to run several tools.


回答1:


1. I personally prefer tox for these tasks, because

  • it will also check if your deps install properly (on all py-versions)
  • it can test your code with multiple python-versions (virtualenv)

And with your setup (since you're already using tox) i don't really see the benefits of writing python setup.py test instead of the exact test-commands into your tox.ini, because it will just add some more complexitiy (new users/contributors have to search two files (tox.ini and setup.py) for tests/tools running instead of one (tox.ini).


2.

To use this command, your project’s tests must be wrapped in a unittest test suite by either a function, a TestCase class or method, or a module or package containing TestCase classes.

setuptools#test




回答2:


How to make setup.py test do tox stuff

import sys
import os

if sys.argv[-1] == 'test':
    try:
        import tox
    except ImportError:
        raise ImportError('You should install `tox` before run `test`')
    sys.exit(os.system('tox'))

Looks not good, but you have a point. Continiue to use tox.ini for configure your tests environment and runners (as flake8, coverage, etc.) Because tox is good at stuff like this.




回答3:


You can add more custom commands to your setup.py file like this:

class FooCommand(distutils.cmd.Command):
    ...

setup(
    cmdclass={
        'foo': FooCommand,
    }
)

however, note that there are already a set of commands you could use, and some runners you can include.

setup(
    setup_requires=['pytest-runner'] # gives you a new pytest command
)

Find out by running python setup.py --help-commands

Extra commands:
  ...
  pytest            run unit tests after in-place build


来源:https://stackoverflow.com/questions/35239628/how-to-run-py-test-and-linters-in-python-setup-py-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!