Как передать переменные во время вызова pytest через скрипт Python (а не из командной строки)?Python

Программы на Python
Anonymous
Как передать переменные во время вызова pytest через скрипт Python (а не из командной строки)?

Сообщение Anonymous »

Я пытаюсь запустить тест, используя среду pytest в скрипте Python. Мне нужен параметр config, который определен в script.py, который будет использоваться файлом test_functions.py.
Ниже приведена моя реализация.
Однако я получаю сообщение об ошибке

E fixture 'config' not found available fixtures: anyio_backend, anyio_backend_name, anyio_backend_options, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, info, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
use 'pytest --fixtures [testpath]' for help on them.`


содержимое script.py
import pytest

if __name__ == '__main__':

config = {
'key1': 'value1',
'key2': 'value2',
# Add more key-value pairs as needed
}

class MyPlugin(object):
def __init__(self, data):
self.data = data

@pytest.fixture
def info(self, request):
return self.data

myplugin = MyPlugin(config)

pytest.main(['test_functions.py'],plugins=[myplugin])

содержимое test_functions.py' следующее
def test_function(config):
# Access the config dictionary
print(config['monthly_or_weekly'])
assert config['value1']=='value2'



Подробнее здесь: https://stackoverflow.com/questions/766 ... mmand-line

Вернуться в «Python»