【发布时间】:2021-07-18 22:23:52
【问题描述】:
我有以下方法:
class Controls:
def check_os(self) -> None:
if os.name != "posix":
raise OSError
我正在尝试这样测试:
import pytest
@pytest.fixture
def name_mock(mocker):
return mocker.patch("path_to_module.controls.os.name", return_value="posix")
def test_check_os_fail(name_mock):
controls = Controls()
controls.check_os()
但随后出现以下错误:
platform win32 -- Python 3.9.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
plugins: cov-2.11.1, mock-3.5.1
collected 57 items / 56 deselected / 1 selected
tests\test_controls.py
INTERNALERROR> Traceback (most recent call last):
...
NotImplementedError: cannot instantiate 'PosixPath' on your system
到底发生了什么?
【问题讨论】:
标签: python unit-testing mocking pytest