Skip to content

Conversation

@acjreno
Copy link
Contributor

@acjreno acjreno commented Apr 12, 2023

Closes #10831.

This fixes a small bug where running tests that contained pytest.fail(pytrace=False) with the --tb=line flag set results in an output of "None" in the Failures section of the output, and adds a test to ensure the behavior is correct.

First time contributor so any advice/suggestions are welcome and encouraged!

excinfo = ExceptionInfo.from_exc_info(excinfo.value.excinfo)
if isinstance(excinfo.value, fail.Exception):
if not excinfo.value.pytrace:
if not excinfo.value.pytrace and style != "line":
Copy link
Member

Choose a reason for hiding this comment

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

The reason "value" doesn't end up working for this seems to be this line

reprcrash: Optional[ReprFileLocation] = (
excinfo_._getreprcrash() if self.style != "value" else None
)

which says that for style = "value", the reprcrash is None. The reprcrash is what --tb=line uses:

pytest/src/_pytest/terminal.py

Lines 1011 to 1014 in 7834b3b

if self.config.option.tbstyle == "line":
for rep in reports:
line = self._getcrashline(rep)
self.write_line(line)

this code is currently the only place where --tb=line condition appears which seems like a nice property to me.

So what I wonder is why "value" doesn't want a reprcrash. This comes from 94c7b8b which introduced "line". I think the idea was to maintain the previous behavior where there was also no reprcrash for pytrace=False.

What is reprcrash anyway? TBH I'm not sure (pytest is full of mysteries like that). [...After looking at the code...] It seems to be used for the following:

  1. For --tb=line. This is the one we want to affect so ✔️
  2. Shown in the short test summary info. Seems good to show the reason message here so ✔️ .
  3. In junitxml output. Not an expert but the change seems pretty minor - adding the Failed: prefix. ✔️
  4. Some other irrelevant stuff to "value" (skips, KeyboardInterruprt). ✔️

So I think instead of this change, we can replace

reprcrash: Optional[ReprFileLocation] = ( 
    excinfo_._getreprcrash() if self.style != "value" else None 
) 

to

reprcrash = excinfo_._getreprcrash()

Can you try this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't realize we were on the cutting edge of development here:

reprcrash: Optional[ReprFileLocation] = (
excinfo_._getreprcrash() if self.style != "value" else None
)

was added as of the latest release, one day after I authored my PR!

I removed my changes and changed those three lines to

reprcrash = excinfo_._getreprcrash()

and all of the tests, including the one I added, passed! Pushing the changes in a new commit now.

Copy link
Member

Choose a reason for hiding this comment

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

Yes we've recently had some crash due to reprcrash :)

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Thanks @acjreno, LGTM. Will merge in the next few days if there are no other comments.

)
result = pytester.runpytest("--tb=line")
s = result.stdout.str()
assert "None" not in s
Copy link
Contributor

@ikonst ikonst Apr 16, 2023

Choose a reason for hiding this comment

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

Without the context of the issue we're fixing, it's a bit odd to test that specifically. I'd rather remove this assertion. It's not the "None" that troubled me, as the lack of the original message.

@bluetech bluetech enabled auto-merge (squash) April 16, 2023 20:14
@bluetech bluetech merged commit 41f57ef into pytest-dev:main Apr 16, 2023
"""
)
result = pytester.runpytest("--tb=line")
result.stdout.str()
Copy link
Contributor

Choose a reason for hiding this comment

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

btw @acjreno, this line probably does nothing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytrace=False and "--tb=line" reports "None"

3 participants