相关疑难解决方法(0)

断言没有使用Mock调用函数/方法

我正在使用Mock库来测试我的应用程序,但我想断言某些函数没有被调用.模拟文档讨论像mock.assert_called_with和的方法mock.assert_called_once_with,但我没有发现任何类似mock.assert_not_called或与确认模拟相关的东西没有调用.

我可以使用类似下面的东西,虽然它看起来不酷也不像pythonic:

def test_something:
    # some actions
    with patch('something') as my_var:
        try:
            # args are not important. func should never be called in this test
            my_var.assert_called_with(some, args)
        except AssertionError:
            pass  # this error being raised means it's ok
    # other stuff
Run Code Online (Sandbox Code Playgroud)

任何想法如何实现这一目标?

谢谢你的帮助 :)

python unit-testing mocking python-mock

117
推荐指数
5
解决办法
7万
查看次数

标签 统计

mocking ×1

python ×1

python-mock ×1

unit-testing ×1