`py.test` and `__init__.py` files

前端 未结 3 1459
栀梦
栀梦 2020-12-20 20:13

I thought py.test is \"standalone\" in a sense that it treats test_*.py files \"as it is\", and only imports modules specified in these files, with

相关标签:
3条回答
  • 2020-12-20 20:30

    You can tell pytest to ignore specific files or glob patterns as described here. Put a conftest.py file in the root directory of your project that lists the files you want pytest to ignore:

    However, many projects will have a setup.py which they don’t want to be imported. Moreover, there may files only importable by a specific python version. For such cases you can dynamically define files to be ignored by listing them in a conftest.py file:

     # content of conftest.py
     import sys
    
     collect_ignore = ["setup.py"]
    
    0 讨论(0)
  • 2020-12-20 20:31

    Looks like py.test is using py._path.pyimport to open your file. If there is a __init__.py file in the directory, it treats your file as a module, otherwise it opens the file. Long story short, delete the __init__.py or put your tests in another directory outside your project code (<--- good idea).

    http://doc.pylib.org/en/latest/_modules/py/_path/local.html#LocalPath.pyimport

    0 讨论(0)
  • 2020-12-20 20:40

    I really suggest you to rename the directory to something not called "distutils". Why ? Because you are overriding an existing module. When "import distutils" or "from distutils import *" appear in the script (from another import or your own python file), it will prefer your directory instead the system one. If the module distutils have been already loaded before, your distutils will not be loaded, because the symbol already exists in global().

    It would be really simpler to rename that directory (like tests) instead of trying to fight with py.text / python internals.

    0 讨论(0)
提交回复
热议问题