简体   繁体   中英

How do I ignore a conftest.py file at the root of a git submodule (e.g. submodule/conftest.py) when running pytest on the parent project?

Question

How can I tell pytest to ignore all test files including conftest.py within a repository's git submodule as these tests and files are irrelevant to the parent repository's test suite?

Background

I have a number of git submodules in my project which house their own self contained testing configurations.

When I try to use pytest in the "parent" repository, I am getting this error because pytest is collecting the conftest.py files within my submodules.

>pytest
=================================================== ERRORS =================================================== 
_______________________________________ ERROR collecting test session ________________________________________ 
Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:
It affects the entire test suite instead of just below the conftest as expected.
  C:\Users\user\git\project\submodule\conftest.py
Please move it to a top level conftest file at the rootdir:
  C:\Users\user\git\project
For more information, visit:
  https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files
========================================== short test summary info =========================================== ERROR
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ============================================== 1 error in 0.45s ==============================================

My "parent" git repository structure looks like:

./.git
./project1/__main__.py
./project1/__init__.py
./project1/other_stuff/
./test/
./conftest.py
./setup.cfg
./submodule/.git
./submodule/project2/__main__.py
./submodule/project2/__init__.py
./submodule/project2/other_stuff/
./submodule/conftest.py
./submodule/setup.cfg
./submodule/test/

One (cumbersome) option may be to only run pytest with uninitialized submodules. However, if I do not initialize the git submodules then I cannot run integration tests with those libraries.

You could setup your submodule with a sparse-checkout rule (with an exclusion rule ) in order to not load their own conftest.py

If that file is not there in the submodule, it would be "ignored" automatically by pytest .

For example, run:

> cd ./submodule
> git sparse-checkout init
> git sparse-checkout set /* !conftest.py
> git sparse-checkout list
/*
!conftest.py

I solved this by creating a pytest.ini file at the root of my project specifying the test path explicitly:

# pytest.ini
[pytest]
testpaths =
    tests

This way the submodule path gets ignored. Messing with which files get checked out of the submodule sounds like a pain tbh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM