2

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.

2 Answers2

2

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.

wiseboar
  • 175
  • 2
  • 13
1

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This sounds like a good approach but I am new to the idea of a git sparse-checkout. I've started digging through those other links and reading the git-sparse-checkout documentation. Are you saying I should put sparse checkout in a branch, say `submodule-release`, of the _submodule_ repository and then have the parent repository refer to that branch? – Francis Sziebert Mar 26 '21 at 15:37
  • @FrancisSziebert I don't think a sparse checkout directive is stored in the repository (to be included in a future clone). It is declared in the local repository configuration, as shown in https://stackoverflow.com/a/53233492/6309 – VonC Mar 26 '21 at 15:40
  • After a little more reading, that makes sense. My edit example works for my local development. For a little sanity check, If I was running pytest on the parent as part of a pipeline, would I just include these sparse-checkout commands in my makefile prior to testing? – Francis Sziebert Mar 26 '21 at 15:56
  • @FrancisSziebert Thank you for the edit. Approved. – VonC Mar 26 '21 at 15:57
  • @FrancisSziebert Yes, the idea would be to integrate those commands to your pipeline. – VonC Mar 26 '21 at 16:27