【问题标题】:Python unit test expectedFailureIfPython unittest 预期失败如果
【发布时间】:2018-06-08 18:07:09
【问题描述】:

我可能是盲人并且在 Python 单元测试框架 (Python 2.7.10) 中遗漏了一些东西。我试图将一个类标记为预期的失败,但前提是该类在 Windows 上运行。其他平台正常工作。所以基本概念是:

@unittest.expectedFailureIf(sys.platform.startswith("win"), "Windows Fails")
class MyTestCase(unittest.TestCase):
    # some class here

【问题讨论】:

  • 哪个版本的python?
  • Python 版本 2.7.10
  • Python 2.7 没有@unittest.expectedFailureIf()。也许你可以使用@unittest.skipIf(sys.platform.startswith("win"), "Windows Fails")
  • 这个请求描述了一个概念。两个平台都需要运行。如果我将它们标记为预期失败,它们将在 Mac 平台上返回意外成功。这当然是错的。他们应该在 Macintosh 上报告成功或失败,而在 Windows 上(由于错误)他们会报告失败,但使用 expectedFailureIf() 的概念,他们将在 Windows 上静音并报告为 expectedFailures。他们将在修复之日报告意外修复。一种更好的方法,因为一个平台将继续报告有效结果。跳过是不可接受的。有办法吗?

标签: python python-2.7 unit-testing


【解决方案1】:

如前所述,Python 2 和 Python 3(3.8 版)都没有内置此功能。

但是,您可以很容易地自己创建它,方法是在文件顶部定义它:

def expectedFailureIf(condition):
    """The test is marked as an expectedFailure if the condition is satisfied."""
    def wrapper(func):
        if condition:
            return unittest.expectedFailure(func)
        else:
            return func
    return wrapper

那么您基本上可以按照您的建议进行操作(我没有添加原因,因为这不在现有的预期失败中):

class MyTestCase(unittest.TestCase):
    # some class here

    @expectedFailureIf(sys.platform.startswith("win"))
    def test_known_to_fail_on_windows_only(self):

【讨论】:

    【解决方案2】:

    来自文档**https://docs.python.org/2/library/unittest.html#skipping-tests-and-expected-failures

    没有expectedFailureIf(),可以使用expectedFailure()skipIf(sys.platform.startswith("win", "Windows Fails"))

    【讨论】:

    • 当然...我正在寻找不涉及 skipIf() 的解决方法,因为我仍然希望 Macintosh 端返回有效的测试结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多