Test discovery failure when tests in different directories are called the same

ε祈祈猫儿з 提交于 2019-11-28 18:31:26
hpk42

Putting an __init__.py is one way of resolving the conflict. Unlike nose, current pytest does not try to unload test modules in order to import test modules with the same import name. I used to think it's a bit magic to do this auto-unimporting and might mess up people's expectation from what the import mechanism does; sometimes people rely on the global state of a test module and with auto-unloading you lose it (a test module importing from another test module might then do unexpected things). But maybe it's not a practical issue and thus pytest could add a similar hack ...

famousgarkin

This is an actual feature of py.test. You can find the reason for this behavior stated in pytest.org - Good Integration Practices - Choosing a test layout / import rules:

  • avoid __init__.py files in your test directories. This way your tests can run easily against an installed version of mypkg, independently from if the installed package contains the tests or not.

As that's the recommended workflow of working with py.test: install the package under development with pip install -e, then test it.

Because of this, I myself opt for unique test names, in the convention over configuration manner. It also ensures that you don't get ambiguous test names in the various test run output.

If you need to keep the test names and don't care about the above mentioned functionality, you should be ok with putting an __init__.py.

Léo Chaz Maltrait

I had the same error, but the solution had nothing to do with the init files nor the name on the test files. I had different python versions on my macbook and on my Docker container. I launched the tests once in the bash from the macbook at the root of the project, instead of the bash of the container.

The solution was to remove the wrongly created files by running (from the bash of the container):

find -name '*.pyc' -delete
find -name __pycache__ -delete

Then launch the test again (still from the bash of the container) and everything worked just fine:

py.test

You can add the following environment variable to your bash init file or somewhere else in your development stack to avoid these folders/files being generated in the first place

export PYTHONDONTWRITEBYTECODE=1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!