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

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:

  • 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 ?
  • 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. 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


    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.


    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
    
    链接地址: http://www.djcxy.com/p/31932.html

    上一篇: Julia的并行编程

    下一篇: 如何在`python setup.py test`中运行py.test和linters