Last active
September 24, 2020 21:04
-
-
Save untitaker/dfd545e836e97d3f7daa6942d877c644 to your computer and use it in GitHub Desktop.
Workaround for applying pytest markers in baseclasses used with multiple inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import pytest | |
class BaseMeta(type): | |
@property | |
def pytestmark(self): | |
return ( | |
getattr(self, "_pytestmark", []) + | |
list(itertools.chain.from_iterable(getattr(x, "_pytestmark", []) for x in self.__mro__)) | |
) | |
@pytestmark.setter | |
def pytestmark(self, value): | |
self._pytestmark = value | |
class Base(object): | |
# Without this metaclass, foo and bar markers override each other, and test_dings will only have one marker | |
# With the metaclass, test_dings will have both | |
__metaclass__ = BaseMeta | |
@pytest.mark.foo | |
class Foo(Base): | |
def foo(self): | |
return "foo" | |
@pytest.mark.bar | |
class Bar(Base): | |
def bar(self): | |
return "bar" | |
class TestDings(Foo, Bar): | |
def test_dings(self): | |
# This test should have both markers, foo and bar. | |
# In practice markers are resolved using MRO (so foo wins), unless the | |
# metaclass is applied | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment