fixture的优势
pytest框架的fixture测试夹具就相当于unittest框架的setup、teardown,但相对之下它的功能更加强大和灵活。
- 命名方式灵活,不限于unittest的setup、teardown
- 可以实现数据共享,多个模块跨文件共享前置后置
- 可以实现多个模块跨文件使用一个session来完成多个用例
- 可以实现unittest不能实现的功能,比如unittest中的测试用例和测试用例之间是无法传递参数和数据的,但是fixture却可以解决这个问题
fixture定义及调用
关键代码:@pytest.fixture()
,用于声明函数是处理前置后置的测试夹具函数。用法如下:
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#61aeee">@pytest.fixture()</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">my_fixture</span>():</span> <span style="color:#b18eb1"><em># 记住这个夹具名</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"我的测试夹具"</span>)</span></span>
测试夹具已经定义好了,那测试用例如何调用呢?有以下三种方式:
- 方式一:将fixture名称作为参数传给测试用例,可以传多个fixture,按先后顺序执行
- 方式二:装饰器:
@pytest.mark.usefixtures(fixture_name)
- 使用在类上,代表这个类所有测试用例都会调用这个fixture
- 使用在用例上,代表只有这个用例调用这个fixture
- 同样可以使用多个fixture,按先后顺序执行
- 如果fixture有返回值,用这个装饰器是无法获取到返回值的,也就无法给用例传递数据,只能用方法一
- 方式三:fixture设置
autouse=True
自动调用,同样fixture有返回值时,无法获取返回值
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#f92672">import</span> pytest
<span style="color:#61aeee">@pytest.fixture()</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">my_fixture</span>():</span> <span style="color:#b18eb1"><em># 记住这个夹具名</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"我的测试夹具"</span>)
<span style="color:#b18eb1"><em># 方式一</em></span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_fix1</span>(<span style="color:#a6e22e">my_fixture</span>):</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"这是测试用例1111"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"-------分割线------"</span>)
<span style="color:#b18eb1"><em># 方式二</em></span>
<span style="color:#b18eb1"><em># 类中应用</em></span>
<span style="color:#61aeee">@pytest.mark.usefixtures(<span style="color:#3388aa">"my_fixture"</span>)</span>
<span style="color:#f92672">class</span> <span style="color:#e6c07b">TestLogin</span>:
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_fix2</span>(<span style="color:#a6e22e">self</span>):</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"这是测试用例2222"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"-------分割线------"</span>)
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_fix3</span>(<span style="color:#a6e22e">self</span>):</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"这是测试用例3333"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"-------分割线------"</span>)
<span style="color:#b18eb1"><em># 测试用例应用</em></span>
<span style="color:#61aeee">@pytest.mark.usefixtures(<span style="color:#3388aa">"my_fixture"</span>)</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_fix4</span>():</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"这是测试用例4444"</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"-------分割线------"</span>)
<span style="color:#b18eb1"><em># 方式三</em></span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_fix5</span>():</span> <span style="color:#b18eb1"><em># 未声明使用测试夹具</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"这是测试用例5555"</span>)
<span style="color:#f92672">if</span> __name__ == <span style="color:#98c379">"__main__"</span>:
pytest.main()</span></span>
运行结果:
<span style="background-color:#282c34"><span style="color:#abb2bf">Testing started at 23:12 ...
C:\software\python\python.exe ...
test.py 我的测试夹具
.这是测试用例1111
-------分割线------
我的测试夹具
.这是测试用例2222
-------分割线------
我的测试夹具
.这是测试用例3333
-------分割线------
我的测试夹具
.这是测试用例4444
-------分割线------
.这是测试用例5555
[100%]
============================== 5 passed in 0.02s ==============================
Process finished with exit code 0</span></span>
为什么方式三没有用到测试夹具呢,因为还没有设置测试夹具自动调用。fixture里面有个参数autouse
(自动使用的意思),默认是False,当设置为True时,用例就会自动调用该fixture功能,这样的话写用例时就不用每次都去传参了。
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#61aeee">@pytest.fixture(autouse=<span style="color:#56b6c2">True</span>) </span><span style="color:#b18eb1"><em># 设置用例自动调用该fixture</em></span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">my_fixture</span>():</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"我的测试夹具"</span>)
</span></span>
fixture作用域
在unittest中还有一个setUpClass和tearDownClass,是作用于类上,在每个测试用例类执行前去执行前置,用例类执行完再执行后置,pytest的fixture同样有这样的作用域,且使用更广泛更灵活。
关键代码:@pytest.fixture(scope='作用范围')
,参数如下:
function
:默认作用域,每个测试用例都运行一次class
:每个测试类只执行一次-
module
:每个模块只执行一次 -
package
:每个python包只执行一次 -
session
:整个会话只执行一次,即运行项目时整个过程只执行一次
优先级:session > package > module > class > function,优先级高的会在低的fixture之前先实例化。
相同作用域的fixture遵循函数中声明的先后顺序,并遵循fixture之间的依赖关系,如在fixture_A里面依赖的fixture_B优先实例化,然后再到fixture_A实例化
我们前面举例fixture的使用时都是使用的默认作用域,下面举例一下作用域为class的场景:
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#b18eb1"><em># 因为用于演示,因此测试夹具直接写在py文件中</em></span>
<span style="color:#f92672">import</span> pytest
<span style="color:#f92672">from</span> selenium <span style="color:#f92672">import</span> webdriver
<span style="color:#61aeee">@pytest.fixture(scope=<span style="color:#3388aa">'class'</span>)</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">my_fixture</span>():</span>
<span style="color:#98c379">"""前置条件"""</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"前置条件-启动浏览器"</span>)
driver = webdriver.Chrome()
<span style="color:#f92672">yield</span> driver
driver.quit()
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"后置条件-关闭浏览器"</span>)
<span style="color:#f92672">class</span> <span style="color:#e6c07b">TestCase</span>:
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_case01</span>(<span style="color:#a6e22e">self, my_fixture</span>):</span> <span style="color:#b18eb1"><em># 这里通过参数传入my_fixture函数,用例执行前会先去执行my_fixture</em></span>
driver = my_fixture <span style="color:#b18eb1"><em># my_fixture不需要加括号</em></span>
driver.get(<span style="color:#98c379">'http://www.baidu.com'</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">'第一个用例'</span>)
<span style="color:#f92672">assert</span> <span style="color:#d19a66">1</span> == <span style="color:#d19a66">1</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_case02</span>(<span style="color:#a6e22e">self, my_fixture</span>):</span> <span style="color:#b18eb1"><em># 这里通过参数传入my_fixture函数,用例执行前会先去执行my_fixture</em></span>
driver = my_fixture <span style="color:#b18eb1"><em># my_fixture不需要加括号</em></span>
driver.get(<span style="color:#98c379">'http://www.cnblogs.com/'</span>)
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">'第二个用例'</span>)
<span style="color:#f92672">assert</span> <span style="color:#d19a66">1</span> == <span style="color:#d19a66">1</span>
<span style="color:#f92672">if</span> __name__ == <span style="color:#98c379">'__main__'</span>:
pytest.main([<span style="color:#98c379">'test.py'</span>, <span style="color:#98c379">'-s'</span>])</span></span>
运行结果如下,从运行结果可以看出,整个类只打开了一次浏览器。
<span style="background-color:#282c34"><span style="color:#abb2bf">C:\software\python\python.exe D:/learn/test.py
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: D:\learn
plugins: html-2.0.0, metadata-1.8.0
collected 2 items
test.py 前置条件-启动浏览器
第一个用例
.第二个用例
.后置条件-关闭浏览器
============================== 2 passed in 9.76s ==============================
Process finished with exit code 0
</span></span>
前面也提到了fixture的另外一个参数autouse,当autouse=True
时,用例会自动执行测试夹具,但无法获取fixture的返回值,如上示例返回的driver就无法传递给用例了。
如果你想让用例自动执行测试夹具且希望driver等参数可以返回给用例时,可以试一下在测试夹具中使用yield
关键字返回值时把值存储到临时变量中,再让用例去取临时变量中的值,这里不作举例,以下是autouse=True
的一个简单示例:(yield
关键字在后面的章节会讲解)
<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#b18eb1"><em># 因为用于演示,因此测试夹具直接写在py文件中</em></span>
<span style="color:#f92672">import</span> pytest
<span style="color:#f92672">from</span> selenium <span style="color:#f92672">import</span> webdriver
<span style="color:#61aeee">@pytest.fixture(scope=<span style="color:#3388aa">'class'</span>, autouse=<span style="color:#56b6c2">True</span>) </span><span style="color:#b18eb1"><em># 所有类自动执行该测试夹具</em></span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">my_fixture</span>():</span>
<span style="color:#98c379">"""前置条件"""</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"前置条件-启动浏览器"</span>)
driver = webdriver.Chrome()
<span style="color:#f92672">yield</span> driver
driver.quit()
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">"后置条件-关闭浏览器"</span>)
<span style="color:#f92672">class</span> <span style="color:#e6c07b">TestCase</span>:
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_case01</span>(<span style="color:#a6e22e">self</span>):</span> <span style="color:#b18eb1"><em># 不需要传入测试夹具</em></span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">'第一个用例'</span>)
<span style="color:#f92672">assert</span> <span style="color:#d19a66">1</span> == <span style="color:#d19a66">1</span>
<span style="color:#61aeee"><span style="color:#f92672">def</span> <span style="color:#61aeee">test_case02</span>(<span style="color:#a6e22e">self</span>):</span>
<span style="color:#e6c07b">print</span>(<span style="color:#98c379">'第二个用例'</span>)
<span style="color:#f92672">assert</span> <span style="color:#d19a66">1</span> == <span style="color:#d19a66">1</span>
<span style="color:#f92672">if</span> __name__ == <span style="color:#98c379">'__main__'</span>:
pytest.main([<span style="color:#98c379">'test.py'</span>, <span style="color:#98c379">'-s'</span>])</span></span>