Skip to content

Instantly share code, notes, and snippets.

@jml
Last active December 11, 2015 08:18
Show Gist options
  • Save jml/4572369 to your computer and use it in GitHub Desktop.
Save jml/4572369 to your computer and use it in GitHub Desktop.
=== modified file 'testtools/testsuite.py'
--- testtools/testsuite.py 2013-01-18 09:17:19 +0000
+++ testtools/testsuite.py 2013-01-19 11:05:50 +0000
@@ -147,6 +147,29 @@
return [(suite_id, suite_or_case)]
+def filtered_tests(suite_or_case, predicate):
+ """Remove tests from suite_or case where predicate(case) returns True.
+
Two thoughts:
* This is the inverse of the `filter` built-in. Shouldn't it keep tests that return True?
* Although `filtered_tests` provides symmetry with `sorted_tests`, it seems a bit odd. This is totally a matter of taste, but I think I'd prefer `filter_tests`
+ :param suite_or_case: A test suite or test case.
+ :param predicate: A function to decide on the fate of a given test.
+ :return: suite_or_case, unless suite_or_case was a case that itself
+ fails the predicate.
+
What does it return if `suite_or_case` was a case that fails the predicate?
+ This helper exists to provide backwards compatability with older versions
+ of Python (currently all versions :)) that don't have a native filter()
+ method on Test(Case|Suite).
+
+ For subclasses of TestSuite, filtering is done by:
+ - attempting to call filter(predicate)
This should probably read `suite_or_case.filter(predicate)`
+ - if there is no filter method, iterating the suite and identifying
+ tests to remove, then removing them from _tests
So this function mutates the incoming suite?
+ - and recursing into child non-cases
+
+ To provide compatability with this routine, just define a filter() method
+ that will remove any tests that return True from predicate(case).
That sounds mutatey too. Probably worth highlighting.
+ """
+
+
def sorted_tests(suite_or_case, unpack_outer=False):
"""Sort suite_or_case while preserving non-vanilla TestSuites."""
tests = _flatten_tests(suite_or_case, unpack_outer=unpack_outer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment