簡體   English   中英

可導入pytest插件結構

[英]Structure of importable pytest plugin

我有一些在多個項目中通用的數據/固定裝置。 所以明智的做法是將它們重構為一個獨立的項目並將它們導入項目中。 我要重構的項目結構如下所示:

my-project/
    src/
        db/
            __init__.py
            module1.py
    tests/
        data/
            test_data.csv
        test_module1.py

test_module1.py包含要重構的裝置和module1.py的單元測試。 共享固定裝置的官方方式是通過pytest-plugins 官方文檔提供了以下pyproject.toml示例:

# sample ./pyproject.toml file
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "myproject"
classifiers = [
    "Framework :: Pytest",
]

[tool.setuptools]
packages = ["myproject"]

[project.entry_points]
pytest11 = [
    "myproject = myproject.pluginmodule",
]

我的問題是:

  1. 我是否將tests/中的夾具和數據重構到new-fixture-project/src/test_data/ (就像常規包一樣)? 或者new-fixture-project/tests/
  2. 無論哪種情況,切入點應該是什么? new-fixture-project = test_data.data (假設我已經將 fixtures 重構為test_data/data.py

[編輯] 回答我在評論中的問題,這是將其構建為常規 package 所需要的。

pyproject.toml

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

setup.cfg中,添加此部分

[options.entry_points]
pytest11 =
    test_data = test_data.plugin

我創建了一個開源 python 項目,您可以查看以供參考: pytest-mock-generator

該項目的結構不是src目錄,而是如下所示:

new-fixture-project/
    new_fixture_project/
        db/
            __init__.py
            module1.py
    tests/
        data/
            test_data.csv
        test_module1.py

所以對於你的問題 - 燈具和代碼 go 到new-fixture-project/new_fixture_project/

入口點是new_fixture_project ,這是模塊名稱(而不是 src),如果您為簡單起見在根__init__.py中定義您的燈具。 例如,您還可以在module1.py中定義您的燈具,然后入口點將是new_fixture_project.module1

我的項目中很少有有用的鏈接:

暫無
暫無

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

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