py.test如何以及在哪里找到固定装置

Dav*_*ave 33 python fixtures pytest

py.test在哪里以​​及如何寻找灯具?我在同一文件夹中的2个文件中有相同的代码.当我删除conftest.py时,找不到运行test_conf.py的cmdopt(也在同一个文件夹中.为什么没有搜索到sonoftest.py?

# content of test_sample.py
def test_answer(cmdopt):
    if cmdopt == "type1":
        print ("first")
    elif cmdopt == "type2":
        print ("second")
    assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)

conftest.py的内容

import pytest

def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")

@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)

sonoftest.py的内容

import pytest

def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")

@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)

文档说

http://pytest.org/latest/fixture.html#fixture-function

  1. pytest因test_前缀而找到test_ehlo.测试函数需要一个名为smtp的函数参数.通过查找名为smtp的夹具标记函数来发现匹配夹具功能.
  2. 调用smtp()来创建实例.
  3. 调用test_ehlo()并在测试函数的最后一行失败.

eca*_*mur 31

默认情况下,py.test将导入conftest.pypython_files模式匹配的所有Python文件test_*.py.如果您有测试夹具,则需要在conftest.py依赖于它的测试文件中包含或从中导入它:

from sonoftest import pytest_addoption, cmdopt
Run Code Online (Sandbox Code Playgroud)

  • 由于某种原因,如果您使用 Pycharm,它会将夹具的导入检测为未使用(至少在版本 2021.2.3 中)。无论如何,如果您删除夹具导入,它就会停止工作。 (2认同)

Ale*_*hko 19

以下是py.test查找fixture(和测试)的顺序(从这里开始):

py.test以下列方式在工具启动时加载插件模块:

  1. 通过加载所有内置插件

  2. 通过加载通过setuptools入口点注册的所有插件.

  3. 通过预扫描-p name选项的命令行并在实际命令行解析之前加载指定的插件.

  4. 通过conftest.py命令行调用(测试文件及其所有父目录)推断加载所有文件.请注意,conftest.py默认情况下,子目录中的 文件不会在工具启动时加载.

  5. 通过递归加载conftest.py文件中pytest_plugins变量指定的所有插件


raf*_*asa 7

我遇到了同样的问题,并花了很多时间找到一个简单的解决方案,这个例子适用于其他与我有类似情况的人。

  • conftest.py:
import pytest

pytest_plugins = [
 "some_package.sonoftest"
]

def pytest_addoption(parser):
  parser.addoption("--cmdopt", action="store", default="type1",
      help="my option: type1 or type2")

@pytest.fixture
def cmdopt(request):
  return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
  • some_package/sonoftest.py:
import pytest

@pytest.fixture
def sono_cmdopt(request):
  return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
  • 一些_package/test_sample.py
def test_answer1(cmdopt):
  if cmdopt == "type1":
      print ("first")
  elif cmdopt == "type2":
      print ("second")
  assert 0 # to see what was printed

def test_answer2(sono_cmdopt):
  if sono_cmdopt == "type1":
      print ("first")
  elif sono_cmdopt == "type2":
      print ("second")
  assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到类似的示例: https: //github.com/pytest-dev/pytest/issues/3039#issuecomment-464489204 以及其他此处/sf/answers/3831546351/

官方pytest文档的描述:https://docs.pytest.org/en/latest/reference.html? highlight=pytest_plugins#pytest-plugins

请注意,所引用的各个目录 some_package.test_sample"需要有用于__init__.py加载插件的文件pytest