Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions pytest_describe/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import sys
import types
from _pytest.python import PyCollector
import pytest


PYTEST_GTE_7_0 = (
hasattr(pytest, 'version_tuple') and pytest.version_tuple >= (7, 0)
)
PYTEST_GTE_5_4 = hasattr(pytest.Collector, 'from_parent')


def trace_function(funcobj, *args, **kwargs):
Expand Down Expand Up @@ -55,19 +61,24 @@ def evaluate_shared_behavior(funcobj):
return funcobj._shared_functions


class DescribeBlock(PyCollector):
class DescribeBlock(pytest.Module):
"""Module-like object representing the scope of a describe block"""

@classmethod
def from_parent(cls, parent, obj):
name = obj.__name__
try:
from_parent_super = super().from_parent
except AttributeError: # PyTest < 5.4
self = cls(name, parent)
name = getattr(obj, '_mangled_name', obj.__name__)
nodeid = parent.nodeid + '::' + name
if PYTEST_GTE_7_0:
self = super().from_parent(
parent=parent, path=parent.path, nodeid=nodeid,
)
elif PYTEST_GTE_5_4:
self = super().from_parent(
parent=parent, fspath=parent.fspath, nodeid=nodeid,
)
else:
self = from_parent_super(parent, name=name)
self._name = getattr(obj, '_mangled_name', name)
self = cls(parent=parent, fspath=parent.fspath, nodeid=nodeid)
self.name = name
self.funcobj = obj
return self

Expand All @@ -78,10 +89,6 @@ def collect(self):
def _getobj(self):
return self._importtestmodule()

def _makeid(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By passing nodeid directly, we don't need this. Also, newer versions of pytest no longer use _makeid.

Copy link
Member

@Cito Cito Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this was removed here in pytest 3.5, and we now require pytest 4.6.

"""Magic that makes fixtures local to each scope"""
return f'{self.parent.nodeid}::{self._name}'

def _importtestmodule(self):
"""Import a describe block as if it was a module"""
module = make_module_from_function(self.funcobj)
Expand All @@ -100,7 +107,7 @@ def classnamefilter(self, name):
return False

def __repr__(self):
return f"<{self.__class__.__name__} {self._name!r}>"
return f"<{self.__class__.__name__} {self.name!r}>"


def pytest_pycollect_makeitem(collector, name, obj):
Expand Down