Skip to content

Instantly share code, notes, and snippets.

@untitaker
Last active September 24, 2020 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save untitaker/dfd545e836e97d3f7daa6942d877c644 to your computer and use it in GitHub Desktop.
Save untitaker/dfd545e836e97d3f7daa6942d877c644 to your computer and use it in GitHub Desktop.
Workaround for applying pytest markers in baseclasses used with multiple inheritance
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