前言
setup和teardown能实现在测试用例执行之前或之后做一些操作,但是这种是整个测试脚本全局生效的;
如果想实现某些用例执行之前进行登录,某些用例执行之前不需要进行登录,这种场景我们再使用setup和teardown就无法实现了,这时候我们就需要用到fixture功能了。
1、fixture函数
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python">fixture<span style="color:#999999">(</span>scope<span style="color:#669900">=</span><span style="color:#669900">"function"</span><span style="color:#999999">,</span> params<span style="color:#669900">=</span><span style="color:#56b6c2">None</span><span style="color:#999999">,</span> autouse<span style="color:#669900">=</span><span style="color:#56b6c2">False</span><span style="color:#999999">,</span> ids<span style="color:#669900">=</span><span style="color:#56b6c2">None</span><span style="color:#999999">,</span> name<span style="color:#669900">=</span><span style="color:#56b6c2">None</span><span style="color:#999999">)</span>
</code></span></span>
参数说明:
1)scope:fixture函数的作用域;
可选值:function(默认)、class、module、session
function:作用于每个方法或函数,每个方法或函数都运行一次
class:作用于整个class类,每个class中的所有test只运行一次
module:作用于整个模块,每个module中的所有test只运行一次
session:作用于整个session,整个session只运行一次(慎用)
2)params:列表类型;
一个可选的参数列表;
它将会多次调用被fixture标记的方法和所有用到这个fixture的test测试用例;
默认为None;当前调用参数可以用 request.param 来获取。
3)autouse:如果为True,则为所有测试用例激活fixture,运行测试用例的时候会自动运行被fixture标记的方法;
如果为False,则需要显示指定来激活fixture,不会自动运行。
4)ids:id字符串列表,与params相对应,因此它们也是测试的一部分。如果没有提供ids,那么将会从params来自动生成。
5)name:fixture的名称。默认为被fixture装饰器标记的函数名。
2、fixture的使用
1)通过参数引用fixture函数
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name:test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> before<span style="color:#999999">)</span><span style="color:#999999">:</span> <span style="color:#5c6370"># test_a方法以参数的形式传入了被fixture标记的函数,fixture的名称默认为被fixture标记的函数名</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
2)通过使用name参数来引用fixture函数
①name参数表示fixture的重命名;
②通常来说使用 fixture 的测试函数会将 fixture 的函数名作为参数传递,但是 pytest 也允许将fixture重命名。
举例1:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name:test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span>name<span style="color:#669900">=</span><span style="color:#669900">"before_fixture_name"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> before_fixture_name<span style="color:#999999">)</span><span style="color:#999999">:</span> <span style="color:#5c6370"># test_a方法以参数的形式传入了被fixture标记的函数,这里的fixture名称为:before_fixture_name,如果不设置name参数,则fixture的名称默认为被fixture标记的函数名</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
举例2:
为fixture函数重命名之后,不可以在使用fixture函数的函数名来调用,只能通过fixture函数重命名的新名字来调用。
3)通过@pytest.mark.usefixtures(‘fixture函数名’)函数的形式引用fixture函数
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#5c6370"># 被fixture标记的函数也可以应用在测试类的外部,使用@pytest.mark.usefixtures()装饰器来引用</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>mark<span style="color:#999999">.</span>usefixtures</span><span style="color:#999999">(</span><span style="color:#669900">"before"</span><span style="color:#999999">)</span> <span style="color:#5c6370"># 通过使用usefixtures()来引用fixture,此时usefixtures()函数的入参是fixture函数的函数名</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
4)通过autouse=True设置默认执行fixture函数
①fixture函数的autouse参数默认等于False;
②fixture函数的autouse参数若为True,刚每个测试函数都会自动调用该fixture函数,而且无需传入fixture函数名。
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span>autouse<span style="color:#669900">=</span><span style="color:#56b6c2">True</span><span style="color:#999999">)</span> <span style="color:#5c6370"># 通过参数autouse=True来设置fixture默认运行</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_b</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_b has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
5)fixture作用域设置成function
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span>scope<span style="color:#669900">=</span><span style="color:#669900">"function"</span><span style="color:#999999">,</span> autouse<span style="color:#669900">=</span><span style="color:#56b6c2">True</span><span style="color:#999999">)</span> <span style="color:#5c6370"># 作用域设置成function,通过参数autouse=True来设置fixture默认运行</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_b</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_b has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
6)fixture作用域设置成class
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span>scope<span style="color:#669900">=</span><span style="color:#669900">"class"</span><span style="color:#999999">,</span> autouse<span style="color:#669900">=</span><span style="color:#56b6c2">True</span><span style="color:#999999">)</span> <span style="color:#5c6370"># 作用域设置成class,通过参数autouse=True来设置fixture默认运行</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">before</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_b</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_b has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
7)fixture的返回值使用
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">return_data</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">return</span> <span style="color:#98c379">2</span> <span style="color:#5c6370"># 返回值</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> return_data<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran-------'</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span> <span style="color:#669900">==</span> return_data <span style="color:#5c6370"># 拿到返回值做断言</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
8)fixture的params参数使用
①params形参是fixture函数的可选形参列表,支持列表传入;
②不传此参数时默认为None;
③每个param的值fixture函数都会去调用执行一次,类似for循环。
④可与参数ids一起使用,作为每个参数的标识,类似于用例参数化时的ids作用。
举例:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># file_name: test_fixture.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span>params<span style="color:#669900">=</span><span style="color:#999999">[</span><span style="color:#98c379">1</span><span style="color:#999999">,</span> <span style="color:#98c379">2</span><span style="color:#999999">,</span> <span style="color:#98c379">3</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">return_data</span><span style="color:#999999">(</span>request<span style="color:#999999">)</span><span style="color:#999999">:</span> <span style="color:#5c6370"># 传入参数request,request系统内置的fixture</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"\n--------before fixture has ran--------"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">return</span> request<span style="color:#999999">.</span>param <span style="color:#5c6370"># 通过request.param 获取当前传入的参数</span>
<span style="color:#c678dd">class</span> Test_A<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_a</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> return_data<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">'-------test_a has ran,return_data的值为:{}-------'</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>return_data<span style="color:#999999">)</span><span style="color:#999999">)</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span> <span style="color:#669900">==</span> return_data <span style="color:#5c6370"># 拿到返回值做断言</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">[</span><span style="color:#669900">'-s'</span><span style="color:#999999">,</span> <span style="color:#669900">'test_fixture.py'</span><span style="color:#999999">]</span><span style="color:#999999">)</span>
</code></span></span>
运行结果:
从结果中我们可以看到测试用例执行了3次。通过设置params参数会导致多次调用被fixture标记的函数,并且使用该fixture函数的测试用例也会执行多次。
9)fixture的params参数于ids参数结合使用
①fixture函数未配置ids参数之前:用例执行后的标识为传入的params参数。
②fixture函数配置ids参数之后:用例执行后的标识为传入的ids参数。并与params参数一一对应。
10)fixture函数的相互调用(fixture函数与fixture函数之间的依赖关系)
举例1:
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#c678dd">import</span> pytest
<span style="color:#5c6370"># fixtrue作为参数,互相调用传入</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">account</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
a <span style="color:#669900">=</span> <span style="color:#669900">"account"</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"第一层fixture"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">return</span> a
<span style="color:#5c6370">#Fixture的相互调用一定是要在测试类里调用这层fixture才会生次,普通函数单独调用是不生效的</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">login</span><span style="color:#999999">(</span>account<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"第二层fixture"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">class</span> TestLogin<span style="color:#999999">:</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_1</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> login<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"直接使用第二层fixture,返回值为{}"</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>login<span style="color:#999999">)</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_2</span><span style="color:#999999">(</span>self<span style="color:#999999">,</span> account<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"只调用account fixture,返回值为{}"</span><span style="color:#999999">.</span><span style="color:#669900">format</span><span style="color:#999999">(</span>account<span style="color:#999999">)</span><span style="color:#999999">)</span>
<span style="color:#c678dd">if</span> __name__ <span style="color:#669900">==</span> <span style="color:#669900">'__main__'</span><span style="color:#999999">:</span>
pytest<span style="color:#999999">.</span>main<span style="color:#999999">(</span><span style="color:#999999">)</span>
</code></span></span>
举例2:
如果一个fixture函数依赖另外一个fixture函数,此时不能使@pytest.mark.usefixtures() 调用被依赖的fixture函数,这种调用方式不会生效。而是需要用函数传递的方式才能生效。
<span style="color:#000000"><span style="background-color:#282c34"><code class="language-python"><span style="color:#5c6370"># test_fixture_02.py</span>
<span style="color:#c678dd">import</span> pytest
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">login_weibo</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"==============登陆微博==============="</span><span style="color:#999999">)</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>fixture</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#5c6370"># @pytest.mark.usefixtures("login_weibo") #这种方式不会生效</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">get_weibo_data</span><span style="color:#999999">(</span>login_weibo<span style="color:#999999">)</span><span style="color:#999999">:</span> <span style="color:#5c6370"># 这种方式才会生效</span>
<span style="color:#669900">"""fixture函数依赖,需要用传递函数的方式"""</span>
<span style="color:#c678dd">print</span><span style="color:#999999">(</span><span style="color:#669900">"=============获取微博数据=============="</span><span style="color:#999999">)</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>mark<span style="color:#999999">.</span>demo</span>
<span style="color:#c678dd">class</span> TestMyCode<span style="color:#999999">:</span>
<span style="color:#999999">@pytest<span style="color:#999999">.</span>mark<span style="color:#999999">.</span>usefixtures</span><span style="color:#999999">(</span><span style="color:#669900">"get_weibo_data"</span><span style="color:#999999">)</span>
<span style="color:#c678dd">def</span> <span style="color:#61aeee">test_fixture_005</span><span style="color:#999999">(</span>self<span style="color:#999999">)</span><span style="color:#999999">:</span>
<span style="color:#669900">"""fixture函数在测试脚本文件中"""</span>
<span style="color:#c678dd">assert</span> <span style="color:#98c379">1</span> <span style="color:#669900">==</span> <span style="color:#98c379">1</span>
</code></span></span>
运行结果:
注意:
①即使fixture函数之间支持相互调用,但普通函数直接使用fixture是不支持的,一定是在测试函数内调用才会逐级调用生效。
②有多层fixture函数调用时,最先执行的是最后一层fixture函数,而不是先执行传入测试函数的fixture函数。
③上层fixture函数的值不会自动return,这里就类似函数相互调用一样的逻辑。【函数调用值需要赋值给一个变量并使用】
感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取