tests: improve test for nose.raises#6521
Conversation
c86cfa7 to
0087db6
Compare
| [ | ||
| "test_raises.py::test_raises_runtimeerror PASSED*", | ||
| "test_raises.py::test_raises_baseexception_not_caught FAILED*", | ||
| "test_raises.py::test_raises_baseexception_caught PASSED*", |
There was a problem hiding this comment.
Without the cast here it would display "test_raises.py::test_raises_runtimeerror <- /…/test_raises0/test_raises.py PASSED` here.
There was a problem hiding this comment.
Hmm sorry, what "cast" you mean?
There was a problem hiding this comment.
@nicoddemus https://github.com/blueyed/pytest/blob/156a41a855d28ae9f6d83545af69b51fb26f199e/src/_pytest/terminal.py#L747-L752
Since location fails to make the path relative it is absolute, and then triggers that code.
|
LGTM but @bluetech specifically said he would like to revisit his original commit instead of reverting it, so let's wait for his review. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
src/_pytest/nodes.py
Outdated
| fspath = self.session._node_location_to_relpath(location[0]) | ||
| fspath = location[0] | ||
| if not isinstance(fspath, py.path.local): | ||
| fspath = py.path.local(fspath) |
There was a problem hiding this comment.
This might be triggered via
Lines 288 to 292 in 156a41a
There was a problem hiding this comment.
Thanks I was wondering this myself because a cursory look at nose.py did not bring anything to light.
b9c303a to
8cd591f
Compare
|
Thanks @blueyed for hardening the test! The test improvements looks good to me, the rest I can't quite assess ATM.
You did -- the revert PR message explains why I was sure the assert was safe, but I'll be more careful with these. Amusingly, this is just the sort of bugs that types help prevent, and we'll get there eventually, but the road can be bumpy :) One thing I'm not sure about in general regarding this part of the code is: are custom |
Yes, I would say it is public, and therefore we need to handle |
This comment has been minimized.
This comment has been minimized.
87091dd to
20d8e04
Compare
OK. I think it will be better to use the same signature for all diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py
index d7ca888cc..ffc1890d4 100644
--- a/src/_pytest/doctest.py
+++ b/src/_pytest/doctest.py
@@ -308,7 +308,7 @@ class DoctestItem(pytest.Item):
else:
return super().repr_failure(excinfo)
- def reportinfo(self) -> Tuple[py.path.local, int, str]:
+ def reportinfo(self) -> Tuple[Union[str, py.path.local], int, str]:
return self.fspath, self.dtest.lineno, "[doctest] %s" % self.name
diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py
index 080f079cd..ab976efae 100644
--- a/src/_pytest/nodes.py
+++ b/src/_pytest/nodes.py
@@ -462,9 +462,10 @@ class Item(Node):
@cached_property
def location(self) -> Tuple[str, Optional[int], str]:
location = self.reportinfo()
- fspath = location[0]
- if not isinstance(fspath, py.path.local):
- fspath = py.path.local(fspath)
- fspath = self.session._node_location_to_relpath(fspath)
+ if isinstance(location[0], py.path.local):
+ fspath = location[0]
+ else:
+ fspath = py.path.local(location[0])
+ relfspath = self.session._node_location_to_relpath(fspath)
assert type(location[2]) is str
- return (fspath, location[1], location[2])
+ return (relfspath, location[1], location[2])
diff --git a/src/_pytest/python.py b/src/_pytest/python.py
index 3d2916c83..82dca3bcc 100644
--- a/src/_pytest/python.py
+++ b/src/_pytest/python.py
@@ -369,7 +369,12 @@ class PyCollector(PyobjMixin, nodes.Collector):
if not isinstance(res, list):
res = [res]
values.extend(res)
- values.sort(key=lambda item: item.reportinfo()[:2])
+
+ def sort_key(item):
+ fspath, lineno, _ = item.reportinfo()
+ return (str(fspath), lineno)
+
+ values.sort(key=sort_key)
return values
def _makeitem(self, name, obj): |
This comment has been minimized.
This comment has been minimized.
Why? If it is known to return a |
|
I think it adds to the confusion. When all the subclasses publicize the same interface that the base class dictates, there is less chance of errors creeping in when the types are interchanged. But you can leave it as just |
|
@bluetech |
|
@bluetech are you OK with it then? |
This should probably get transferred into a `pytest.fail` really, but tests/documents the current behavior.
…ency"" Without changes to test_itemreport_reportinfo. This reverts commit fb99b5c. Conflicts: testing/test_nose.py
20d8e04 to
9c7b3c5
Compare
bluetech
left a comment
There was a problem hiding this comment.
LGTM with a couple of suggestions.
I also think we should fold this change in here as well:
diff --git a/src/_pytest/python.py b/src/_pytest/python.py
index 3d2916c83..82dca3bcc 100644
--- a/src/_pytest/python.py
+++ b/src/_pytest/python.py
@@ -369,7 +369,12 @@ class PyCollector(PyobjMixin, nodes.Collector):
if not isinstance(res, list):
res = [res]
values.extend(res)
- values.sort(key=lambda item: item.reportinfo()[:2])
+
+ def sort_key(item):
+ fspath, lineno, _ = item.reportinfo()
+ return (str(fspath), lineno)
+
+ values.sort(key=sort_key)
return values
def _makeitem(self, name, obj):The idea is that since item.reportinfo()[1] is Union[str, py.path.local], sorting by it can compare str with py.path.local. While this does the right thing, I find it distasteful, so I prefer that the str(fspath) is explicit.
bluetech
left a comment
There was a problem hiding this comment.
Thanks, LGTM now, if CI passes.
This should probably get transferred into a
pytest.failreally, buttests/documents the current behavior.