由于 pytest 中的 Mocking 导致覆盖 INTERNALERROR

时间:2021-04-04 19:30:17

标签: python python-3.x unit-testing pytest code-coverage

我正在尝试测试以下功能:

def my_testable_function(input_filename):
        if os.path.isfile(input_filename):
            return "Exist!"
        return "Not exist"

这是我第一次尝试测试上述功能:

def test_1():
    os.path.isfile = Mock(return_value=True)
    result = my_testable_function("/existing/path/file.txt")
    assert result == "Exist!"

使用 pytest==6.2.1 + pytest-cov==2.11.1 运行:

$ py.test -v --ff --cov=<my_package> tests

我最终遇到了内部错误:

====================================================================== test session starts =======================================================================
platform darwin -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- /Users/my-user/my-project/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/my-user/my-project
plugins: cov-2.11.1, aiohttp-0.3.0
collected 6 items
run-last-failure: no previously failed tests, not deselecting items.

tests/test_app.py::test_1 PASSED                                                             [100%]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/_pytest/main.py", line 323, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pluggy/callers.py", line 203, in _multicall
INTERNALERROR>     gen.send(outcome)
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pytest_cov/plugin.py", line 271, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pytest_cov/engine.py", line 44, in ensure_topdir_wrapper
INTERNALERROR>     return meth(self, *args, **kwargs)
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/pytest_cov/engine.py", line 230, in finish
INTERNALERROR>     self.cov.stop()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/control.py", line 701, in combine
INTERNALERROR>     combine_parallel_data(
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/data.py", line 110, in combine_parallel_data
INTERNALERROR>     new_data.read()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 753, in read
INTERNALERROR>     with self._connect():       # TODO: doesn't look right
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 298, in _connect
INTERNALERROR>     self._open_db()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 266, in _open_db
INTERNALERROR>     self._read_db()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 270, in _read_db
INTERNALERROR>     with self._dbs[get_thread_id()] as db:
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 1037, in __enter__
INTERNALERROR>     self._connect()
INTERNALERROR>   File "/Users/my-user/my-project/venv/lib/python3.8/site-packages/coverage/sqldata.py", line 1019, in _connect
INTERNALERROR>     self.con = sqlite3.connect(filename, check_same_thread=False)
INTERNALERROR> sqlite3.OperationalError: unable to open database file

尝试使用 monkeypatch 进行操作(没有 INTERNALERROR):

def mock_is_file(filename):
    if filename == "/existing/path/file.txt":
        return True
    return os.path.isfile(filename)


def test_2(monkeypatch):
    monkeypatch.setattr(os.path, 'isfile', mock_is_file)
    my_testable_function("/existing/path/file.txt")
    assert result == "Exist!"

我的问题是,为什么它在我的第一次尝试中发生,为什么它在第二次尝试中起作用?

1 个答案:

答案 0 :(得分:1)

当您放置模拟时,您还必须确保在测试结束时撤消模拟。您在测试中更改了 os.path.isfile ,然后将其更改。后来,coverage 需要那个函数,取而代之的是你的模拟。

monkeypatch 方法有效,因为在测试完成后,monkeypatch 夹具会自动清理模拟。