Give every test its own RPC client mock (#37)
The seafile stub returned the same RpcClient() mock to every test, so changes one test made to it leaked into later tests. pytest 9.2.0 exposed this. monkeypatch.setattr now reads the old value from the instance __dict__, where a MagicMock does not keep its child attributes, so undoing a patch of client.rpc.get_repo_list deletes the attribute. A deleted attribute on a MagicMock stays deleted, and two later health check tests failed with AttributeError. An autouse fixture now replaces seafile.RpcClient with a new mock for each test. All 202 tests pass on pytest 9.1.1 and 9.2.0.
This commit is contained in:
1 parent
9e399df8e4
commit
22543e6f7f
1 file changed
+14
@@ -23,6 +23,20 @@ def _install_seafile_stub():
|
|||||||
_install_seafile_stub()
|
_install_seafile_stub()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def fresh_rpc_client(monkeypatch):
|
||||||
|
"""
|
||||||
|
Give every test its own RPC client mock.
|
||||||
|
|
||||||
|
A single module-level mock returns the same ``RpcClient()`` object to every
|
||||||
|
test, so changes one test makes to it leak into the next. One such leak:
|
||||||
|
when ``monkeypatch.setattr`` patches a method on that object, undoing the
|
||||||
|
patch deletes the attribute, and a deleted attribute on a ``MagicMock``
|
||||||
|
stays deleted.
|
||||||
|
"""
|
||||||
|
monkeypatch.setattr(sys.modules["seafile"], "RpcClient", mock.MagicMock(name="RpcClient"))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def no_network(monkeypatch):
|
def no_network(monkeypatch):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in new issue
Block a user