前言
学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unittest是没什么区别的(个人理解)。
fixture用途
1.做测试前后的初始化设置,如测试数据准备,链接数据库,打开浏览器等这些操作都可以使用fixture来实现
2.测试用例的前置条件可以使用fixture实现
3.支持经典的xunit fixture ,像unittest使用的setup和teardown
4.fixture可以实现unittest不能实现的功能,比如unittest中的测试用例和测试用例之间是无法传递参数和数据的,但是fixture却可以解决这个问题
fixture定义
fixture通过@pytest.fixture()装饰器装饰一个函数,那么这个函数就是一个fixture,看个实例
1
2
3
4
5
6
7
8
9
|
# test_fixture.py import pytest @pytest .fixture() def fixtureFunc(): return 'fixtureFunc' def test_fixture(fixtureFunc): print ( '我调用了{}' . format (fixtureFunc)) if __name__ = = '__main__' : pytest.main([ '-v' , 'test_fixture.py' ]) |
执行结果
1
2
3
4
|
test_fixture.py .我调用了fixtureFunc [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 1 passed in 0.02 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 |
fixtureFunc 这个函数就是一个fixture,fixture函数内部可以实现一些初始化操作!
fixture使用
调用fixture有三种方式
方式1
fixture的名字直接作为测试用例的参数,上面的实例就这这种方式,再来看一个实例
1
2
3
4
5
6
7
8
9
10
11
12
|
# test_fixture.py import pytest @pytest .fixture() def fixtureFunc(): return 'fixtureFunc' def test_fixture(fixtureFunc): print ( '我调用了{}' . format (fixtureFunc)) class TestFixture( object ): def test_fixture_class( self , fixtureFunc): print ( '在类中使用fixture "{}"' . format (fixtureFunc)) if __name__ = = '__main__' : pytest.main([ '-v' , 'test_fixture.py' ]) |
方式2
每个函数或者类前使用@pytest.mark.usefixtures('fixture')装饰器装饰
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# test_fixture.py import pytest @pytest .fixture() def fixtureFunc(): print ( '\n fixture->fixtureFunc' ) @pytest .mark.usefixtures( 'fixtureFunc' ) def test_fixture(): print ( 'in test_fixture' ) @pytest .mark.usefixtures( 'fixtureFunc' ) class TestFixture( object ): def test_fixture_class( self ): print ( 'in class with text_fixture_class' ) if __name__ = = '__main__' : pytest.main([ '-v' , 'test_fixture.py' ]) |
方式3
指定fixture的参数autouse=True这样每个测试用例会自动调用fixture(其实这里说的不是很准确,因为还涉及到fixture的作用范围,那么我们这里默认是函数级别的,后面会具体说fixture的作用范围)
实例
1
2
3
4
5
6
7
8
9
10
11
12
|
# test_fixture.py import pytest @pytest .fixture(autouse = True ) def fixtureFunc(): print ( '\n fixture->fixtureFunc' ) def test_fixture(): print ( 'in test_fixture' ) class TestFixture( object ): def test_fixture_class( self ): print ( 'in class with text_fixture_class' ) if __name__ = = '__main__' : pytest.main([ '-v' , 'test_fixture.py' ]) |
结果
1
2
3
4
5
6
|
fixture - >fixtureFunc . in test_fixture fixture - >fixtureFunc . in class with text_fixture_class [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.04 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
从结果可以看到每个测试用例执行前都自动执行了fixture
小结
掌握上面的方法,就可以使用fixture了,那么这几种方式又有是区别呢? 其实从我写的代码中就能看出来, 如果测试用例需要使用fixture中返回的参数,那么通过后面这两种方式是无法使用返回的参数的,因为fixture中返回的数据默认存在fixture名字里面存储,所以只能使用第一种方式才可以调用fixture中的返回值。(理论永远是理论,看文章的老铁还是自己试试吧!)
fixtur作用范围
上面所有的实例默认都是函数级别的,所以测试函数只要调用了fixture,那么在测试函数执行前都会先指定fixture。说到作用范围就不得不说fixture 的第二个参数scope参数。
scope参数可以是session, module,class,function; 默认为function
1.session 会话级别(通常这个级别会结合conftest.py文件使用,所以后面说到conftest.py文件的时候再说)
2.module 模块级别: 模块里所有的用例执行前执行一次module级别的fixture
3.class 类级别 :每个类执行前都会执行一次class级别的fixture
4.function :前面实例已经说了,这个默认是默认的模式,函数级别的,每个测试用例执行前都会执行一次function级别的fixture
下面我们通过一个实例具体看一下 fixture的作用范围
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# test_fixture.py import pytest @pytest .fixture(scope = 'module' , autouse = True ) def module_fixture(): print ( '\n-----------------' ) print ( '我是module fixture' ) print ( '-----------------' ) @pytest .fixture(scope = 'class' ) def class_fixture(): print ( '\n-----------------' ) print ( '我是class fixture' ) print ( '-------------------' ) @pytest .fixture(scope = 'function' , autouse = True ) def func_fixture(): print ( '\n-----------------' ) print ( '我是function fixture' ) print ( '-------------------' ) def test_1(): print ( '\n 我是test1' ) @pytest .mark.usefixtures( 'class_fixture' ) class TestFixture1( object ): def test_2( self ): print ( '\n我是class1里面的test2' ) def test_3( self ): print ( '\n我是class1里面的test3' ) @pytest .mark.usefixtures( 'class_fixture' ) class TestFixture2( object ): def test_4( self ): print ( '\n我是class2里面的test4' ) def test_5( self ): print ( '\n我是class2里面的test5' ) if __name__ = = '__main__' : pytest.main([ '-v' , '--setup-show' , 'test_fixture.py' ]) |
运行结果
我们在cdm里面执行使用 --setup-show 可以查看到具体setup和teardoen顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
test_fixture.py SETUP M module_fixture SETUP F func_fixture - - - - - - - - - - - - - - - - - 我是module fixture - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 我是function fixture - - - - - - - - - - - - - - - - - - - test_fixture.py::test_1 (fixtures used: func_fixture, module_fixture). 我是test1 TEARDOWN F func_fixture SETUP C class_fixture SETUP F func_fixture - - - - - - - - - - - - - - - - - 我是 class fixture - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 我是function fixture - - - - - - - - - - - - - - - - - - - test_fixture.py::TestFixture1::test_2 (fixtures used: class_fixture, func_fixture, module_fixture). 我是class1里面的test2 TEARDOWN F func_fixture SETUP F func_fixture - - - - - - - - - - - - - - - - - 我是function fixture - - - - - - - - - - - - - - - - - - - test_fixture.py::TestFixture1::test_3 (fixtures used: class_fixture, func_fixture, module_fixture). 我是class1里面的test3 TEARDOWN F func_fixture TEARDOWN C class_fixture SETUP C class_fixture SETUP F func_fixture - - - - - - - - - - - - - - - - - 我是 class fixture - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 我是function fixture - - - - - - - - - - - - - - - - - - - test_fixture.py::TestFixture2::test_4 (fixtures used: class_fixture, func_fixture, module_fixture). 我是class2里面的test4 TEARDOWN F func_fixture SETUP F func_fixture - - - - - - - - - - - - - - - - - 我是function fixture - - - - - - - - - - - - - - - - - - - test_fixture.py::TestFixture2::test_5 (fixtures used: class_fixture, func_fixture, module_fixture). 我是class2里面的test5 TEARDOWN F func_fixture TEARDOWN C class_fixture TEARDOWN M module_fixture = = = = = = = = = = = = = = = = = = = = = = = = = = 5 passed in 0.05 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
我们可以很清楚的看到 整个模块只执行了一次module级别的fixture , 每个类分别执行了一次class级别的fixture, 而每一个函数之前都执行了一次function级别的fixture
fixture实现teardown
其实前面的所有实例都只是做了测试用例执行之前的准备工作,那么用例执行之后该如何实现环境的清理工作呢?这不得不说yield关键字了,相比大家都或多或少的知道这个关键字,他的作用其实和return差不多,也能够返回数据给调用者,唯一的不同是被掉函数执行遇到yield会停止执行,接着执行调用处的函数,调用出的函数执行完后会继续执行yield关键后面的代码(具体原理可以看下我之前的文章关于生成器)。看下下面的实例来了解一下如何实现teardown功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import pytest from selenium import webdriver import time @pytest .fixture() def fixtureFunc(): '''实现浏览器的打开和关闭''' driver = webdriver.Firefox() yield driver driver.quit() def test_search(fixtureFunc): '''访问百度首页,搜索pytest字符串是否在页面源码中''' driver = fixtureFunc driver.get( 'http://www.baidu.com' ) driver.find_element_by_id( 'kw' ).send_keys( 'pytest' ) driver.find_element_by_id( 'su' ).click() time.sleep( 3 ) source = driver.page_source assert 'pytest' in source if __name__ = = '__main__' : pytest.main([ '--setup-show' , 'test_fixture.py' ]) |
这个实例会先打开浏览器,然后执行测试用例,最后关闭浏览器。大家可以试试! 通过yield就实现了 用例执行后的teardown功能
总结
1.fixture如何定义
2.fixture的使用方式
3.fixture作用范围
4.fixture用yield实现teardown功能
最后提一句:实际工作中尽量少用auto=True这个参数,可能会引发意想不到的结果! 最常用的还是通过传递参数最好!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-fixture.html