Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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_Pytest_Exec - Fatal编程技术网

Python 如何从控制台菜单运行pytest?

Python 如何从控制台菜单运行pytest?,python,pytest,exec,Python,Pytest,Exec,我有控制台菜单,比如: if n == 1: exec('pytest test_1.py' ) # example, it doesn't work elif n == 2: exec('pytest test_2.py' ) # example, it doesn't work 和login.py: from functools import partial import pytest_bdd scenario = partial(pytest_bdd.scenario,

我有控制台菜单,比如:

if n == 1:
    exec('pytest test_1.py' ) # example, it doesn't work
elif n == 2:
    exec('pytest test_2.py' ) # example, it doesn't work
和login.py:

from functools import partial
import pytest_bdd

scenario = partial(pytest_bdd.scenario, '..\\login.feature')

@Scenario('login 2')
def test_2():
    pass()
当我使用这个菜单时,我想为特殊方法启动pytest(test_2)。 现在我从PyCharm终端运行pytest或“运行”“调试”

如何使用python中的exec()调用pytest?
比如:


可能吗?或者它有另一种方式?

我认为您可以使用子流程调用pytest:

import subprocess
subprocess.run(["pytest", "-s", "login.test_2"])

也请看@hoefling TY,我将搜索并阅读有关它的信息。TY!这是工作。请稍等:需要在venv中使用py.test的普通路径和python文件的路径进行测试。
import subprocess
subprocess.run(["pytest", "-s", "login.test_2"])