首页 > 编程问答 >Tox 中的 Pytest - 找不到测试,`ImportError`

Tox 中的 Pytest - 找不到测试,`ImportError`

时间:2024-07-31 10:57:19浏览次数:9  
标签:python pytest python-poetry tox

我有一个具有当前结构的包:

my_package
   |- pyproject.toml
   |- poetry.lock
   |- tox.ini
   |- my_package
   |  |- __init__.py
   |  |- my_package.py
   |- tests
      |- test_my_package.py

pyproject.toml 为 pytest 配置如下:

[tool.pytest.ini_options]
addopts = [
    "--import-mode=importlib",
]
testpaths = [
    "tests",
]

tox.ini 具有以下配置,根据诗歌官方示例中的第一个用例 文档

[tox]
env_list =
    format
    py312

[testenv:format]
description = install black in a virtual environment and invoke it on the current folder
deps = black==22.3.0
skip_install = true
commands = black .

[testenv:py312]
deps =
    pytest
commands =
    pytest tests

运行 pytest tests 工作得很好,但是当我运行 tox 时,我得到 ImportError while importing test module '~\Documents\repos\my_package\tests\test_my_package.py - ModuleNotFoundError: No module named tests

我尝试使用 testpaths 配置tox和pytest(pytest在没有此配置的情况下工作得很好)。我还尝试将 __init__.py 添加到目录 tests

我在这里缺少什么?

作为旁注,我尝试了 Poetry 文档中的第二个用例 - 它可以运行测试,但它不会在与我的工作 venv 分开的虚拟环境中运行测试,并且它还会卸载我生成文档等所需的任何可选包。


问题在于,tox 在默认情况下会在名为 .tox (tox 本身的配置所在的位置)的目录中创建一个虚拟环境。因此,当运行 pytest tests 时,它找不到 tests 模块,因为它不在 tox 创建的虚拟环境的路径中。

可以通过以下几种方法解决此问题:

1. 使用 -r 标志重新创建 tox 环境:

这将强制 tox 重新创建虚拟环境,并包含的 tests 目录。

bash tox -r

2. 在 tox.ini 中指定 changedir

可以指示 tox 在运行测试之前更改到的项目根目录。

ini [testenv:py312] changedir = {toxinidir}/.. deps = pytest commands = pytest tests

这会将当前工作目录更改为 toxinidir .tox 目录)的父目录,即的项目根目录。

3. 使用 poetry install 安装包:

可以指示 tox 使用 Poetry 安装的包,包括开发依赖项,以便 tests 目录在路径中。

ini [testenv:py312] deps = pytest commands = poetry install pytest tests

这将使用 Poetry 安装的包,包括在 pyproject.toml 中定义的开发依赖项,并确保 tests 目录在 Python 路径中。

这些解决方案中的任何一个都应该可以解决的问题。我个人建议使用第三种方法,因为它更明确地定义了的测试环境,并且与 Poetry 的工作方式更加一致。

标签:python,pytest,python-poetry,tox
From: 78814048

相关文章

  • 三种语言实现二维前缀和(C++/Python/Java)
    题目输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1,y1,x2,y2表示一个子矩阵的左上角坐标和右下角坐标。对于每个询问输出子矩阵中所有数的和。输入格式第一行包含三个整数n,m,q接下来n行,每行包含m个整数,表示整数矩阵。接下来q行,每行包含四个整数......
  • Python rocketMq 客户端的同步和异步模式
    同步模式fromrocketmq.clientimportPushConsumer,ConsumeStatusimporttimedefcallback(msg):print(msg.id,msg.body,msg.get_property('property'))returnConsumeStatus.CONSUME_SUCCESSdefstart_consume_message():consumer=PushCon......
  • python中元组的学习
    元组目录元组元组的概念元组操作元组的常用方法元组的遍历元组的概念Tuple(元组)与列表相似,不同之处遭遇元组的元素不能修改元组表示多个元素组成的序列用于储存一串信息,数据之间使用,分隔元组用()定义#元组的创建info_tuple=("zhangsan",18,1.75)info_tuple2=(1,)#......
  • 尝试通过Python访问.zip文件中的.gz文件
    我有一个包含大量.gz文件的.zip文件,我需要对其进行处理。我想打开.zip,我可以通过以下代码轻松完成:zf=zipfile.ZipFile("file.zip","r")forgzfileinzf.filelist:withgzip.GzipFile(fileobj=zf.open(gzfile.filename,"r"),mode="r")asf:df......
  • python导入包报错ImportError: cannot import name ‘Protocol‘
    python32.pyTraceback(mostrecentcalllast):File"2.py",line5,in<module>importptwt#use"fromsrcimportptwt"foraclonedtherepoFile"……lib/python3.6/site-packages/ptwt/_util.py",line2......
  • Python - Creating your own Iterator
    Inourfirstexample,wewillcreateiterableobjects,which,wheniteratedover,willgiveoutcubesofnumbers,andtheseobjectswillsupportmultipleiterations.classCubes:def__init__(self,start,stop):self.start=startsel......
  • 三种语言实现前缀和(C++/Python/Java)
    题目输入一个长度为n的整数序列。接下来再输入m个询问,每个询问输入一对l,r对于每个询问,输出原序列中从第l个数到第r个数的和。输入格式第一行包含两个整数n和m。第二行包含n个整数,表示整数数列。接下来m行,每行包含两个整数l和r,表示一个询问的区间范围。......
  • Python - 旨在通过命令提示符执行数据清理,但代码似乎无法运行
    我从一位同事那里收到了这段代码,我打算用它来处理100csv文件以提取有关粒子的值。代码如下所示:importsysimportcsv#Usage#skdata_decode.py[inputfile1][inputfile2]...#(Itispossibletousefiledcardtospecifyinputfiles.)##l......
  • 如何在 python 终端中的 x,y 位置上书写(基于文本)
    我想在python(基于文本)的终端中的定义位置(x,y)上写入字符。假设,我有一个大小为25x80的终端,并且想要在位置(2,20)上写入字符。我可以在Python中执行此操作吗?现在,我使用25x80数组,并写入该数组。为了在屏幕上显示,我清除屏幕并将该数组的全部内容写入屏幕,但这效......
  • Python - Composition
     classEngine:def__init__(self,power):self.power=powerdefstart(self):self.draw_current()self.spin()self.ignite()defdraw_current(self):print('Drawingcurrent')defspin(sel......