我有一个脚本,可以循环多个项目并执行
pytest ${project} --foo 123
。我需要将选项从 foo
更改为 bar
。我想将脚本更改为 pytest ${project} --foo 123 --bar 123
,给团队时间从使用 foo
切换到 bar
,然后删除 --foo 123
。有办法实现吗?
为此,您可能需要使用运行
pytest.ini
的根文件夹中的 pytest
文件。 (更多信息:https://pytest.org/en/latest/reference/customize.html)
在
pytes.ini
文件中,您可能有这样的内容:
[pytest]
# Display console output and disable cacheprovider:
addopts = --capture=no -p no:cacheprovider
pytest.ini
文件允许您为使用 pytest
运行的所有测试指定参数,这样您就不必直接在运行命令中指定这些参数。
对于您的情况,最初可能看起来像这样:
[pytest]
addopts = --foo 123 --bar 123
然后您可以根据需要更换东西。要修改其中包含
foo_abc
的现有脚本(并替换为 bar_abc
),您可以使用以下脚本(Linux 示例):
对于当前目录中的 Python 文件,在 Linux 上将所有出现的“foo_abc”替换为“bar_xyz”:
sed -i 's/foo_abc/bar_xyz/g' *.py
(其他操作系统的最佳字符串替换命令可能略有不同。)
为了避免在尚未声明
--bar
时出现错误,您可以在 conftest.py
文件中创建临时参数。 (参考:https://docs.pytest.org/en/latest/example/simple.html)
def pytest_addoption(parser):
parser.addoption(
"--bar", action="store", dest="bar_var", default=None
)
然后在实际定义了
--bar
参数后删除该占位符。