简体   繁体   English

使用 pytest 和 pyfakefs 测试 isinstance(PosixPath)

[英]Testing isinstance(PosixPath) using pytest with pyfakefs

In my code, I test if config_location is an instance of PosixPath or None :在我的代码中,我测试 config_location 是config_location还是NonePosixPath

if isinstance(self.config_location, (PosixPath, type(None))):
    ...

This works fine when run with the normal python interpreter, but fails during my pytest tests using pyfakefs, as the pyfakefs object is of type pyfakefs.fake_pathlib.FakePathlibModule.PosixPath .这在使用普通的 python 解释器运行时工作正常,但在我使用 pyfakefs 的 pytest 测试期间失败,因为 pyfakefs 对象的类型为pyfakefs.fake_pathlib.FakePathlibModule.PosixPath The obvious solution here would be to import pyfakefs into the tested code and add pyfakefs.fake_pathlib.FakePathlibModule.PosixPath to the isinstance tuple, but this approach seems very clunky to me.这里明显的解决方案是将 pyfakefs 导入到测试代码中,并将pyfakefs.fake_pathlib.FakePathlibModule.PosixPath添加到 isinstance 元组,但这种方法对我来说似乎很笨拙。

There has to be a better way ?一定有更好的方法 ?

测试PurePath反而有效。

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

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