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

When I use a patch wrapper in test, PTO19 fires when the patched items passed into the argument are unused #202

Open
jplhanna opened this issue Nov 17, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@jplhanna
Copy link

Bug report

What's wrong

I use the unit test patch as a wrapper around test cases and test functions, which requires the patched items to be passed into the test as the first X arguments.
The name of the arguments doesn't matter, they are just passed in reverse order of the patches.

When running flake8 against a test file with such a use case and the argument is not used, PT019 will fire saying the fixture without value should be placed in pytest.mark.usefixtures.

Example:

@patch.object(Config, "get_first", return_value=None)
@patch("src.app.config.controllers.db")
class TestCreateConfigFromExisting:
    def test_new_config_has_given_unit(self, _mock_db, _get_config):
        # Arrange
        mock_unit = MagicMock()
        existing_config = Config(categories=[])

        # Act
        config = create_item_from_existing(mock_unit, existing_config)

        # Assert
        assert config.unit == mock_unit

_mock_db and _get_config will fire PT019

How it should work

I would expect this case to be ignored, or suggest placing _ in front of the argument if the patched item is not used in the test.

System information

  • Operating system: MacOS Monterey
  • Python version: 3.8
  • flake8 version: 4.0.1
  • flake8-pytest-style version: 1.6.0
@jplhanna jplhanna added the bug Something isn't working label Nov 17, 2022
@m-burst
Copy link
Owner

m-burst commented Nov 18, 2022

Hi @jplhanna,
Thanks for the report!
I have not considered this case when writing the rule.

I am not sure when I can find the time to look into a proper fix.
However, I might suggest a workaround using the mocker fixture from the pytest-mock library, so your test would look like this:

@pytest.fixture()
def _mock_get_config(mocker):
    mocker.patch.object(Config, "get_first", return_value=None)

@pytest.fixture()
def _mock_db(mocker):
    mocker.patch("src.app.config.controllers.db")

@pytest.mark.usefixtures("_mock_get_config", "_mock_db")
class TestCreateConfigFromExisting:
    def test_new_config_has_given_unit(self):
        # Arrange
        mock_unit = MagicMock()
        existing_config = Config(categories=[])

        # Act
        config = create_item_from_existing(mock_unit, existing_config)

        # Assert
        assert config.unit == mock_unit

@jplhanna
Copy link
Author

Thanks for the suggestion! It might be tough to use for the current project but I'll keep it in mind for future ones.
I figured this wouldn't be an easy/quick fix. Just something worth noting.

@funkyfuture
Copy link

when the name _in (as complement to out) is used with parametrized tests it also evokes false PT019. or any other name starting with _.

@m-burst
Copy link
Owner

m-burst commented Apr 7, 2023

I would suggest using in_ instead of _in, because a leading underscore conventionally implies that the name is unused

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants