flake8-pytest-style icon indicating copy to clipboard operation
flake8-pytest-style copied to clipboard

Warn about `__init__.py` in tests

Open sobolevn opened this issue 4 years ago • 6 comments

Rule request

Description

It is the best practice to remove __init__.py from tests folder. https://docs.pytest.org/en/latest/goodpractices.html#tests-outside-application-code

Rationale

So, this linter should warn users about incorrect setup. It is probably a good idea to make this configurable:

  1. tests path
  2. style: with __init__.py or without

sobolevn avatar Apr 17 '20 07:04 sobolevn

Hi @sobolevn,

I'm not sure I agree that this rule should be enforced. Per the pytest docs, layout without __init__.py files is only one of supported layouts, and an alternative layout with multiple packages and __init__.py files is also supported. I've seen both in my experience, both have pros and cons, and I don't really feel that either of them should be enforced for all projects.

m-burst avatar Apr 19 '20 21:04 m-burst

That's why I have added a note about a configuration:

It is probably a good idea to make this configurable:

tests path
style: with __init__.py or without

sobolevn avatar Apr 19 '20 23:04 sobolevn

My bad, must've missed it :( I'll keep this in mind, thanks

m-burst avatar Apr 20 '20 09:04 m-burst

Having this configurable is a good idea. While pytest emulates the presence of __init__.py, there are cases when this may break (especially when you also have to support Python 2).

I personally like not having __init__.py, but FTR here's some corner cases that I met:

  1. various tooling does not support/understand implicit namespaces or such support is implemented poorly
  2. when the custom import machinery is implemented, it may conflict with what pytest does and it has to be disabled (this is the case I saw in Ansible, in particular)

It's probably worth mentioning the presence of the exceptional cases in the doc for the rule, once implemented.

webknjaz avatar Apr 22 '20 16:04 webknjaz

  1. When you have duplicate test names

sobolevn avatar Apr 22 '20 16:04 sobolevn

unless importlib mode is used, its wrong to drop them

RonnyPfannschmidt avatar Nov 17 '23 21:11 RonnyPfannschmidt