简体   繁体   English

Pytest 在使用 pyhamcrest raises 时失败

[英]Pytest fails when using pyhamcrest raises

Currently I have a test cases like the following:目前我有一个如下的测试用例:

    def test_foo(self):
        assert_that(self.target.do(), raises(FileNotFoundError))

Which passes using the standard python unittest framework, however if I change to use pytest, it fails.使用标准 python 单元测试框架通过,但是如果我更改为使用 pytest,它会失败。 Switching to the pytest syntax works (as below), but why doesn't the pyhamcrest matcher work in this case?切换到 pytest 语法有效(如下所示),但为什么 pyhamcrest 匹配器在这种情况下不起作用?

    def test_foo(self):
        with pytest.raises(FileNotFoundError):
             self.target.do()

PyHamcrest's raises() matcher requires you to make the call with the calling() function - see the tutorial . PyHamcrest 的raises()匹配器要求您使用calling() function 进行调用 - 请参阅教程 So, in your case you'd want:所以,在你的情况下你会想要:

assert_that(calling(self.target.do), raises(FileNotFoundError))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM