Another big aspect of local tests is testing the __init__.py files.
__init__.py file is responsible of loading up necessary modules and classes at appropriate stages. For examples if your source code is in a folder you don’t want to force user to do repetitive import tasks. And also, you don’t want to oblige user to import each class every time they import a library or module.
In that case we would have to do something like this every time we import a library:
- import folder
- import library
- import class a
- import class b
So, developers usually strive to eliminate unnecessary steps from the importing process and achieve something like this:
- import folder
- import library
- import class a
- import class b
and hopefully everything relevant gets loaded automatically. This depends on a proper structuring of __init__.py file. Don’t worry in practice it’s a couple of lines of code and not so difficult but it’s just important to get it right and sometimes it can be a bit complicated to do so. Explanations and examples below should help you get it right.
It makes perfect sense to test __init__.py file and see if its functioning as it should via a local installation. Other files such as Readme.md or setup.py will cause errors during installation or have visible flaws when something’s wrong but it’s possible to have a malfunctioning program due to __init__.py file despite a successfully executed installation. That’s why it’s wise to test the program locally in this sense as well.
Since this file’s behavior can be observed during and after the import it’s quite useful to take advantage of local tests to see if __init__.py is doing what it’s intended to be doing.
__init__.py file is used to load up modules, libraries and classes when a library is imported.
For instance when your library exists inside a folder, if you don’t include a proper __init__.py file, when your library is imported it will only import the folder name. Then the users will have to follow a repetitive calling structure such as
library.library.class.function
First library in this example is only the folder while the second library is the actual Python module.
You can also include multiple __init__.py files. It’s common practice to include a __init__.py file in the root folder to ensure proper importing of the main library and then several __init__.py files as needed to where modules exist. In the same folder with modules you will want to make sure classes of these modules are initialized inside the __init__.py files.