1

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'
  

2 Answers2

0

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'])

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • Thanks for your response -- I am trying to define the fuction def test_function in a seperate file and invoke the test with the expression pytest.main(['test_function.py'])-- We need to pass this fixture config during the call. – Tilak Mallikarjun Jul 15 '23 at 14:40
  • You can define your fixtures in a ``conftest.py`` file so that they are available to all tests. https://stackoverflow.com/a/34520971/7058266 – Michael Mintz Jul 15 '23 at 15:54
0

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/"

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 11 '23 at 20:09