Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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.

like image 204
Amir Rachum Avatar asked Feb 06 '16 09:02

Amir Rachum


People also ask

How do I run a test in setup py?

You can just specify test_suite="tests", and it will pick up everything recursively. Note that each nested directory will need to have an __init__.py file in it. You need to do from setuptools import setup instead of from distutils. core import setup for this option to exist.

How do I run a specific test in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

What is Python Linter?

Linters are programs that advise about code quality by displaying warnings and errors. They can detect your Python code mistakes, notice invalid code patterns and find elements that do not follow your conventions. Python linters have a number of advantages, such as: Preventing bugs in a project.


1 Answers

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

like image 62
Thor77 Avatar answered Nov 15 '22 08:11

Thor77