Python · testing
prefer-library-fake
python:prefer-library-fake Tests should use maintained service fakes or emulators instead of hand-rolled third-party doubles.
- Code
- SARJ059
- Default
- error
- Fix
- none
- Languages
- python
Why
Hand-written doubles model only remembered protocol behavior and can let invalid requests pass.
Fix
Use the recognized library fake, emulator, or test container while keeping the production client.
Before / after
Executed by this rule’s unit tests.
Before
Test defines its own S3 client
class FakeS3Client:
def put_object(self, **kwargs):
return kwargs
def get_object(self, **kwargs):
return kwargs
def delete_object(self, **kwargs):
return kwargs
After
Test uses the maintained AWS fake
from moto import mock_aws
@mock_aws
def test_upload():
assert upload_with_real_client()
Limits
- Only test and shared-double paths are analyzed.
- Only recognized external services and substantial hand-rolled doubles are reported.