接口自动化测试框架-fixture函数使用

news2024/11/30 7:40:48

前言

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函数重命名的新名字来调用。

A1

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,这里就类似函数相互调用一样的逻辑。【函数调用值需要赋值给一个变量并使用】

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1815342.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AIOps实现的简单途径

AIOps需要大模型的支持&#xff0c;但是训练一个业务专用的大模型并不是一件理想的任务&#xff0c;所以利用开源的通用大模型才是王道。 我们可以利用AI大模型的理解能力来帮助分析和解释Kubernetes&#xff08;K8s&#xff09;的日志。通过提供日志中可能存在问题的部分&…

莱佛士设计学院师生参观白鹅潭大湾区艺术中心

莱佛士设计学院一直秉持着&#xff0c;不将“学习”局限在课堂或学校的原则。 为了让学生收集“主要课题研究”这门课程的图像和灵感。近日&#xff0c;莱佛士设计学院的Ricky老师带领学生们一起参观白鹅潭大湾区艺术中心。 白鹅潭大湾区艺术中心&#xff0c;这座集广东美术馆…

【HarmonyOS】鸿蒙入门学习

一、开发前的准备 &#xff08;一&#xff09;HarmonyOS 开发套件介绍 &#xff08;二&#xff09;开发者主要使用的核心套件 主要为代码开发阶段会使用到的 DevEco Studio、ArkTS、ArkUI三个工具。 &#xff08;三&#xff09;熟悉鸿蒙官网 1、网址 https://developer.hua…

本地生活服务电商平台小程序源码系统 含完整的安装代码包+搭建教程

系统概述 本地生活服务电商平台小程序源码系统&#xff0c;是一款集成了商品展示、在线交易、服务预约、优惠券发放、会员管理、订单处理、即时通讯等多种功能于一体的综合性解决方案。它旨在为本地商家提供一个高效、便捷的线上经营平台&#xff0c;同时为消费者带来流畅的使…

C++ 中的负无穷大赋值

1&#xff0c;代码先行 示例&#xff1a; #include<iostream> #include<limits>using namespace std;int main() {float inf_pos numeric_limits<float>::infinity();float inf_neg -1*inf_pos;cout << "inf_pos " << inf_pos &l…

Windows系统下CUDA、cuDNN与PyTorch的更新与安装全攻略

Windows系统下CUDA、cuDNN与PyTorch的更新与安装全攻略 文章目录 Windows系统下CUDA、cuDNN与PyTorch的更新与安装全攻略一、引言二、CUDA、cuDNN与PyTorch-GPU介绍三、安装准备1. 查看支持的CUDA版本2. 查看已安装的CUDA版本3. 查看支持的PyTorch版本 四、卸载旧版CUDA五、下载…

【企业家必看】解锁财富新机遇:二人订单共享模式

在这个充满变革与创新的时代&#xff0c;我们有幸向您介绍一种全新的商业模式——二人订单共享模式。这不仅是一次商业创新&#xff0c;更是一次财富与价值共享的革命。 终身消费&#xff0c;终身收益 只需一次499元的终身消费&#xff0c;您即可成为会员。这意味着&#xff0…

游戏试玩站打码平台系统可运营的任务网源码

安装说明 1.恢复数据&#xff1b; 2.数据连接库配置路径&#xff1a;protected\config\mail.php 文件中修改第60行 &#xff08;记得不要用记事本修改&#xff0c;否则可能会出现验证码显示不了问题&#xff0c;建议用Notepad&#xff09; 3.浏览器访问输入 127.0.0.2 显示界…

dvwa SQl注入中级、高级(使用sqlmap渗透)

sql注入中级 发现是一个表单提交数据 点提交 改为2&#xff0c;再点提交 burp抓包 发现是以post的方式传递 这里尝试以两种方式解题 &#xff08;1&#xff09;-r “txt”的方式 python sqlmap.py -r ""C:\Users\fuqia\Desktop\1.txt"" -p id --batch …

为CAP面板天添加简单的认证功能 C#|.net

做过后端的比较熟悉,CAP面板有个界面,可以通过域名加cap访问: 但是这个面板直接通过url就可以访问了。 Hangfire Dashboard有自己的面板,可以使用用户名和密码做简单的认证。 LogDashboard也有自己的面板,可以使用用户名和密码做简单的认证。 如下图: 但是CAP的面板是…

nodejs湖北省智慧乡村旅游平台-计算机毕业设计源码00232

摘 要 随着科学技术的飞速发展&#xff0c;社会的方方面面、各行各业都在努力与现代的先进技术接轨&#xff0c;通过科技手段来提高自身的优势&#xff0c;旅游行业当然也不能排除在外。智慧乡村旅游平台是以实际运用为开发背景&#xff0c;运用软件工程开发方法&#xff0c;采…

JAVA代码审计之文件上传代码审计

1 文件上传简介 1.1 什么是任意文件上传漏洞 任意文件上传漏洞常发生在文件上传功能中&#xff0c;由于后端代码中没有严格限制用户上传的文件&#xff0c;导致攻击者可以上传带有恶意攻击代码的JSP 脚本到目标服务器&#xff0c;进而执行脚本&#xff0c;以达到控制操纵目标…

cesium 渐变虚线效果 PolylineDashMaterialProperty

cesium中有虚线材质PolylineDashMaterialProperty&#xff0c;可以在这个材质的基础上结合uv设置每个顶点的透明度&#xff0c;就能实现渐变的效果了。 一、原理&#xff1a;在glsl中结合uv设置每个顶点的透明度 vec2 st materialInput.st; material.alpha fragColor.a * (1…

【SpringCloud】创建新工程

前言 本文使用的是jdk17&#xff0c;mysql8。 以下用两个服务做演示&#xff1a; 订单服务&#xff1a;提供订单ID&#xff0c;获取订单详细信息。 商品服务&#xff1a;提供商品ID&#xff0c;获取商品详细信息。 对于订单服务和商品服务分别建立数据库&#xff1a; -- 订单服…

Javaweb避坑指北(持续更新)

内容较多可按CtrlF搜索 0.目录 1.获取插入数据后自增长主键的值 2.Controller中返回给ajax请求字符串/json会跳转到xxx.jsp 3.ajax请求获得的json无法解析 4.在Controller中使用ServletFileUpload获取的上传文件为null 5.莫名其妙报service和dao里方法的错误 6.ajax请求拿…

期权费的核心是什么

期权费的核心 在涉足股票期权投资的领域时&#xff0c;投资者们往往会将焦点首先锁定在期权费这一显性成本上。然而&#xff0c;实际操作中&#xff0c;一个更为核心且决定性的考量因素是&#xff0c;目标股票是否具备通过个股期权策略进行买入的潜在价值。特别是&#xff0c;…

NFTScan 正式上线 Sei NFTScan 浏览器和 NFT API 数据服务

2024 年 6 月 12 号&#xff0c;NFTScan 团队正式对外发布了 Sei NFTScan 浏览器&#xff0c;将为 Sei 生态的 NFT 开发者和用户提供简洁高效的 NFT 数据搜索查询服务。NFTScan 作为全球领先的 NFT 数据基础设施服务商&#xff0c;Sei 是继 Bitcoin、Ethereum、BNBChain、Polyg…

数据安全“大考”,双一流高校们来交卷!

又是一年高考落幕 埋头题山卷海的考生们 终于迎来“大解放”&#xff01; 在数字化浪潮中 众高校面临的重要考试 ——“数据安全大考”还在继续 如何作答&#xff1f; 这些双一流高校交出高分答卷&#xff01; 数据分类分级如何开展落地&#xff1f; 难点分析&#xff1…

python游标卡尺什么梗

python游标卡尺什么梗&#xff1f;下面给大家介绍一下这个梗&#xff1a; 因为 python 是通过缩进来区分代码块的&#xff08;而不是{}或者是其他符号&#xff09;&#xff0c;而不是像其他程序语言&#xff0c;缩进仅仅为了可读性。导致打印出来的 python 代码&#xff08;比…

神经气体生长算法【GNG】

当德国计算神经学家 Bernd Fritzke 在其 1995 年的开创性论文中提出后来被称为神经气体生长&#xff08;GNG&#xff09;的算法时&#xff0c;机器学习还是一个相对较新的领域&#xff0c;并且受到实际神经科学的极大启发。 当时&#xff0c;神经科学正处于一个突破性的时代—…