File tree Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Original file line number Diff line number Diff line change
1
+ Improve type checking for some exception-raising functions (``pytest.xfail ``, ``pytest.skip ``, etc)
2
+ so they provide better error messages when users meant to use marks (for example ``@pytest.xfail ``
3
+ instead of ``@pytest.mark.xfail ``).
Original file line number Diff line number Diff line change @@ -13,16 +13,19 @@ class OutcomeException(BaseException):
13
13
"""
14
14
15
15
def __init__ (self , msg = None , pytrace = True ):
16
+ if msg is not None and not isinstance (msg , str ):
17
+ error_msg = (
18
+ "{} expected string as 'msg' parameter, got '{}' instead.\n "
19
+ "Perhaps you meant to use a mark?"
20
+ )
21
+ raise TypeError (error_msg .format (type (self ).__name__ , type (msg ).__name__ ))
16
22
BaseException .__init__ (self , msg )
17
23
self .msg = msg
18
24
self .pytrace = pytrace
19
25
20
26
def __repr__ (self ):
21
27
if self .msg :
22
- val = self .msg
23
- if isinstance (val , bytes ):
24
- val = val .decode ("UTF-8" , errors = "replace" )
25
- return val
28
+ return self .msg
26
29
return "<{} instance>" .format (self .__class__ .__name__ )
27
30
28
31
__str__ = __repr__
Original file line number Diff line number Diff line change 11
11
from _pytest import outcomes
12
12
from _pytest import reports
13
13
from _pytest import runner
14
+ from _pytest .outcomes import OutcomeException
14
15
15
16
16
17
class TestSetupState :
@@ -990,3 +991,18 @@ def test_func():
990
991
rep = reports [1 ]
991
992
assert rep .capstdout == ""
992
993
assert rep .capstderr == ""
994
+
995
+
996
+ def test_outcome_exception_bad_msg ():
997
+ """Check that OutcomeExceptions validate their input to prevent confusing errors (#5578)"""
998
+
999
+ def func ():
1000
+ pass
1001
+
1002
+ expected = (
1003
+ "OutcomeException expected string as 'msg' parameter, got 'function' instead.\n "
1004
+ "Perhaps you meant to use a mark?"
1005
+ )
1006
+ with pytest .raises (TypeError ) as excinfo :
1007
+ OutcomeException (func )
1008
+ assert str (excinfo .value ) == expected
Original file line number Diff line number Diff line change @@ -1066,7 +1066,8 @@ def test_module_level_skip_error(testdir):
1066
1066
testdir .makepyfile (
1067
1067
"""
1068
1068
import pytest
1069
- @pytest.skip
1069
+ pytest.skip("skip_module_level")
1070
+
1070
1071
def test_func():
1071
1072
assert True
1072
1073
"""
You can’t perform that action at this time.
0 commit comments