Skip to content

Python · testing

unused-mock-setup

python:unused-mock-setup

Tests should remove mock configuration that cannot affect execution.

Code
SARJ067
Default
error
Fix
none
Languages
python

Why

Overwritten or contradicted mock setup adds misleading, unreachable test behavior.

Fix

Delete the unused setup or exercise the mock before replacing or contradicting it.

Before / after

Executed by this rule’s unit tests.

Before

Mock return value is overwritten before use

tests/test_billing.py · focus
def test_charge():
    gateway.charge.return_value = 1
    gateway.charge.return_value = 2
    assert billing.charge(gateway) == 2

After

Test uses each configured value

tests/test_billing.py · focus
def test_charge():
    gateway.charge.return_value = 1
    assert billing.charge(gateway) == 1
    gateway.charge.return_value = 2
    assert billing.charge(gateway) == 2

Limits

  • Only test paths are analyzed.
  • Potentially effectful statements between assignments prevent a finding.