python - 装饰器适用于功能但不适用于类

标签 python python-2.7 python-unittest python-decorators

需要一些帮助来纠正单元测试的装饰器。我不确定如何满足单元测试的要求。 这个装饰器的想法是在传入 True 时将测试标记为 expectedFailure。否则让测试简单地运行。这个装饰器适用于测试函数,但不适用于类定义 p>

import unittest

def expectedFailureIf(expFailure):
    if expFailure: 
        return unittest.expectedFailure
    return lambda func: func

@expectedFailureIf(GetCurrentOS() == kPlatMac)  # Fails on Class
class someClass(unittest.TestCase):
    #@expectedFailureIf(GetCurrentOS() == kPlatMac) # Works on Function
    def test_sometestA(self):
        assert True

    def test_sometestB(self):
        assert False

我得到的错误是 test_sometest() 正好需要 1 个参数。删除装饰器允许测试运行。将装饰器移动到函数顶部允许测试运行。

历史...我的一个平台运行良好,而另一个平台运行不正常。我想让一个平台运行所有测试,而另一个平台将被标记为预期失败。当然我不想使用 skip 或 skip if 。因为那将不允许有效平台运行。将它们标记为预期的失败也将不起作用,因为一个平台将返回意外的成功。在 expectedFailureIf() 到位的情况下,每个平台都将正确报告,一旦问题得到解决,这些测试将报告为意外成功。事情解决后会通知我。对我来说……这似乎是一个更好的结果。

最佳答案

在 Python 3 上,您的代码运行良好。 implementation of unittest.expectedFailure 在那里更好,并且在类或函数上装饰时它可以正常工作。

在 Python 2 上,unittest.expectedFailure is only designed to work on functions

这是一个适用于 Python 2 的替代品。

import inspect
import types
import unittest

def expectedFailureIf(condition):
    if callable(condition):
        condition = condition()
    if not condition:
        # return identity function for no-op
        return lambda x: x
    def patch(func_or_class):
        if isinstance(func_or_class, types.FunctionType):
            return unittest.expectedFailure(func_or_class)
        for name, member in inspect.getmembers(func_or_class):
            if name.startswith('test') and isinstance(member, types.MethodType):
                setattr(func_or_class, name, unittest.expectedFailure(member))
        return func_or_class
    return patch

@expectedFailureIf(True)
class MyTest(unittest.TestCase):

    def test_that_passes(self):
        assert 2 + 2 == 4

    def test_that_fails(self):
        assert 2 + 2 == 5

if __name__ == "__main__":
    unittest.main(verbosity=2)

结果:

test_that_fails (__main__.MyTest) ... expected failure
test_that_passes (__main__.MyTest) ... unexpected success

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK (expected failures=1, unexpected successes=1)

警告!

事实上,意外的成功并没有使测试运行失败,这是 Python 中的一个错误!它在 2014 年 1 月 (Python 3.4) 中得到解决,但由于向后兼容性问题,此错误修复未合并到 2.7 的分支中(请参阅 issue20165 上的评论)。所以,不幸的是,它现在是 Python 2 "feature"

如果这对您来说是一个交易破坏者,请考虑升级到更多 recent Python version 和/或使用 better test runner

关于python - 装饰器适用于功能但不适用于类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51068281/

相关文章:

python - 根据条件将多列上的值替换为 nan

python - EAST文本检测-215:断言失败(OpenCV Python)

python - 适用于 python 2.7.3 的 pygame

python - 如何使用 Python 的单元测试正确比较两个 SQLAlchemy Column 对象?

mocking - 单元测试模拟函数输出

python - django 上奇怪的 UnicodeDecodeError

python - Pandas:使用分位数过滤组内后 10% 和前 25% 数据的最佳方法

python - 如何调用另一个 lambda 异步并将上下文传递给它?

python - 如何在 Django 模板中获取字典键和值?

Python 模拟 : Patching Python Pika's "basic_publish" function