如何通过Python脚本在pytest调用期间传递变量(而不是通过命令行)?

huangapple go评论159阅读模式
英文:

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的内容

  1. import pytest
  2. if __name__ == '__main__':
  3. config = {
  4. 'key1': 'value1',
  5. 'key2': 'value2',
  6. # 根据需要添加更多键值对
  7. }
  8. class MyPlugin(object):
  9. def __init__(self, data):
  10. self.data = data
  11. @pytest.fixture
  12. def info(self, request):
  13. return self.data
  14. myplugin = MyPlugin(config)
  15. pytest.main(['test_functions.py'], plugins=[myplugin])

test_functions.py的内容如下:

  1. def test_function(config):
  2. # 访问配置字典
  3. print(config['key1'])
  4. 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

  1. import pytest
  2. if __name__ == '__main__':
  3. config = {
  4. 'key1': 'value1',
  5. 'key2': 'value2',
  6. # Add more key-value pairs as needed
  7. }
  8. class MyPlugin(object):
  9. def __init__(self, data):
  10. self.data = data
  11. @pytest.fixture
  12. def info(self, request):
  13. return self.data
  14. myplugin = MyPlugin(config)
  15. pytest.main(['test_functions.py'],plugins=[myplugin])

content of test_functions.py' is as follows

  1. def test_function(config):
  2. # Access the config dictionary
  3. print(config['monthly_or_weekly'])
  4. assert config['value1']=='value2'

答案1

得分: 0

您正在尝试在测试中使用“config”作为一个 fixture,但是在您的代码中未定义“config”作为 fixture。您想要像这样做:

  1. import pytest
  2. @pytest.fixture
  3. def config():
  4. yield {
  5. 'key1': 'value1',
  6. 'key2': 'value2',
  7. }
  8. def test_function(config):
  9. print(config['key1'])
  10. print(config['key2'])
  11. }
英文:

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:

  1. import pytest
  2. @pytest.fixture
  3. def config():
  4. yield {
  5. 'key1': 'value1',
  6. 'key2': 'value2',
  7. }
  8. def test_function(config):
  9. print(config['key1'])
  10. 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

huangapple
  • 本文由 发表于 2023年7月13日 20:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定