Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checks for mocking best practices #27

Merged
merged 1 commit into from
Apr 16, 2024
Merged

Added checks for mocking best practices #27

merged 1 commit into from
Apr 16, 2024

Conversation

nfx
Copy link
Collaborator

@nfx nfx commented Apr 16, 2024

mocking checker

[back to top]

R8918: explicit-dependency-required

Obscure implicit test dependency with mock.patch(XXX). Rewrite to inject dependencies through constructor.. Using patch to mock dependencies in unit tests can introduce implicit
dependencies within a class, making it unclear to other developers. Constructor arguments, on the other hand,
explicitly declare dependencies, enhancing code readability and maintainability. However, reliance on patch
for testing may lead to issues during refactoring, as updates to underlying implementations would necessitate
changes across multiple unrelated unit tests. Moreover, the use of hard-coded strings in patch can obscure
which unit tests require modification, as they lack strongly typed references. This coupling of the class
under test to concrete classes signifies a code smell, and such code is not easily portable to statically typed
languages where monkey patching isn't feasible without significant effort. In essence, extensive patching of
external clients suggests a need for refactoring, with experienced engineers recognizing the potential for
dependency inversion in such scenarios.

To address this issue, refactor the code to inject dependencies through the constructor. This approach
explicitly declares dependencies, enhancing code readability and maintainability. Moreover, it allows for
dependency inversion, enabling the use of interfaces to decouple the class under test from concrete classes.
This decoupling facilitates unit testing, as it allows for the substitution of mock objects for concrete
implementations, ensuring that the class under test behaves as expected. By following this approach, you can
create more robust and maintainable unit tests, improving the overall quality of your codebase.

Use require-explicit-dependency option to specify the package names that contain code for your project.

[back to top]

R8919: obscure-mock

Obscure implicit test dependency with MagicMock(). Rewrite with create_autospec(ConcreteType).. Using MagicMock to mock dependencies in unit tests can introduce implicit dependencies
within a class, making it unclear to other developers. create_autospec(ConcreteType) is a better alternative, as it
automatically creates a mock object with the same attributes and methods as the concrete class. This
approach ensures that the mock object behaves like the concrete class, allowing for more robust and
maintainable unit tests. Moreover, reliance on MagicMock for testing leads to issues during refactoring,
as updates to underlying implementations would necessitate changes across multiple unrelated unit tests.

[back to top]

@nfx nfx merged commit 595ad15 into main Apr 16, 2024
8 checks passed
@nfx nfx deleted the feat/mocking branch April 16, 2024 18:00
nfx added a commit that referenced this pull request Apr 16, 2024
* Added checks for mocking best practices ([#27](#27)). A new `mocking` checker has been added to the pylint plugin for Databricks to improve the quality of unit tests by enforcing best practices in mocking. This checker introduces two rules: `explicit-dependency-required` and `obscure-mock`. The `explicit-dependency-required` rule encourages injecting dependencies through constructors instead of using `mock.patch`, promoting code readability, maintainability, and dependency inversion for unit testing. The `obscure-mock` rule recommends using `create_autospec(ConcreteType)` over `MagicMock` to create mock objects that behave like concrete classes, resulting in more robust and maintainable unit tests. These rules can be configured to specify package names containing project code. This addition helps developers create reliable, maintainable, and testable code by adhering to recommended mocking practices.
@nfx nfx mentioned this pull request Apr 16, 2024
nfx added a commit that referenced this pull request Apr 16, 2024
* Added checks for mocking best practices
([#27](#27)). A
new `mocking` checker has been added to the pylint plugin for Databricks
to improve the quality of unit tests by enforcing best practices in
mocking. This checker introduces two rules:
`explicit-dependency-required` and `obscure-mock`. The
`explicit-dependency-required` rule encourages injecting dependencies
through constructors instead of using `mock.patch`, promoting code
readability, maintainability, and dependency inversion for unit testing.
The `obscure-mock` rule recommends using `create_autospec(ConcreteType)`
over `MagicMock` to create mock objects that behave like concrete
classes, resulting in more robust and maintainable unit tests. These
rules can be configured to specify package names containing project
code. This addition helps developers create reliable, maintainable, and
testable code by adhering to recommended mocking practices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant