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

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

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

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:

确定