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.0def test_total(tmp_path): path = tmp_path / "orders.csv" path.write_text("price\n2.0\n", encoding="utf-8") assert total(path) == 2.0What the rule reports
Section titled “What the rule reports”The rule reports an import of unittest.mock, mock or pytest_mock.
How to fix it
Section titled “How to fix it”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.
Message
Section titled “Message”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.