From 2d8bcbdf557344dd8afd97e00d635e43014a13e9 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 28 Oct 2010 09:29:56 +0200 Subject: [PATCH] document "setup.py test" to use genscript'ed version. --HG-- branch : trunk --- doc/goodpractises.txt | 50 +++++++++++++++++++++++++++++--- pytest/plugin/genscript.py | 2 +- testing/plugin/test_genscript.py | 5 ++-- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/doc/goodpractises.txt b/doc/goodpractises.txt index 47b6b43c5..14ea561f2 100644 --- a/doc/goodpractises.txt +++ b/doc/goodpractises.txt @@ -1,4 +1,5 @@ +.. highlightlang:: python .. _`good practises`: Good Practises @@ -28,8 +29,7 @@ py.test supports common test layouts. XXX - - +.. _`genscript method`: Generating a py.test standalone Script ------------------------------------------- @@ -38,12 +38,12 @@ If you are a maintainer or application developer and want users to run tests you can use a facility to generate a standalone "py.test" script that you can tell users to run:: - py.test --genscript=mytest + py.test --genscript=runtests.py will generate a ``mytest`` script that is, in fact, a ``py.test`` under disguise. You can tell people to download and then e.g. run it like this:: - python mytest --pastebin=all + python runtests.py --pastebin=all and ask them to send you the resulting URL. The resulting script has all core features and runs unchanged under Python2 and Python3 interpreters. @@ -51,4 +51,46 @@ all core features and runs unchanged under Python2 and Python3 interpreters. .. _`Distribute for installation`: http://pypi.python.org/pypi/distribute#installation-instructions .. _`distribute installation`: http://pypi.python.org/pypi/distribute + +Integrating with distutils / ``python setup.py test`` +-------------------------------------------------------- + +You can easily integrate test runs into your distutils or +setuptools based project. Use the `genscript method`_ +to generate a standalone py.test script:: + + py.test --genscript=runtests.py + +and make this script part of your distribution and then add +this to your ``setup.py`` file:: + + from distutils.core import setup, Command + # you can also import from setuptools + + class PyTest(Command): + user_options = [] + def initialize_options(self): + pass + def finalize_options(self): + pass + def run(self): + import sys,subprocess + errno = subprocess.call([sys.executable, 'runtest.py']) + raise SystemExit(errno) + setup( + #..., + cmdclass = {'test': PyTest}, + #..., + ) + +If you now type:: + + python setup.py test + +this will execute your tests using ``runtest.py``. As this is a +standalone version of ``py.test`` no prior installation whatsoever is +required for calling the test command. You can also pass additional +arguments to the subprocess-calls like your test directory or other +options. + .. include:: links.inc diff --git a/pytest/plugin/genscript.py b/pytest/plugin/genscript.py index 2b379294e..a9188add9 100755 --- a/pytest/plugin/genscript.py +++ b/pytest/plugin/genscript.py @@ -58,7 +58,7 @@ def pytest_cmdline_main(config): genscript = config.getvalue("genscript") if genscript: script = generate_script( - 'import py; py.test.cmdline.main()', + 'import py; raise SystemExit(py.test.cmdline.main())', ['py', 'pytest'], ) diff --git a/testing/plugin/test_genscript.py b/testing/plugin/test_genscript.py index bb4e555c0..c71ac1526 100644 --- a/testing/plugin/test_genscript.py +++ b/testing/plugin/test_genscript.py @@ -19,13 +19,14 @@ class Standalone: return testdir._run(anypython, self.script, *args) def test_gen(testdir, anypython, standalone): - result = standalone.run(anypython, testdir, '-h') - assert result.ret == 0 result = standalone.run(anypython, testdir, '--version') assert result.ret == 0 result.stderr.fnmatch_lines([ "*imported from*mypytest" ]) + p = testdir.makepyfile("def test_func(): assert 0") + result = standalone.run(anypython, testdir, p) + assert result.ret != 0 @py.test.mark.xfail(reason="fix-dist", run=False) def test_rundist(testdir, pytestconfig, standalone):