No subject


Mon Jan 15 04:54:02 PST 2018


The scopes are used to know when the setup and teardown of each fixture
needs to run, e.g. with a function-scope the fixture will be created for
just before a single test which needs it and destroyed right after this
test has run.

For a module scope the fixture gets only created once for
all tests in a module.  So if multiple tests in test_x require either
fixtureMod_1 or fixtureMod_2 their setup and teardown will only be
executed once.

So scopes have nothing to do with ordering.

As you discovered if you declare dependencies on fixtures then the
ordering will start to be defined:

@pytest.fixture
def fixtureFunc_1(fixtureMod_2):
    pass

This will ensure that fixtureMod_2 is created first.  It's good that
this dependency needs to be explicit in your code.  However be careful
in general to not over-use dependencies like this, e.g. if the only
reason you declare fixtureMod_2 is to use as a dependency in
fixtureFunc_1 then maybe fixtureMod_2 shouldn't be a fixture at all.


As another aside btw, I always *strongy* advice against ever importing
fixtures.  The semantics of it are very confusing (this was probably a
design mistake) so instead always move the fixture to the right
location, i.e. your conftest.py file.

> 2. I have one usecase where i want to dynamically create a fixture chain..
> say i mark by testcase with useCaseA and that translates to an ordering of
> fixture.. can i add a fixture to a testcase at run time.. at
> pytest_generate_test phase or pytest_configure phase?? i found a method
> called add_marker to add a marker at runt time.. since fixtures can also be
> added via markers i was trying to explore if can use that method but it
> doesnt accept any parameters.. any way i can achieve this??

Here I'm somewhat lost with your example, I think you have this:

@pytest.mark.useCaseA
def test_foo():
    pass

But I'm afraid you'll have to give me more info on how tha translates to
fixtures in your implementation.

With respect to dynamically requested fixtures, this is usually done
using request.getfixturevalue() [0].  However, and this is where
personal opinions differ, my advice is to avoid this if you possibly
can.  And my opinion is also that this is always possible, e.g. a
fixture can return instances of two different classes depending on the
dynamic "thing".


Hope this helps,
Floris


[0] https://docs.pytest.org/en/latest/builtin.html?highlight=getfixturevalue#_pytest.fixtures.FixtureRequest.getfixturevalue



More information about the testing-in-python mailing list