3

I have a fixture that creates an object I use in many tests.
This Object has a property - priority (1 - 10)

Now, there are a lot of tests that need to "know" the priority of the object (in order to test various logic paths)

So I could have 10 different fixtures:

@pytest.fixture
def object_priority_1():
    return MyObj(priority=1)
@pytest.fixture
def object_priority_2():
    return MyObj(priority=2)
//....
@pytest.fixture
def object_priority_10():
    return MyObj(priority=10)

But it seems off... I'm sure there is a way to deliver an external param to a fixture each time - I just couldn't find it

Edit: To clarify - I would have wanted something in the area of:

@pytest.fixture
    def my_object(<priority as an external param>):
        return MyObj(priority)

And then the test would be something like:

def test_foo(my_object(1), my_object(6)):
//...
Boaz
  • 4,864
  • 12
  • 50
  • 90
  • possible duplicate of [py.test: Pass a parameter to a fixture function](http://stackoverflow.com/questions/18011902/py-test-pass-a-parameter-to-a-fixture-function) – Richard Aug 30 '15 at 14:17

2 Answers2

5

You can do :

@pytest.fixture
def my_object(priority):
    return MyObj(priority)    

def test_foo(my_object):
    obj_1 = my_object(1)
    assert something # 1
    obj_2 = my_object(2)
    assert something # 2

I come with this idea, then I found this Q&A :

py.test: Pass a parameter to a fixture function

I think your question maybe consider a duplicate if your problem is solved.

Community
  • 1
  • 1
Richard
  • 721
  • 5
  • 16
  • 1
    I tried this particular syntax earlier and didn't quite work, it errored out saying there fixture `priority` couldn't be found. The solution in the link you provided does work though. – Victor Uriarte Jun 19 '16 at 16:26
  • Is an object (function) defined with an parameters named priority? – Richard Jun 20 '16 at 17:39
  • Not with that specific variable name. I got it working when I used the variable name `request`, but didn't work when i replaced the variable with the one i actually wanted to use (`filename`). I'm going to try it again in case it was user error. You can see the code I'm refering to [here](https://github.com/vmuriart/sqlparse/blob/85349e68592964e66e5dfe7e48e9f76cb93d48fd/tests/conftest.py#L32) – Victor Uriarte Jun 21 '16 at 00:45
  • `fixture 'priority' not found available fixtures: tmpdir_factory, pytestconfig, filepath, cache, recwarn, monkeypatch, record_xml_property, capfd, load_file, capsys, tmpdir use 'py.test --fixtures [testpath]' for help on them.` same error as before – Victor Uriarte Jun 21 '16 at 00:51
  • I gave a look at the code, I am not exactly sure I understand what you are trying to do... Please try the code as is and see if it allow to what it is suppose to do which is passing a parameter to an object from a test... You can mock the MyObj with a simple print function for instance... I am not sure it the right place to resolve your issue as I feel I would need much more inside on what you are doing and we are not supposed to use comment for this purpose... – Richard Jun 21 '16 at 19:40
  • No worries. My code is working using the solution provided on your link. I meant my initial comment as an FYI for anyone else that may have having similar issues as I did. It's still very likely it might be a mistake on my side though. Thanks for following up though :) – Victor Uriarte Jun 22 '16 at 03:39
-1

You can do something like this, it will return run your fixture with each of those params:

@pytest.fixture(
    params=[
        "param1",
        "param2",
        "param3",
        "param4",
        "param5",
        "param6",
    ]
)
def fixture_with_params(request):
    return request.param