小编Ale*_*mov的帖子

使用 pytest-mock 检查调用顺序

在下面显示的代码片段中,我想测试函数中的函数调用顺序run(),即,在调用之后f_3调用:f_2f_1

class TestMock:

    def f_1(self) -> None:
        pass

    def f_2(self) -> None:
        pass

    def f_3(self) -> None:
        pass

    def run(self) -> None:
        self.f_1()
        self.f_2()
        self.f_3()
Run Code Online (Sandbox Code Playgroud)

有什么方法可以使用 来做到这一点吗pytest-mock?我尝试在测试文件中模拟f_1f_2和函数并使用with ,但是没有成功。f_3assert_has_callsany_order=False

预先感谢您的任何帮助或提示!

最好的,阿列克谢

python mocking pytest pytest-mock

5
推荐指数
1
解决办法
2178
查看次数

将 pytest 作为预提交钩子运行 // 没有这样的文件或目录问题

在我的 Python 项目中,我pytest用作pre-commit挂钩。一些测试创建和删除临时文件,当我运行pytest <test directory>. 但是,当我运行git commitpre-commit挂钩 triggers 时pytest,某些测试由于FileNotFoundError: [Errno 2] No such file or directory: '<file name>'. 我的印象是,当许多文件已更改且位于暂存区域时,会发生这种情况(对于 1-2 个文件,我没有观察到此问题)。这是我的pytest部分.pre-commit-config.yaml

  - repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        entry: bash -c 'pytest'
        language: system
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

pytest-check.............................................................Failed
- hook id: pytest-check
- exit code: 1

tests/utils/test_application.py F                                        [ 91%]
tests/utils/test_image_io.py .FFF.........                               [100%]

==================================== ERRORS ====================================
_ ERROR at teardown of test_calling_with_nonexisting_parameter[--non_existing …
Run Code Online (Sandbox Code Playgroud)

python pre-commit pre-commit-hook pytest pre-commit.com

3
推荐指数
2
解决办法
4014
查看次数