pytest的精髓在这里,可以么说
Pytest之fixture
- fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进
- 有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活
- 按模块化的方式实现,每个fixture都可以互相调用
- fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围
fixtur当做参数传入
- 将login函数直接做为参数传入
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest @pytest.fixture() def login(): print('登录系统') **直接使用函数名做为参数传入** def test_01(login): print('测试用例一') def test_02(): print('测试用例2') def test03(): print('测试用例3')
运行结果
- 只有tes_01调用了login
- 遗留问题来了,如果我这里有10个方法或更多?是不是都需调用login方法?继续看下面的fixture参数
testcase.py::test_01 登录系统 测试用例一 PASSED testcase.py::test_02 测试用例2 PASSED testcase.py::test03 测试用例3 PASSED
fixturep完整语法
# scope有4个作用范围:function(不填则默认)、class、module、session fixture(scope='function', params=None, autouse=False, ids=None, name=None)
参数说明
- scope:即作用域,function"(默认),“class”,“module”,"session"四个
- params:可选参数列表,它将导致多个参数调用fixture函数和所有测试使用它。
- autouse:默认:False,需要用例手动调用该fixture;如果是True,所有作用域内的测试用例都会自动调用该fixture
- ids:params测试ID的一部分。如果没有将从params自动生成.
- name:默认:装饰器的名称,同一模块的fixture相互调用建议写个不同的name。
- session的作用域:是整个测试会话,即开始执行pytest到结束测试
scope参数作用范围控制fixture的作用范围:session>module>class>function
autouse
- 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。
- 当用例很多的时候,每次都传这个参数,会比较麻烦。fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest # 当前就算定义了装饰器,也不会调用Login @pytest.fixture() def login(): print("打开浏览器") def test1(): print("test1里的用例") def test2(): print("test2里的用例")
调用方式1
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest @pytest.fixture() def login(): print("打开浏览器") # 直接传入函数名 def test1(login): print("test1里的用例") def test2(login): print("test2里的用例")
调用方式2
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest # autouse设为True,就能自动调用login的装饰器 @pytest.fixture(autouse=True) def login(): print("打开浏览器") # 直接传入函数名 def test1(): print("test1里的用例") def test2(): print("test2里的用例")
function
- function:作用域为函数
- 所有的方法都调用了login
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest @pytest.fixture(scope='function', autouse=True) def login(): print('登录系统') def test_01(): print('测试用例一') def test_02(): print('测试用例2') def exit(): print("这条用例不会调用login") def test03(): print('测试用例3')
运行结果
- 符合用例名设计的都会调用装饰器
- exit不符合用例名要求,所以不会调用login
testcase.py::test_01 登录系统 测试用例一 PASSED testcase.py::test_02 登录系统 测试用例2 PASSED testcase.py::test03 登录系统 测试用例3 PASSED
class
- class:作用域为类
- 所以TestCase1和TestCase2这两个类都会执行login
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest @pytest.fixture(scope='class', autouse=True) def login(): print('登录系统') def test_01(): print('这个是类外面的用例') class TestCase1: def test_02(self): print('测试用例2') def test03(self): print('测试用例3') class TestCase2: def test_04(self): print('测试用例4') def test05(self): print('测试用例5')
运行结果
- 类里面的方法只会调用一次
- pytest机制,因为方法是以test开头,所以也会调用
testcase.py::test_01 登录系统 这个是类外面的用例 PASSED testcase.py::TestCase1::test_02 登录系统 测试用例2 PASSED testcase.py::TestCase1::test03 测试用例3 PASSED testcase.py::TestCase2::test_04 登录系统 测试用例4 PASSED testcase.py::TestCase2::test05 测试用例5 PASSED
module
- module:在当前.py脚本里面所有用例开始前只执行一次
- 只要符合用例的设计要求,不管是类里和外边的都会调用
# -*- coding: utf-8 -*- # @Author : 码上开始 import pytest @pytest.fixture(scope='class', autouse=True) def open(): print("打开浏览器,并且打开百度首页") def test_s1(): print("用例1:搜索python-1") class TestCase(): def test_s2(self): print("用例2:搜索python-2") def test_s3(self): print("用例3:搜索python-3")
运行结果
- 当前文件里的用例都调用了装饰器
- 如果类名不是为Test开头你试试看是否还会调用装饰器?
testcase.py::test_s1 打开浏览器,并且打开百度首页 用例1:搜索python-1 PASSED testcase.py::TestCase::test_s2 打开浏览器,并且打开百度首页 用例2:搜索python-2 PASSED testcase.py::TestCase::test_s3 用例3:搜索python-3 PASSED
session
- fixture为session级别是可以跨.py模块调用的
- 当我们有多个.py文件的用例时候,如果多个用例只需调用一次fixture,那就可以设置为scope=“session”,并写到conftest.py文件里
- conftest.py文件名称是固定的,pytest会自动识别该文件。放到工程的根目录下,就可以全局调用了
- 如果放到某个package包下,那就只在该package内有效
# -*- coding: utf-8 -*- # @Author : 码上开始 # conftest文件内容 import pytest @pytest.fixture(scope="session", autouse=True) def login(): print("调用conftest文件的里的方法")
# -*- coding: utf-8 -*- # @Author : 码上开始 # testcase1.py import pytest def test1(): print("test1里的用例") def test2(): print("test2里的用例") # -*- coding: utf-8 -*- # testcase1.py import pytest def test3(): print("test3里的用例") def test4(): print("test4里的用例")
运行结果
- 两个文件只有testcase1文件的用例调了conftest里的方法
testcase.py::test1 调用conftest文件的里的方法 test1里的用例 PASSED testcase.py::test2 test2里的用例 PASSED testcase1.py::test3 test3里的用例 PASSED testcase1.py::test4 test4里的用例 PASSED
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!