[TIP] pytest, setup.py and custom options

Sean Fisk sean at seanfisk.com
Wed Nov 27 01:40:02 PST 2013


Hi Daniel,

Sorry I misunderstood your question. You should be able to add
user-passable options to your test command as follows:

# setup.pyfrom setuptools import setupfrom setuptools.command.test
import test as TestCommand
class PyTest(TestCommand):
    user_options = [
        # long option, short option, description
        ('flakes', None, 'Use pyflakes'),
        ('coverage', 'C', 'Show coverage statistics'),
    ]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.flakes = False
        self.coverage = False

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = ['tests']
        if self.flakes:
            self.test_args.append('--flakes')
        if self.coverage:
            self.test_args += [
                '--cov', 'code_directory', '--cov-report', 'term-missing']
        self.test_suite = True

    def run_tests(self):
        import pytest
        raise SystemExit(pytest.main(self.test_args))

setup(
    tests_require=['pytest'],
    cmdclass={'test': PyTest},
)

This would add the ability to run python setup.py test --flakes or python
setup.py test --coverage. Make sure you change “code_directory” to the
actual directory containing your code.

I don’t know if it’s easy to pass-through options specified by the user on
the command-line, if that’s what you want. You could definitely munge
sys.argv, but at that point I would not use Setuptools’ test facility and
just create your own by intercepting control at the beginning of the
script. In general, I find distutils and Setuptools a little cumbersome in
this area, which is why I use Paver.

Hope this helps!


--
Sean Fisk


On Wed, Nov 27, 2013 at 1:35 AM, Daniel Farina <daniel at heroku.com> wrote:

> On Tue, Nov 26, 2013 at 9:12 PM, Sean Fisk <sean at seanfisk.com> wrote:
> > Hi Daniel,
> >
> > The excellent pytest documentation has a section on Setuptools
> integration.
> > In that example, you would want to write instead:
> >
> > # ...
> > self.test_args = ['--flakes']
> > # ...
>
> I don't want a static list of these options: I want the user to be
> able to modify them.  I want to have the defaults to be sane (I may
> add a few common options) but allow advanced and/or slow use cases
> (like coverage generation) to be added by the user.
>
> In retrospect, I think I can do this by munging sys.argv before
> setup() is called, but I am wondering if that is among the better
> ways.  It seems rather heavyhanded for setuptools to insist to be
> informed a-priori of every single valid option, so I imagine there may
> be another way.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20131127/1311171c/attachment.html>


More information about the testing-in-python mailing list