簡體   English   中英

在父項目上運行 pytest 時,如何忽略 git 子模塊(例如 submodule/conftest.py)根目錄下的 conftest.py 文件?

[英]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?

問題

我如何告訴 pytest 忽略存儲庫的 git 子模塊中的所有測試文件,包括 conftest.py ,因為這些測試和文件與父存儲庫的測試套件無關?

背景

我的項目中有許多 git 子模塊,它們包含自己的獨立測試配置。

當我嘗試在“父”存儲庫中使用 pytest 時,我收到此錯誤,因為 pytest 正在我的子模塊中收集 conftest.py 文件。

>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 ==============================================

我的“父”git 存儲庫結構如下所示:

./.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/

一個(繁瑣的)選項可能是僅使用未初始化的子模塊運行 pytest。 但是,如果我不初始化 git 子模塊,那么我無法使用這些庫運行集成測試。

您可以使用 sparse-checkout 規則(帶有排除規則)設置您的子模塊,以便不加載自己的conftest.py

如果該文件在子模塊中不存在,它將被pytest自動“忽略”。

例如,運行:

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

我通過在我的項目的根目錄中創建一個pytest.ini文件來解決這個問題,明確指定測試路徑:

# pytest.ini
[pytest]
testpaths =
    tests

這樣子模塊路徑就會被忽略。 弄亂從子模塊中簽出的文件聽起來很痛苦。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM