[TIP] py.test and unittest2 expectedFailure decorator

David Cournapeau cournape at gmail.com
Mon Mar 19 12:02:46 PDT 2012


Hi there,

I'd like to be able to use expectedFailure from
unittest2/unittest at python2.7with py.test. For a test like:

import unittest

class Foo(unittest.TestCase):
    @unittest.expectedFailure
    def test_working(self):
        "A working test that is expected to fail"

I get the traceback:

self = <foo.Foo testMethod=test_working>, result = <TestCaseFunction
'test_working'>

    def run(self, result=None):
        orig_result = result
        if result is None:
            result = self.defaultTestResult()
            startTestRun = getattr(result, 'startTestRun', None)
            if startTestRun is not None:
                startTestRun()

        self._resultForDoCleanups = result
        result.startTest(self)

        testMethod = getattr(self, self._testMethodName)
        if (getattr(self.__class__, "__unittest_skip__", False) or
            getattr(testMethod, "__unittest_skip__", False)):
            # If the class or method was skipped.
            try:
                skip_why = (getattr(self.__class__,
'__unittest_skip_why__', '')
                            or getattr(testMethod, '__unittest_skip_why__',
''))
                self._addSkip(result, skip_why)
            finally:
                result.stopTest(self)
            return
        try:
            success = False
            try:
                self.setUp()
            except SkipTest as e:
                self._addSkip(result, str(e))
            except KeyboardInterrupt:
                raise
            except:
                result.addError(self, sys.exc_info())
            else:
                try:
                    testMethod()
                except KeyboardInterrupt:
                    raise
                except self.failureException:
                    result.addFailure(self, sys.exc_info())
                except _ExpectedFailure as e:
                    addExpectedFailure = getattr(result,
'addExpectedFailure', None)
                    if addExpectedFailure is not None:
                        addExpectedFailure(self, e.exc_info)
                    else:
                        warnings.warn("TestResult has no addExpectedFailure
method, reporting as passes",
                                      RuntimeWarning)
                        result.addSuccess(self)
                except _UnexpectedSuccess:
                    addUnexpectedSuccess = getattr(result,
'addUnexpectedSuccess', None)
                    if addUnexpectedSuccess is not None:
>                       addUnexpectedSuccess(self)
E                       TypeError: addUnexpectedSuccess() takes exactly 3
arguments (2 given)

Is it a but or am I using it wrong ?

cheers,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120319/e0fc466c/attachment.html>


More information about the testing-in-python mailing list