pytest 可以接收常规 python 参数或从配置文件中读取参数吗?

Can pytest receive regular python parameters or read params from configuration file?

我正在尝试使用 pytest 来测试某些 AWS Lambdas。

为了执行某些操作,我必须传入一些 ID。

我用非常老套的方式做到了,在 conftest.py:

def pytest_addoption(parser):
    store = ParamsStore()
    try:
        parames = sys.argv[1:]
        print(parames)
        store.my_id = parames[1]
        store.dont_store = parames[3]
    except:
        store.existing_stack = ''
    parser.addoption("--myid", action="store", default="", help="Pass RunId To reuse it Later")
    parser.addoption("--dontstore", action="store", default=False, help="Pass True if you'd like to persist when tests are passing")

我知道这是“反对”pytest 逻辑,但我需要将这些参数存储为全局参数。

现在我要解决两个问题。

  1. 我希望它不要像现在这样笨拙,我通过读取参数来拦截参数。

我已经阅读了所有“make fixtures”文档和建议,但我需要在 conftest.py 中在任何测试之前使用它们。它甚至不是“在所有之前”或每个“每个测试”,它实际上是通过读取本地 AWS cdk.out 并在 运行 测试之前操纵云程序集(仅一次)来设置整个过程,并且该过程是完成一次,因此不需要“每次测试”。

到目前为止我看到的唯一方法是有两个单独的脚本,一个常规 python 代码,然后 运行 在 pytest 本身之前,但这变得很难处理,所以我正在尝试将其捆绑并仅通过使用 pytest 来完成所有操作。

如果我可以读取 args - 它会起作用。如果我可以读取一些配置文件 - 它会起作用。如果我可以将 pytest 捆绑到“首先执行一些 python 规定”,然后 运行 pytest - 它也可能有效。

  1. 我想(我不确定 1 是否可以更好地解决)只传递一个标志“--dontstore”,之后没有任何价值,只是标志的存在就意味着某些东西是 'True',如果不存在,则为 'False'.

谢谢,

选项 1:conftest.py:

中这样的事情怎么样
def pytest_addoption(parser):
    parser.addoption("--myid", action="store", default="", help="Pass RunId To reuse it Later")
    parser.addoption("--dontstore", action="store", default=False, help="Pass True if you'd like to persist when tests are passing")

def pytest_configure(config):
    my_id = config.getoption("--myid")
    dont_store = config.getoption("--dontstore")

将这两个变量用于您的设置。

选项 2

Set-up 一个带有 python 脚本的 jenkins 作业,用于在 pytests 之前设置为 运行。