【问题标题】:Testing isinstance(PosixPath) using pytest with pyfakefs使用 pytest 和 pyfakefs 测试 isinstance(PosixPath)
【发布时间】:2022-07-01 05:23:01
【问题描述】:

在我的代码中,我测试config_location 是否是PosixPathNone 的实例:

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

这在使用普通 python 解释器运行时工作正常,但在我使用 pyfakefs 的 pytest 测试期间失败,因为 pyfakefs 对象的类型为 pyfakefs.fake_pathlib.FakePathlibModule.PosixPath。这里明显的解决方案是将 pyfakefs 导入到测试代码中,并将pyfakefs.fake_pathlib.FakePathlibModule.PosixPath 添加到 isinstance 元组中,但这种方法对我来说似乎很笨拙。

一定有更好的方法吗?

【问题讨论】:

  • 检查 pyfakefs 的限制:jmcgeheeiv.github.io/pyfakefs/release/…
  • 这取决于你的目标。如果您只想检查对象是否属于路径类型,则可以改为检查 PurePath - 两个 PosixPath 实现都源自于此。一般来说,使用isinstance 不被认为是好的做法,但我不知道在你的情况下是否有更好的方法。

标签: python python-3.x pyfakefs


【解决方案1】:

测试 PurePath 反而有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多