简体   繁体   中英

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. Switching to the pytest syntax works (as below), but why doesn't the pyhamcrest matcher work in this case?

    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 . So, in your case you'd want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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