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):
1313 """
1414
1515 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__ ))
1622 BaseException .__init__ (self , msg )
1723 self .msg = msg
1824 self .pytrace = pytrace
1925
2026 def __repr__ (self ):
2127 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
2629 return "<{} instance>" .format (self .__class__ .__name__ )
2730
2831 __str__ = __repr__
Original file line number Diff line number Diff line change 1111from _pytest import outcomes
1212from _pytest import reports
1313from _pytest import runner
14+ from _pytest .outcomes import OutcomeException
1415
1516
1617class TestSetupState :
@@ -990,3 +991,18 @@ def test_func():
990991 rep = reports [1 ]
991992 assert rep .capstdout == ""
992993 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):
10661066 testdir .makepyfile (
10671067 """
10681068 import pytest
1069- @pytest.skip
1069+ pytest.skip("skip_module_level")
1070+
10701071 def test_func():
10711072 assert True
10721073 """
You can’t perform that action at this time.
0 commit comments