Skip to content

python/mock-import

Rule mock-import
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

A test with a mock checks the mock and not the real code. Run the real code on real input.

from unittest.mock import patch
def test_total():
with patch("orders.load", return_value=[{"price": 2.0}]):
assert total() == 2.0
def test_total(tmp_path):
path = tmp_path / "orders.csv"
path.write_text("price\n2.0\n", encoding="utf-8")
assert total(path) == 2.0

The rule reports an import of unittest.mock, mock or pytest_mock.

xpd has no automatic fix, so make the change by hand.

A test that replaces a function with a mock passes when the mock is called the way the test expects, whatever the real function does. Run the real code on real input.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This imports a mock library. A test with a mock checks the mock, so run the real code on real input.

Browse the other rules of the Python pack.