1

Is there any usage of empty __init__.py or conftest.py files in Python? Today I've had a discussion about it in my team and someone said that they may have an importance in testing with Pytest and checking code coverage.

Spartan2909
  • 101
  • 2
  • 9
  • this might help https://stackoverflow.com/questions/34466027/in-pytest-what-is-the-use-of-conftest-py-files – Gunesh Shanbhag Dec 19 '22 at 09:47
  • empty `__init__.py` are often necessary. I would not advise removing them... [read this](https://docs.python.org/3/tutorial/modules.html#packages) and [this](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) – Alexander Dec 19 '22 at 09:51
  • Empty `__init__.py` causes that a directory becomes package, it has nothing to do with `conftest` actually. – Gameplay Dec 19 '22 at 09:52

1 Answers1

0

Conftest file is used by pytest to provide fixtures : Official doc

For __init__.py, it is used to declare packages, and ca be used to search modules in your directories. It can also be used to initialise code (this is the first thing that is executed when you import a module): doc

So, both are useful.

Spartan2909
  • 101
  • 2
  • 9
Sami Tahri
  • 1,077
  • 9
  • 19
  • Hi what the reason to have empty conftest.py? – Brown Bear Dec 19 '22 at 10:09
  • If I remember correctly, pytest will add the directory containing confest.py in the sys.path. I don't know why and it may be just people struggling to understand how it works, but I know sometimes people just place it so that tests can be discovered, because it changes the behavior of pytest. – Sami Tahri Dec 19 '22 at 10:49
  • in nutshell we don't need it. What will be break in case we don't put empty conftest.py in the test directory? – Brown Bear Dec 19 '22 at 11:10
  • Your test "could" not be detected anymore. Just try it out, if it stills work, then I would suggest you to still keep it, so you know where to put configuration for your tests later ;). – Sami Tahri Dec 19 '22 at 11:19