[Checkins] SVN: zc.selenium/trunk/ Feature: Added a `-t` option to filter selenium tests by regexps. You can

Marius Gedminas marius at pov.lt
Tue Sep 30 18:16:46 EDT 2008


Log message for revision 91642:
  Feature: Added a `-t` option to filter selenium tests by regexps.  You can
  also specify multiple `-t` options.
  
  

Changed:
  U   zc.selenium/trunk/CHANGES.txt
  U   zc.selenium/trunk/src/zc/selenium/pytest.py
  U   zc.selenium/trunk/src/zc/selenium/pytest.txt
  U   zc.selenium/trunk/src/zc/selenium/selenium.py
  U   zc.selenium/trunk/src/zc/selenium/tests.py

-=-
Modified: zc.selenium/trunk/CHANGES.txt
===================================================================
--- zc.selenium/trunk/CHANGES.txt	2008-09-30 18:50:42 UTC (rev 91641)
+++ zc.selenium/trunk/CHANGES.txt	2008-09-30 22:16:43 UTC (rev 91642)
@@ -12,6 +12,9 @@
   not dependent to include the default layer in the default skin. (Who does
   this? What a security hole!)
 
+- Feature: Added a `-t` option to filter selenium tests by regexps.  You can
+  also specify multiple `-t` options.
+
 - Bug: Added documentation on how to setup `zc.selenium`.
 
 - Bug: Allow wsgi option to work with python 2.5

Modified: zc.selenium/trunk/src/zc/selenium/pytest.py
===================================================================
--- zc.selenium/trunk/src/zc/selenium/pytest.py	2008-09-30 18:50:42 UTC (rev 91641)
+++ zc.selenium/trunk/src/zc/selenium/pytest.py	2008-09-30 22:16:43 UTC (rev 91642)
@@ -18,6 +18,7 @@
 import os.path
 import sys
 import urlparse
+import re
 from xml.sax.saxutils import escape
 
 import zope.publisher.interfaces.browser
@@ -26,16 +27,57 @@
 
 
 class ISeleniumTest(zope.publisher.interfaces.browser.IBrowserPublisher):
-    """A page that is a selenium test
-    """
+    """A page that is a selenium test."""
 
     def __call__():
-        """Return the selenium test
-        """
+        """Return the selenium test as an HTML string."""
 
+
+PATTERNS = ()
+
+
+def selectTestsToRun(patterns):
+    """Filter the Selenium tests in the test suite.
+
+    Only tests with names matching any of the regular expressions in the
+    list of patterns will be run.
+
+    selectTestsToRun([]) removes any filtering.
+    """
+    global PATTERNS
+    PATTERNS = patterns
+
+
+def matchesAnyPattern(name, patterns):
+    """Check if a test name matches any of the patterns.
+
+        >>> matchesAnyPattern('foobar', ['ab', 'ob'])
+        True
+        >>> matchesAnyPattern('foobar', ['bo', 'bu'])
+        False
+
+    The patterns are regular expressions
+
+        >>> matchesAnyPattern('foobar', ['^f.*ar$'])
+        True
+
+    If the pattern list is empty, returns True
+
+        >>> matchesAnyPattern('foobar', [])
+        True
+
+    """
+    for pattern in patterns:
+        if re.search(pattern, name):
+            return True
+    return not patterns
+
+
 def suite(request):
-    tests = [item for item in component.getAdapters([request], ISeleniumTest)]
-    tests.sort()
+    tests = sorted(component.getAdapters([request], ISeleniumTest))
+    if PATTERNS:
+        tests = [(name, test) for name, test in tests
+                 if matchesAnyPattern(name, PATTERNS)]
     return '\n'.join([
         ('<tr><td><a href="/@@/%s">%s</a></td></tr>' % (
              name,

Modified: zc.selenium/trunk/src/zc/selenium/pytest.txt
===================================================================
--- zc.selenium/trunk/src/zc/selenium/pytest.txt	2008-09-30 18:50:42 UTC (rev 91641)
+++ zc.selenium/trunk/src/zc/selenium/pytest.txt	2008-09-30 22:16:43 UTC (rev 91642)
@@ -508,3 +508,17 @@
 For each test, a row is output with a link to the test resource and a
 link title computed from the test doc string.  The tests are output in
 adapter (resource) name order.
+
+You can specify a test filter
+
+    >>> zc.selenium.pytest.selectTestsToRun(['first'])
+    >>> print zc.selenium.pytest.suite(TestRequest())
+    <tr><td><a href="/@@/first.html">My first test</a></td></tr>
+
+or reset it
+
+    >>> zc.selenium.pytest.selectTestsToRun([])
+    >>> print zc.selenium.pytest.suite(TestRequest())
+    <tr><td><a href="/@@/first.html">My first test</a></td></tr>
+    <tr><td><a href="/@@/second.html">My second test</a></td></tr>
+

Modified: zc.selenium/trunk/src/zc/selenium/selenium.py
===================================================================
--- zc.selenium/trunk/src/zc/selenium/selenium.py	2008-09-30 18:50:42 UTC (rev 91641)
+++ zc.selenium/trunk/src/zc/selenium/selenium.py	2008-09-30 22:16:43 UTC (rev 91642)
@@ -29,6 +29,8 @@
 
 from zope.testing import testrunner
 
+from zc.selenium.pytest import selectTestsToRun
+
 # Compute a default port; this is simple and doesn't ensure that the
 # port is available, but does better than just hardcoding a port
 # number.  The goal is to avoid browser cache effects due to resource
@@ -168,6 +170,8 @@
     parser.add_option('-u', '--base-url', dest='base_url',
                       default='http://localhost:%(port)s/',
                       help='The base URL of the Zope site (may contain skin).')
+    parser.add_option('-t', '--test', action="append", dest='tests',
+                      help='specify tests to run')
     parser.add_option(
         '--coverage', action="store", type='string', dest='coverage',
         help="""\
@@ -200,6 +204,8 @@
     if options.random_port:
         options.port = random_port()
 
+    selectTestsToRun(options.tests)
+
     runner = run_zope
     if options.wsgi_app:
         runner = make_wsgi_run_zope(options.wsgi_app)

Modified: zc.selenium/trunk/src/zc/selenium/tests.py
===================================================================
--- zc.selenium/trunk/src/zc/selenium/tests.py	2008-09-30 18:50:42 UTC (rev 91641)
+++ zc.selenium/trunk/src/zc/selenium/tests.py	2008-09-30 22:16:43 UTC (rev 91642)
@@ -22,9 +22,11 @@
 
 
 def test_suite():
-    return doctest.DocFileSuite(
-        'pytest.txt',
-        optionflags=(doctest.ELLIPSIS | doctest.REPORT_NDIFF))
+    return unittest.TestSuite([
+        doctest.DocFileSuite('pytest.txt',
+                    optionflags=doctest.ELLIPSIS|doctest.REPORT_NDIFF),
+        doctest.DocTestSuite('zc.selenium.pytest'),
+    ])
 
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')



More information about the Checkins mailing list