Skip to content

Commit 26f590b

Browse files
committed
Merge pull request #789 from pytest-dev/issue-114
Skip reports refer to pytest code instead of original test functions
2 parents 0431b8b + f90b2f8 commit 26f590b

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
- fix issue735: assertion failures on debug versions of Python 3.4+
1111
Thanks Benjamin Peterson.
1212

13+
- fix issue114: skipif marker reports to internal skipping plugin;
14+
Thanks Floris Bruynooghe for reporting and Bruno Oliveira for the PR.
15+
16+
- fix issue748: unittest.SkipTest reports to internal pytest unittest plugin.
17+
Thanks Thomas De Schampheleire for reporting and Bruno Oliveira for the PR.
18+
1319
2.7.1 (compared to 2.7.0)
1420
-----------------------------
1521

_pytest/skipping.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def getexplanation(self):
137137
def pytest_runtest_setup(item):
138138
evalskip = MarkEvaluator(item, 'skipif')
139139
if evalskip.istrue():
140+
item._evalskip = evalskip
140141
pytest.skip(evalskip.getexplanation())
141142
item._evalxfail = MarkEvaluator(item, 'xfail')
142143
check_xfail_no_run(item)
@@ -156,6 +157,7 @@ def pytest_runtest_makereport(item, call):
156157
outcome = yield
157158
rep = outcome.get_result()
158159
evalxfail = getattr(item, '_evalxfail', None)
160+
evalskip = getattr(item, '_evalskip', None)
159161
# unitttest special case, see setting of _unexpectedsuccess
160162
if hasattr(item, '_unexpectedsuccess') and rep.when == "call":
161163
# we need to translate into how pytest encodes xpass
@@ -177,6 +179,13 @@ def pytest_runtest_makereport(item, call):
177179
elif call.when == "call":
178180
rep.outcome = "failed" # xpass outcome
179181
rep.wasxfail = evalxfail.getexplanation()
182+
elif evalskip is not None and rep.skipped and type(rep.longrepr) is tuple:
183+
# skipped by mark.skipif; change the location of the failure
184+
# to point to the item definition, otherwise it will display
185+
# the location of where the skip exception was raised within pytest
186+
filename, line, reason = rep.longrepr
187+
filename, line = item.location[:2]
188+
rep.longrepr = filename, line, reason
180189

181190
# called by terminalreporter progress reporting
182191
def pytest_report_teststatus(report):

_pytest/unittest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# for transfering markers
1111
from _pytest.python import transfer_markers
12+
from _pytest.skipping import MarkEvaluator
1213

1314

1415
def pytest_pycollect_makeitem(collector, name, obj):
@@ -113,6 +114,8 @@ def addSkip(self, testcase, reason):
113114
try:
114115
pytest.skip(reason)
115116
except pytest.skip.Exception:
117+
self._evalskip = MarkEvaluator(self, 'SkipTest')
118+
self._evalskip.result = True
116119
self._addexcinfo(sys.exc_info())
117120

118121
def addExpectedFailure(self, testcase, rawexcinfo, reason=""):

testing/test_skipping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,15 @@ def test_func():
396396

397397

398398
def test_skipif_reporting(self, testdir):
399-
p = testdir.makepyfile("""
399+
p = testdir.makepyfile(test_foo="""
400400
import pytest
401401
@pytest.mark.skipif("hasattr(sys, 'platform')")
402402
def test_that():
403403
assert 0
404404
""")
405405
result = testdir.runpytest(p, '-s', '-rs')
406406
result.stdout.fnmatch_lines([
407-
"*SKIP*1*platform*",
407+
"*SKIP*1*test_foo.py*platform*",
408408
"*1 skipped*"
409409
])
410410
assert result.ret == 0

testing/test_unittest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,17 @@ def test_func(self):
700700
reprec = testdir.inline_run()
701701
reprec.assertoutcome(failed=1)
702702

703+
@pytest.mark.skipif("sys.version_info < (2,7)")
704+
def test_unittest_raise_skip_issue748(testdir):
705+
testdir.makepyfile(test_foo="""
706+
import unittest
703707
708+
class MyTestCase(unittest.TestCase):
709+
def test_one(self):
710+
raise unittest.SkipTest('skipping due to reasons')
711+
""")
712+
result = testdir.runpytest("-v", '-rs')
713+
result.stdout.fnmatch_lines("""
714+
*SKIP*[1]*test_foo.py*skipping due to reasons*
715+
*1 skipped*
716+
""")

0 commit comments

Comments
 (0)