No subject


Tue Jan 16 06:10:05 PST 2018


r>
The scopes are used to know when the setup and teardown of each fixture<br>
needs to run, e.g. with a function-scope the fixture will be created for<br=
>
just before a single test which needs it and destroyed right after this<br>
test has run.<br>
<br>
For a module scope the fixture gets only created once for<br>
all tests in a module.=C2=A0 So if multiple tests in test_x require either<=
br>
fixtureMod_1 or fixtureMod_2 their setup and teardown will only be<br>
executed once.<br>
<br>
So scopes have nothing to do with ordering.<br>
<br>
As you discovered if you declare dependencies on fixtures then the<br>
ordering will start to be defined:<br>
<br>
@pytest.fixture<br>
def fixtureFunc_1(fixtureMod_2):<br>
=C2=A0 =C2=A0 pass<br>
<br>
This will ensure that fixtureMod_2 is created first.=C2=A0 It&#39;s good th=
at<br>
this dependency needs to be explicit in your code.=C2=A0 However be careful=
<br>
in general to not over-use dependencies like this, e.g. if the only<br>
reason you declare fixtureMod_2 is to use as a dependency in<br>
fixtureFunc_1 then maybe fixtureMod_2 shouldn&#39;t be a fixture at all.<br=
>
<br>
<br>
As another aside btw, I always *strongy* advice against ever importing<br>
fixtures.=C2=A0 The semantics of it are very confusing (this was probably a=
<br>
design mistake) so instead always move the fixture to the right<br>
location, i.e. your conftest.py file.<br>
<span class=3D""><br>
&gt; 2. I have one usecase where i want to dynamically create a fixture cha=
in..<br>
&gt; say i mark by testcase with useCaseA and that translates to an orderin=
g of<br>
&gt; fixture.. can i add a fixture to a testcase at run time.. at<br>
&gt; pytest_generate_test phase or pytest_configure phase?? i found a metho=
d<br>
&gt; called add_marker to add a marker at runt time.. since fixtures can al=
so be<br>
&gt; added via markers i was trying to explore if can use that method but i=
t<br>
&gt; doesnt accept any parameters.. any way i can achieve this??<br>
<br>
</span>Here I&#39;m somewhat lost with your example, I think you have this:=
<br>
<br>
@pytest.mark.useCaseA<br>
def test_foo():<br>
=C2=A0 =C2=A0 pass<br>
<br>
But I&#39;m afraid you&#39;ll have to give me more info on how tha translat=
es to<br>
fixtures in your implementation.<br>
<br>
With respect to dynamically requested fixtures, this is usually done<br>
using request.getfixturevalue() [0].=C2=A0 However, and this is where<br>
personal opinions differ, my advice is to avoid this if you possibly<br>
can.=C2=A0 And my opinion is also that this is always possible, e.g. a<br>
fixture can return instances of two different classes depending on the<br>
dynamic &quot;thing&quot;.<br>
<br>
<br>
Hope this helps,<br>
Floris<br>
<br>
<br>
[0] <a href=3D"https://docs.pytest.org/en/latest/builtin.html?highlight=3Dg=
etfixturevalue#_pytest.fixtures.FixtureRequest.getfixturevalue" rel=3D"nore=
ferrer" target=3D"_blank">https://docs.pytest.org/en/<wbr>latest/builtin.ht=
ml?highlight=3D<wbr>getfixturevalue#_pytest.<wbr>fixtures.FixtureRequest.<w=
br>getfixturevalue</a><br>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature">=E0=AE=85=E0=AE=A9=
=E0=AF=8D=E0=AE=AA=E0=AF=81=E0=AE=9F=E0=AE=A9=E0=AF=8D,<br>
=E0=AE=AA=E0=AE=BE. =E0=AE=85=E0=AE=B0=E0=AF=81=E0=AE=A3=E0=AF=8D =E0=AE=95=
=E0=AE=BE=E0=AE=B3=E0=AE=BF =E0=AE=B0=E0=AE=BE=E0=AE=9A=E0=AE=BE </div>
</div>

--001a114b2d023150000562e764a4--



More information about the testing-in-python mailing list