From 9d4e0365da1c7f76a6d225b40819003ed5ca2328 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 18 Jun 2015 21:59:44 -0300 Subject: [PATCH 1/2] Skipif marker report now refers to the skipped function Fix #114 --- CHANGELOG | 3 +++ _pytest/skipping.py | 9 +++++++++ testing/test_skipping.py | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 74fb004f6..2a3595dd8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -10,6 +10,9 @@ - fix issue735: assertion failures on debug versions of Python 3.4+ Thanks Benjamin Peterson. +- fix issue114: skipif marker reports to internal skipping plugin; + Thanks Floris Bruynooghe for reporting and Bruno Oliveira for the PR. + 2.7.1 (compared to 2.7.0) ----------------------------- diff --git a/_pytest/skipping.py b/_pytest/skipping.py index f95edf8bd..5f31166aa 100644 --- a/_pytest/skipping.py +++ b/_pytest/skipping.py @@ -137,6 +137,7 @@ class MarkEvaluator: def pytest_runtest_setup(item): evalskip = MarkEvaluator(item, 'skipif') if evalskip.istrue(): + item._evalskip = evalskip pytest.skip(evalskip.getexplanation()) item._evalxfail = MarkEvaluator(item, 'xfail') check_xfail_no_run(item) @@ -156,6 +157,7 @@ def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() evalxfail = getattr(item, '_evalxfail', None) + evalskip = getattr(item, '_evalskip', None) # unitttest special case, see setting of _unexpectedsuccess if hasattr(item, '_unexpectedsuccess') and rep.when == "call": # we need to translate into how pytest encodes xpass @@ -177,6 +179,13 @@ def pytest_runtest_makereport(item, call): elif call.when == "call": rep.outcome = "failed" # xpass outcome rep.wasxfail = evalxfail.getexplanation() + elif evalskip is not None and rep.skipped and type(rep.longrepr) is tuple: + # skipped by mark.skipif; change the location of the failure + # to point to the item definition, otherwise it will display + # the location of where the skip exception was raised within pytest + filename, line, reason = rep.longrepr + filename, line = item.location[:2] + rep.longrepr = filename, line, reason # called by terminalreporter progress reporting def pytest_report_teststatus(report): diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 1425b88ef..00f6833e6 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -396,7 +396,7 @@ class TestSkipif: def test_skipif_reporting(self, testdir): - p = testdir.makepyfile(""" + p = testdir.makepyfile(test_foo=""" import pytest @pytest.mark.skipif("hasattr(sys, 'platform')") def test_that(): @@ -404,7 +404,7 @@ class TestSkipif: """) result = testdir.runpytest(p, '-s', '-rs') result.stdout.fnmatch_lines([ - "*SKIP*1*platform*", + "*SKIP*1*test_foo.py*platform*", "*1 skipped*" ]) assert result.ret == 0 From f90b2f845c7214a3243f438936a33152f0a29c3d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 18 Jun 2015 22:26:35 -0300 Subject: [PATCH 2/2] unittest.SkipTest now reports original function location Fix #748 --- CHANGELOG | 3 +++ _pytest/unittest.py | 3 +++ testing/test_unittest.py | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 2a3595dd8..052454d2f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,9 @@ - fix issue114: skipif marker reports to internal skipping plugin; Thanks Floris Bruynooghe for reporting and Bruno Oliveira for the PR. +- fix issue748: unittest.SkipTest reports to internal pytest unittest plugin. + Thanks Thomas De Schampheleire for reporting and Bruno Oliveira for the PR. + 2.7.1 (compared to 2.7.0) ----------------------------- diff --git a/_pytest/unittest.py b/_pytest/unittest.py index c035bdd1a..0666fba87 100644 --- a/_pytest/unittest.py +++ b/_pytest/unittest.py @@ -9,6 +9,7 @@ import py # for transfering markers from _pytest.python import transfer_markers +from _pytest.skipping import MarkEvaluator def pytest_pycollect_makeitem(collector, name, obj): @@ -113,6 +114,8 @@ class TestCaseFunction(pytest.Function): try: pytest.skip(reason) except pytest.skip.Exception: + self._evalskip = MarkEvaluator(self, 'SkipTest') + self._evalskip.result = True self._addexcinfo(sys.exc_info()) def addExpectedFailure(self, testcase, rawexcinfo, reason=""): diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 9c342976b..1ab016a5a 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -700,4 +700,17 @@ def test_issue333_result_clearing(testdir): reprec = testdir.inline_run() reprec.assertoutcome(failed=1) +@pytest.mark.skipif("sys.version_info < (2,7)") +def test_unittest_raise_skip_issue748(testdir): + testdir.makepyfile(test_foo=""" + import unittest + class MyTestCase(unittest.TestCase): + def test_one(self): + raise unittest.SkipTest('skipping due to reasons') + """) + result = testdir.runpytest("-v", '-rs') + result.stdout.fnmatch_lines(""" + *SKIP*[1]*test_foo.py*skipping due to reasons* + *1 skipped* + """)