[Checkins] SVN: zc.recipe.testrunner/trunk/ - Merged ctheune-environ branch

Christian Theune ct at gocept.com
Thu Apr 26 00:02:47 EDT 2007


Log message for revision 74776:
   - Merged ctheune-environ branch
  

Changed:
  _U  zc.recipe.testrunner/trunk/
  U   zc.recipe.testrunner/trunk/CHANGES.txt
  U   zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/README.txt
  U   zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/__init__.py

-=-

Property changes on: zc.recipe.testrunner/trunk
___________________________________________________________________
Name: svn:ignore
   - build
dist
develop-eggs
bin
parts
.installed.cfg

   + build
eggs
dist
develop-eggs
bin
parts
.installed.cfg


Modified: zc.recipe.testrunner/trunk/CHANGES.txt
===================================================================
--- zc.recipe.testrunner/trunk/CHANGES.txt	2007-04-26 03:59:06 UTC (rev 74775)
+++ zc.recipe.testrunner/trunk/CHANGES.txt	2007-04-26 04:02:47 UTC (rev 74776)
@@ -2,6 +2,16 @@
 Change History
 **************
 
+1.0.0b7 (2007-04-26)
+====================
+
+Feature Changes
+---------------
+
+- Added optional option `environment` that allows defining a section in your
+  buildout.cfg to specify environment variables that should be set before
+  running the tests.
+
 1.0.0b6 (2007-02-25)
 ====================
 

Modified: zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/README.txt
===================================================================
--- zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/README.txt	2007-04-26 03:59:06 UTC (rev 74775)
+++ zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/README.txt	2007-04-26 04:02:47 UTC (rev 74776)
@@ -30,11 +30,15 @@
     run. If the working directory is the empty string or not specified
     at all, the recipe will create a working directory among the parts.
 
-(Note that, at this time, due to limitations in the Zope test runner,
- the distributions cannot be zip files. TODO: Fix the test runner!)
+environment
+    A set of environment variables that should be exported before
+    starting the tests.
 
+(Note that, at this time, due to limitations in the Zope test runner, the
+distributions cannot be zip files. TODO: Fix the test runner!)
+
 To illustrate this, we'll create a pair of projects in our sample
-buildout:
+buildout::
 
     >>> mkdir(sample_buildout, 'demo')
     >>> mkdir(sample_buildout, 'demo', 'demo')
@@ -84,7 +88,7 @@
 
     >>> write(sample_buildout, 'demo2', 'README.txt', '')
 
-Demo 2 depends on demoneeded:
+Demo 2 depends on demoneeded::
 
     >>> mkdir(sample_buildout, 'demoneeded')
     >>> mkdir(sample_buildout, 'demoneeded', 'demoneeded')
@@ -111,7 +115,7 @@
     >>> write(sample_buildout, 'demoneeded', 'README.txt', '')
 
 We'll update our buildout to install the demo project as a
-develop egg and to create the test script:
+develop egg and to create the test script::
 
     >>> write(sample_buildout, 'buildout.cfg',
     ... """
@@ -133,24 +137,24 @@
 
 We also specified the offline option to run the buildout in offline mode.
 
-Now when we run the buildout:
+Now when we run the buildout::
 
     >>> import os
     >>> os.chdir(sample_buildout)
     >>> print system(os.path.join(sample_buildout, 'bin', 'buildout') + ' -q'),
 
-We get a test script installed in our bin directory:
+We get a test script installed in our bin directory::
 
     >>> ls(sample_buildout, 'bin')
     -  buildout
     -  test
 
-We also get a part directory for the tests to run in:
+We also get a part directory for the tests to run in::
 
     >>> ls(sample_buildout, 'parts')
     d  testdemo
 
-And updating leaves its contents intact:
+And updating leaves its contents intact::
 
     >>> _ = system(os.path.join(sample_buildout, 'bin', 'test') +
     ...            ' -q --coverage=coverage')
@@ -160,7 +164,7 @@
     >>> ls(sample_buildout, 'parts', 'testdemo')
     d  coverage
 
-We can run the test script to run our demo test:
+We can run the test script to run our demo test::
 
     >>> print system(os.path.join(sample_buildout, 'bin', 'test') + ' -vv'),
     Running tests at level 1
@@ -174,7 +178,7 @@
 the eggs listed, not for their dependencies.
 
 If we leave the script option out of the configuration, then the test
-script will get it's name from the part:
+script will get it's name from the part::
 
     >>> write(sample_buildout, 'buildout.cfg',
     ... """
@@ -194,14 +198,14 @@
     -  buildout
     -  testdemo
 
-We can run the test script to run our demo test:
+We can run the test script to run our demo test::
 
     >>> print system(os.path.join(sample_buildout, 'bin', 'testdemo') + ' -q'),
     Running unit tests:
       Ran 1 tests with 0 failures and 0 errors in 0.000 seconds.
 
 If we need to include other paths in our test script, we can use the
-extra-paths option to specify them:
+extra-paths option to specify them::
 
     >>> write(sample_buildout, 'buildout.cfg',
     ... """
@@ -218,7 +222,7 @@
 
     >>> print system(os.path.join(sample_buildout, 'bin', 'buildout') + ' -q'),
 
-    >>> cat(sample_buildout, 'bin', 'testdemo')
+    >>> cat(sample_buildout, 'bin', 'testdemo') # doctest: +REPORT_NDIFF
     #!/usr/local/bin/python2.4
     <BLANKLINE>
     import sys
@@ -233,6 +237,7 @@
     sys.argv[0] = os.path.abspath(sys.argv[0])
     os.chdir('/sample-buildout/parts/testdemo')
     <BLANKLINE>
+    <BLANKLINE>
     import zope.testing.testrunner
     <BLANKLINE>
     if __name__ == '__main__':
@@ -241,7 +246,7 @@
       ])
 
 We can use the working-directory option to specify a working
-directory:
+directory::
 
     >>> write(sample_buildout, 'buildout.cfg',
     ... """
@@ -259,7 +264,7 @@
 
     >>> print system(os.path.join(sample_buildout, 'bin', 'buildout') + ' -q'),
 
-    >>> cat(sample_buildout, 'bin', 'testdemo')
+    >>> cat(sample_buildout, 'bin', 'testdemo') # doctest: +REPORT_NDIFF
     #!/usr/local/bin/python2.4
     <BLANKLINE>
     import sys
@@ -274,6 +279,7 @@
     sys.argv[0] = os.path.abspath(sys.argv[0])
     os.chdir('/foo/bar')
     <BLANKLINE>
+    <BLANKLINE>
     import zope.testing.testrunner
     <BLANKLINE>
     if __name__ == '__main__':
@@ -282,18 +288,17 @@
       ])
 
 Now that out tests use a specified working directory, their designated
-part directory is gone:
+part directory is gone::
 
     >>> ls(sample_buildout, 'parts')
 
-
 If we need to specify default options, we can use the defaults
 option. For example, Zope 3 applications typically define test suites
 in modules named ftests or tests.  The default test runner behaviour
 is to look in modules named tests.  To specify that we want to look in
 tests and ftests module, we'd supply a default for the --tests-pattern
 option.  If we like dots, we could also request more verbose output
-using the -v option. 
+using the -v option::
 
     >>> write(sample_buildout, 'buildout.cfg',
     ... """
@@ -313,7 +318,7 @@
 
     >>> print system(os.path.join(sample_buildout, 'bin', 'buildout') + ' -q'),
 
-    >>> cat(sample_buildout, 'bin', 'testdemo')
+    >>> cat(sample_buildout, 'bin', 'testdemo') 
     #!/usr/local/bin/python2.4
     <BLANKLINE>
     import sys
@@ -328,6 +333,7 @@
     sys.argv[0] = os.path.abspath(sys.argv[0])
     os.chdir('/sample-buildout/parts/testdemo')
     <BLANKLINE>
+    <BLANKLINE>
     import zope.testing.testrunner
     <BLANKLINE>
     if __name__ == '__main__':
@@ -339,8 +345,98 @@
 
 Some things to note from this example:
 
-- Parentheses are placed around the given expression.  
+- Parentheses are placed around the given expression.
 
 - Leading whitespace is removed.
 
-  
+
+To demonstrate the ``environment`` option, we first update the tests to
+include a check for an environment variable::
+
+    >>> write(sample_buildout, 'demo', 'demo', 'tests.py',
+    ... '''
+    ... import unittest
+    ... import os
+    ...
+    ... class DemoTests(unittest.TestCase):
+    ...    def test(self):
+    ...        self.assertEquals('42', os.environ.get('zc.recipe.testrunner', '23'))
+    ...
+    ... def test_suite():
+    ...     return unittest.makeSuite(DemoTests)
+    ... ''')
+
+Running them with the current buildout will produce a failure::
+
+    >>> print system(os.path.join(sample_buildout, 'bin', 'testdemo') + ' -vv'), # doctest: +REPORT_NDIFF +ELLIPSIS
+    Running tests at level 1
+    Running unit tests:
+      Running:
+     test (demo.tests.DemoTests)
+    <BLANKLINE>
+    Failure in test test (demo.tests.DemoTests)
+    Traceback (most recent call last):
+      ...
+        raise self.failureException, \
+    AssertionError: '42' != '23'
+    <BLANKLINE>
+    <BLANKLINE>
+      Ran 1 tests with 1 failures and 0 errors in 0.001 seconds.
+    <BLANKLINE>
+    Tests with failures:
+       test (demo.tests.DemoTests)
+
+
+Let's update the buildout to specify the environment variable for the test
+runner::
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... develop = demo
+    ... parts = testdemo
+    ... offline = true
+    ...
+    ... [testdemo]
+    ... recipe = zc.recipe.testrunner
+    ... eggs = demo
+    ... environment = testenv
+    ...
+    ... [testenv]
+    ... zc.recipe.testrunner = 42
+    ... """)
+
+We run buildout and see that the test runner script now includes setting up
+the environment variable. Also, the tests pass again::
+
+    >>> print system(os.path.join(sample_buildout, 'bin', 'buildout') + ' -q'),
+
+    >>> cat(sample_buildout, 'bin', 'testdemo') # doctest: +REPORT_NDIFF
+    #!/usr/local/bin/python2.4
+    <BLANKLINE>
+    import sys
+    sys.path[0:0] = [
+      '/sample-buildout/demo',
+      '/sample-buildout/eggs/zope.testing-3.0-py2.3.egg',
+      '/sample-buildout/eggs/setuptools-0.6-py1.3.egg',
+      ]
+    <BLANKLINE>
+    import os
+    sys.argv[0] = os.path.abspath(sys.argv[0])
+    os.chdir('/sample-buildout/parts/testdemo')
+    os.environ['zc.recipe.testrunner'] = '42'
+    <BLANKLINE>
+    <BLANKLINE>
+    import zope.testing.testrunner
+    <BLANKLINE>
+    if __name__ == '__main__':
+        zope.testing.testrunner.run([
+      '--test-path', '/sample-buildout/demo',
+      ])
+
+    >>> print system(os.path.join(sample_buildout, 'bin', 'testdemo') + ' -vv'),
+    Running tests at level 1
+    Running unit tests:
+      Running:
+     test (demo.tests.DemoTests)
+      Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.

Modified: zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/__init__.py
===================================================================
--- zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/__init__.py	2007-04-26 03:59:06 UTC (rev 74775)
+++ zc.recipe.testrunner/trunk/src/zc/recipe/testrunner/__init__.py	2007-04-26 04:02:47 UTC (rev 74776)
@@ -56,7 +56,13 @@
                 os.mkdir(wd)
             dest.append(wd)
         initialization = initialization_template % wd
-        
+
+        env_section = options.get('environment', '').strip()
+        if env_section:
+            env = self.buildout[env_section]
+            for key, value in env.items():
+                initialization += env_template % (key, value)
+
         dest.extend(zc.buildout.easy_install.scripts(
             [(options['script'], 'zope.testing.testrunner', 'run')],
             ws, options['executable'],
@@ -76,7 +82,11 @@
 arg_template = """[
   '--test-path', %(TESTPATH)s,
   ]"""
-                                 
+
 initialization_template = """import os
 sys.argv[0] = os.path.abspath(sys.argv[0])
-os.chdir(%r)"""
+os.chdir(%r)
+"""
+
+env_template = """os.environ['%s'] = %r
+"""



More information about the Checkins mailing list