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"])