Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest:为测试集合定义自定义路径_Python_Python 3.x_Pytest - Fatal编程技术网

Python Pytest:为测试集合定义自定义路径

Python Pytest:为测试集合定义自定义路径,python,python-3.x,pytest,Python,Python 3.x,Pytest,我正在构建一个测试运行程序包,其中包含定制的fixture、pytest.ini和conftest。 我希望此运行程序能够接受目录路径作为参数, 并收集该路径中的测试,并使用自定义配置文件运行它们 我现在做的是将给定目录复制到包中的已知位置,然后以编程方式运行pytest,路径为tests和-rootdir参数 Dir tree: - runner ---- tests ---- conftest.py ---- pytest.ini ---- fixtures.py ---- runner.p

我正在构建一个测试运行程序包,其中包含定制的fixture、pytest.ini和conftest。 我希望此运行程序能够接受目录路径作为参数, 并收集该路径中的测试,并使用自定义配置文件运行它们

我现在做的是将给定目录复制到包中的已知位置,然后以编程方式运行pytest,路径为tests和-rootdir参数

Dir tree:
- runner
---- tests
---- conftest.py
---- pytest.ini
---- fixtures.py
---- runner.py

Python code:
pytest_params = ['tests', '--rootdir=runner', <other params passed to pytest>]
pytest.main([pytest_params])
这很有效。 但是我不想每次运行都复制整个测试目录

我试图在pytest.ini中设置testpath arg并从pytest参数中远程设置path参数,但这会导致pytest丢失rootdir和pytest.ini文件,当然会失败


有什么想法吗?

您需要明确指定ini文件的位置。可以使用-c参数指定ini文件的位置。要从runner目录加载设置,需要使用pytest运行conftest.py

您的参数如下所示:

pytest_params = ['conftest.py', '-c=pytest.ini', '--rootdir=<path_of_external_tests', <other params passed to pytest>]
pytest.main(pytest_params)

谢谢@SilentGuy,但是现在,当指定conftest.py时,pytest找不到任何测试。当我删除conftest.py参数时,只有pytest才能在rootdir中找到测试。有什么想法吗?补充:显然,当指定conftest时,pytest会忽略-rootdir并在conftest所在的同一目录中查找测试:/一种解决方法是在python脚本中将conftest文件符号链接到runner dir的顶部。这样,pytest将收集所有测试,就好像conftest.py在那里一样。但是目录中不能有多个conftest.py。我已尝试将所有测试符号链接到已知目录,但这会导致conftest出现各种问题:/其他解决方法是将rootdir定义为conftest和test目录的公共顶部目录。并且,将测试集合包含列表指定为conftest和测试目录。e、 g.测试路径:a/b/c/tests和conftest a/d/x/conftest.py。将a指定为根目录,并仅指定a/b/c/tests和a/d/x/as-collect。这是老套的方式。非黑客方式是将您的配置打包到pytest插件中。并安装该插件,以便在任何需要的地方使用该配置。