英文:
How to pass variables during pytest call via python script(not command line)?
问题
以下是已翻译的内容:
"我正在尝试在Python脚本中使用pytest环境运行测试。我需要在script.py中定义的名为config的参数被文件'test_functions.py'使用。以下是我的实现。
但是我收到了错误消息:
fixture 'config' 未找到 可用的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
请使用 'pytest --fixtures [testpath]' 获取有关它们的帮助。"
script.py的内容:
import pytest
if __name__ == '__main__':
config = {
'key1': 'value1',
'key2': 'value2',
# 根据需要添加更多键值对
}
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):
# 访问配置字典
print(config['key1'])
assert config['key1'] == 'value2'
英文:
I am trying to run a test using pytest environment within a python script. I need the parameter called config which is defined in script.py to be used by the file 'test_functions.py'
Below is my implementtion .
However I get the error
> 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.`
contents of 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])
content of test_functions.py' is as follows
def test_function(config):
# Access the config dictionary
print(config['monthly_or_weekly'])
assert config['value1']=='value2'
答案1
得分: 0
您正在尝试在测试中使用“config”作为一个 fixture,但是在您的代码中未定义“config”作为 fixture。您想要像这样做:
import pytest
@pytest.fixture
def config():
yield {
'key1': 'value1',
'key2': 'value2',
}
def test_function(config):
print(config['key1'])
print(config['key2'])
}
英文:
You're trying to use config
as a fixture in your test, but config
isn't being defined as one in your code. You want something like this:
import pytest
@pytest.fixture
def config():
yield {
'key1': 'value1',
'key2': 'value2',
}
def test_function(config):
print(config['key1'])
print(config['key2'])
答案2
得分: 0
我们可以像这样在 PyTest 中使用配置文件:pytest --variables mac_capabilities.json ./demo.py。查看它 - "https://pypi.org/project/pytest-variables/"
应该安装 pytest-variables
英文:
We can use config file in PyTest like this: pytest --variables mac_capabilities.json ./demo.py. check it out - "https://pypi.org/project/pytest-variables/"
should have pytestvariables
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论