如何修改pytest参数?

时间:2018-11-30 01:04:31

标签: python automated-tests pytest xdist

我发现可以使用PyTest函数pytest_load_initial_conftests()

https://docs.pytest.org/en/latest/example/simple.html#dynamically-adding-command-line-options

但是我无法正确实现此示例(请参见链接)。

pytest_load_initial_conftests()甚至没有启动(通过调试查看)。 测试可以在没有任何参数(一个线程)的情况下正常进行,但是我希望参数是“ -n”。

我安装了pytest和xdist。 项目中只有两个文件。没有pytest.ini。

我在做什么错?请帮助运行它。

conftest.py

import pytest
import os
import sys


def pytest_addoption(parser):
    parser.addoption('--some_param', action='store', help='some_param', default='')


def pytest_configure(config):
    some_param = config.getoption('--some_param')


def pytest_load_initial_conftests(args):
    if "xdist" in sys.modules:
        import multiprocessing
        num = max(multiprocessing.cpu_count() / 2, 1)
        args[:] = ["-n", str(num)] + args

test_t1.py

import inspect
from time import sleep
import os
import pytest


class Test_Run:

    def test_1(self):
        body()

    def test_2(self):
        body()

    def test_3(self):
        body()

    def test_4(self):
        body()

    def setup(self):
        pass

    def teardown(self):
        pass


def body():
    sleep(5)

1 个答案:

答案 0 :(得分:2)

根据pytest_load_initial_conftests上的文档:

  

注意:不会为conftest.py文件调用此挂钩,仅针对   setuptools插件。

https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_load_initial_conftests

在您找到的页面上可能不应提及它。