将bash变量传递给pytest

时间:2018-10-24 14:13:15

标签: python bash pytest

我在将bash变量传递给pytests时遇到问题。这是运行的内容:

FILES="$(git ls-files)"
python3.6 -m pytest -q tests/TestStructure.py --myoption=$FILES[0] --junitxml results.xml

如果我将它打印到终端,这就是$ FILES中的内容:

README.md controls_remediation/__init__.py controls_remediation/control_handler.py controls_remediation/dbfunctions.py controls_remediation/main.py controls_remediation/pandafunctions.py controls_remediation/utilities/CONSTANTS.py controls_remediation/utilities/__init__.py controls_remediation/utilities/hadoopconnect.py controls_remediation/utilities/log_helper.py controls_remediation/utilities/settings_constants.py kafka_controls/KAFKACONSTANTS.py kafka_controls/__init__.py kafka_controls/producer.py setup.py tests/TestStructure.py tests/__init__.py tests/conftest.py tests/functional/TestFunctional.py tests/functional/__init__.py tests/unit/TestKafka.py tests/unit/TestPandaFunctions.py tests/unit/__init__.py

这是TestStructure.py的样子:

import pytest


def test_answer(myoption):
    if myoption == "this":
        print ("do this option")
    elif myoption == "that":
        print ("do that option")
    else:
        print(myoption)
    assert 0

这是conftest.py的样子:

import pytest


def pytest_addoption(parser):
    parser.addoption("--myoption", action="store", default="that",
        help="my option: this or that")


@pytest.fixture
def myoption(request):
    return request.config.getoption("--myoption")

这是我使用第一部分运行pytest时遇到的错误:

no tests ran in 0.00 seconds
ERROR: file not found: tests/unit/__init__.py[0]

我真的不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您只需命名数组$FILES即可访问数组的第一个元素。

您还可以通过放置索引来访问第一个元素,但是必须将表达式用大括号括起来:${FILES[0]}

通过使用$FILES[0],您将扩展$FILES,然后将[0]添加到该字符串。

尝试:

FILES="$(git ls-files)"
python3.6 -m pytest -q tests/TestStructure.py --myoption=${FILES[0]} --junitxml results.xml