1. David Burns
  2. pytest

Commits

David Burns  committed 3bff0a6

Added filters by marks

  • Participants
  • Parent commits 097870d
  • Branches marks

Comments (0)

Files changed (3)

File _pytest/core.py

View file
  • Ignore whitespace
 default_plugins = (
  "config mark main terminal runner python pdb unittest capture skipping "
  "tmpdir monkeypatch recwarn pastebin helpconfig nose assertion genscript "
- "junitxml resultlog doctest").split()
+ "junitxml resultlog doctest markification").split()
 
 class TagTracer:
     def __init__(self):

File _pytest/markification.py

View file
  • Ignore whitespace
+import copy
+import pytest, py
+from _pytest.mark import MarkInfo
+
+def pytest_addoption(parser):
+    group = parser.getgroup("general")
+    group._addoption('-f',
+        action="append", dest="filter", default=[], metavar="FILTEREXPR",
+        help="only run tests which have marks that match given"
+             "keyword expression.  "
+             "An expression consists of space-separated terms. "
+             "Each term must match. Precede a term with '-' to negate.")
+
+# blatantly lifted from _pytest.mark.py
+def pytest_collection_modifyitems(items, config):
+    filterlist = config.option.filter
+    if not filterlist:
+        return
+
+    remaining = []
+    deselected = []
+    for filtr in filterlist:
+        dirty_items = copy.copy(items)
+        
+        for item in dirty_items:
+            if filtr[:1] != '-' and skipbykeyword(item, filtr):
+                # skip
+                deselected.append(item)
+            elif filtr[:1] == '-' and skipbykeyword(item, filtr):
+                # remove
+                deselected.append(item)
+                if item in remaining:
+                    del remaining[remaining.index(item)]
+            else:
+                # keep
+                remaining.append(item)
+
+    deselected = [i for i in set(deselected)]
+    remaining = [i for i in set(remaining)]
+    if deselected:
+        config.hook.pytest_deselected(items=deselected)
+        items[:] = remaining
+
+# blatantly lifted from _pytest.mark.py
+def skipbykeyword(colitem, keywordexpr):
+    """ return True if they given keyword expression means to
+        skip this collector/item.
+    """
+    if not keywordexpr:
+        return
+
+    itemkeywords = getkeywords(colitem)
+    for key in filter(None, keywordexpr.split()):
+        eor = key[:1] == '-'
+        if eor:
+            key = key[1:]
+        if not (eor ^ matchonekeyword(key, itemkeywords)):
+            return True
+
+# blatantly lifted from _pytest.mark.py but checks not for whether it is a MarkInfo object
+def getkeywords(node):
+    keywords = {}
+    while node is not None:
+        for keyword in node.keywords:
+            if isinstance(node.keywords[keyword], MarkInfo):
+                keywords[keyword] = node.keywords[keyword]
+        node = node.parent
+    return keywords
+
+# blatantly lifted from _pytest.mark.py
+def matchonekeyword(key, itemkeywords):
+    for elem in key.split("."):
+        for kw in itemkeywords:
+            if elem in kw:
+                break
+        else:
+            return False
+    return True
+    
+class MarkFiltration(object):
+    pass
+

File testing/test_collection.py

View file
  • Ignore whitespace
     
 
 
+class Test_collection:
+    def test_check_collect_foo(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+            
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+            
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", "-f", "foo", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 2
+        acceptable = ['test_baz', 'test_bar']
+        for passd in passed:
+            assert passd.nodeid.split('::')[1] in acceptable
+
+    def test_check_collect_bar(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", "-f", "bar", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 2
+        acceptable = ['test_baz', 'test_foo']
+        for passd in passed:
+            assert passd.nodeid.split('::')[1] in acceptable
+            
+    def test_check_collect_foo_and_bar(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", '-f', "foo bar", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 1
+        assert passed[0].nodeid.split('::')[1] == 'test_baz'
+        
+    def test_check_collect_foo_or_bar(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", '-f', "foo", "-f", "bar", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 3
+        acceptable = ['test_baz', 'test_foo', 'test_bar']
+        for passd in passed:
+            assert passd.nodeid.split('::')[1] in acceptable
+
+    def test_check_collect_foo_but_not_bar(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", '-f', "foo -bar", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 1
+        assert passed[0].nodeid.split('::')[1] == 'test_bar'
+        
+    def test_check_collect_foo_but_not_bar_as_or(self, testdir):
+        p = testdir.makepyfile("""
+            import pytest
+
+            @pytest.mark.bar
+            def test_foo():
+                pass
+
+            @pytest.mark.foo
+            def test_bar():
+                pass
+
+            @pytest.mark.foo
+            @pytest.mark.bar
+            def test_baz():
+                pass
+        """)
+        reprec = testdir.inline_run("-s", '-f', "foo", "-f", "-bar", p)
+        passed, skipped, failed = reprec.listoutcomes()
+        assert len(passed) == 1
+        assert passed[0].nodeid.split('::')[1] == 'test_bar'