Skip to content

'.[' replaced with '[' in the headline shown of the test report #6189

Closed
@linw1995

Description

@linw1995
bug.py F                                                                 [100%]

=================================== FAILURES ===================================
_________________________________ test_boo[.[] _________________________________

a = '..['

    @pytest.mark.parametrize("a",["..["])
    def test_boo(a):
>       assert 0
E       assert 0

bug.py:6: AssertionError
============================== 1 failed in 0.06s ===============================

The "test_boo[..[]" replaced with "test_boo[.[]" in the headline shown with long report output.

The same problem also causing the vscode-python test discovery error.

What causing the problem

I trace back the source code.

@property
def head_line(self):
"""
**Experimental**
Returns the head line shown with longrepr output for this report, more commonly during
traceback representation during failures::
________ Test.foo ________
In the example above, the head_line is "Test.foo".
.. note::
This function is considered **experimental**, so beware that it is subject to changes
even in patch releases.
"""
if self.location is not None:
fspath, lineno, domain = self.location
return domain

The headline comes from line 148.

pytest/src/_pytest/nodes.py

Lines 432 to 441 in 92d6a05

@property
def location(self):
try:
return self._location
except AttributeError:
location = self.reportinfo()
fspath = self.session._node_location_to_relpath(location[0])
location = (fspath, location[1], str(location[2]))
self._location = location
return location

location comes from line 437 location = self.reportinfo()

def reportinfo(self):
# XXX caching?
obj = self.obj
compat_co_firstlineno = getattr(obj, "compat_co_firstlineno", None)
if isinstance(compat_co_firstlineno, int):
# nose compatibility
fspath = sys.modules[obj.__module__].__file__
if fspath.endswith(".pyc"):
fspath = fspath[:-1]
lineno = compat_co_firstlineno
else:
fspath, lineno = getfslineno(obj)
modpath = self.getmodpath()
assert isinstance(lineno, int)
return fspath, lineno, modpath

The headline comes from line 306 modpath = self.getmodpath()

def getmodpath(self, stopatmodule=True, includemodule=False):
""" return python path relative to the containing module. """
chain = self.listchain()
chain.reverse()
parts = []
for node in chain:
if isinstance(node, Instance):
continue
name = node.name
if isinstance(node, Module):
name = os.path.splitext(name)[0]
if stopatmodule:
if includemodule:
parts.append(name)
break
parts.append(name)
parts.reverse()
s = ".".join(parts)
return s.replace(".[", "[")

This line of code return s.replace(".[", "[") causes the problem. We should replace it with return s. After changing this, run tox -e linting,py37, pass all the tests and resolve this issue. But I can't find this line of code for what purpose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: reportingrelated to terminal output and user-facing messages and errorstype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions