pytest单元测试框架
单元测试
(1)单元测试是指在软件开发的过程中,针对软件的最小单位(函数、方法)进行的测试
(2)python单元测试的框架:
> unittest
> pytest
> nose
unittest是python内置的单元测试框架,和安装的python无兼容性的问题
pytest是第三方测试框架,需要手动安装
单元测试框架和自动化测试框架关系
自动化测试框架提供可重用的基础自动化测试模块
自动化测试框架的作用:
1.提高测试效率,降低成本
2.减少人工干预,提高测试的准确性,增加代码的重用性
pytest单元测试框架和自动化测试框架的关系:
1.单元测试框架,是自动化测试框架的组成部分之一
2.数据驱动测试
3.关键字测试
4.自动化测试报告和邮件
5.日志封装和配置文件封装
6.断言…
pytest框架
pytest简介:
1.pytest实现了测试用例的灵活筛选和管理
2.pytest可以和selinum,requests、appiunm结合实现web自动化,接口自动化和app自动化
3. pytest可以实现失败用例的重跑
4. 可以结合allure实现精美的测试报告
5. 和jenkines结合实现持续集成
6. pytest有非常强大的插件
pytest-html(生成html的测试报告)
Pytest-xdist(测试用例分布式执行,多cpu分发)
Pytest-ording(测试用例按照顺序执行)
Pytest-rerunfailures(失败用例重跑)
Allure-pytest(生成美观的测试报告)
pytest用例编写规则
1.模块是以test开头
2.函数以test开头
3.如果写在类下面,类需要以大写的Test开头,且类下面的方法以test开头
1.测试用例函数里面不能有return
2.在测试用例函数里面,不能随意去调用函数
3.测试用例函数里面不要随意增加参数
pytest安装:
1.在线安装:pip install pytest
2.更新安装:pip instal pytest -u
pytest用例运行方式:
1.命令行模式:
(1)运行所有:进入到对应目录:执行pytest
(2)指定模块:pytest -vs xxx.py
(3)执行目录:pytest -vs 目录
(4)运行带有标记的用例:pytest -vs xxx.py -m 标记名
2.主函数模式:
(1)运行所有的用例:pytest.main()
(2)运行指定的模块:pytest.main([’-vs’,‘xxx.py’])
(3)指定目录:pytest.main([’-vs’,’./目录’])
(4)运行带有标记的用例:pytest.main([’ -vs’, ‘xxx.py’, ‘-m 标记名’])
(5)运行的时候需要在pycharm最后面顶格运行
注:
-v显示更详细的信息
-s 小时输出用例中调试的信息,包括print()打印的内容
-vs 参数结合使用
pytest标签
标签的注册
在当前项目根目录新建一个pytest.ini文件,文件格式如下:
[pytest]
markers=
smoke
qita
上述smoke,qita都是标签名
标签的使用
标签注册好之后,可以在测试用例函数前面加上标签名,格式:@pytest.mark.标记名
@pytest.mark.smoke
def test_add():
assert (add(4,6)==10)
运行带有标签的用例
运行带有标记的用例:pytest -m ‘标记名’
pytest xxx.py -m 标签名
pytest测试html报告
安装
pip install pytest-html
生成测试报告
pytest -html=xx.html
数据驱动测试:
定义:数据驱动测试,也叫做ddt,data driven testing
格式:
@pytest.mark.parametrize(字符串,参数),字符串负责接收后面参数的值,如下例子:
data=[{},{},{}]
@pytest.mark.parameritize(‘info’,data)
def test_001(info):
pass
数据驱动测试作用:
1.将数据和测试逻辑分离
2.保证测试用例的独立性
3.方便测试数据的维护
案例:
import pytest
#定义登录函数
def login(username,password):
"""
:param username: 用户名
:param password: 密码
:return:
"""
if username=='feier' and password=='123456':
return '登录成功'
return '登录失败'
#设计测试用例
data=[{"username":"yuze","password":"123456","expect":"登录成功"},
{"username":"","password":"","expect":"登录失败"},
{"username":"haha","password":"123456","expect":"登录失败"},
{"username":"yuze","password":"111111","expect":"登录失败"},
{"username":"aa","password":"111111","expect":"登录失败"}
]
@pytest.mark.parametrize('info',data)
def test_login(info):
u=info['username']
p=['password']
expect=['expect']
assert expect==login(u,p)
测试夹具:
定义:测试用例的前置和后置条件
格式:@pytes.fixture
import pytest
@pytest.fixture()
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
在测试用例中的应用:
def test_001(function_before):
print(function_before)
assert 1+1==3
fixtrue的用法 :
1.夹具本身是一个函数,夹具函数不能以test开头,不能作为测试用例使用
2.声明夹具函数:@pytest.fixtrue
3.将夹具函数作为参数传递给测试用例函数,如果测试用例函数不需要前置条件,就不需要传递
4.如何接受测试夹具中的值,打印夹具名称,会把返回值给测试用例函数,使用夹具名称可以直接获取返回值
fixtrue的作用域 :
使用scope设置作用域,scope默认值是function
1.函数作用域:将夹具函数放在测试用例函数中,传递了夹具函数的测试用例都会执行夹具函数里面的内容
@pytest.fixture(scope=’function’)
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
测试用例:
def test_001(function_before):
print(function_before)
assert 1+1==3
2.类作用域:@Pytest.fixtrue(Scope=class),整个类,不管有多少个用例,整个类都会执行一次
import pytest
@pytest.fixture(scope='class')
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
测试类:
class Test_user:
def test_user(self,function_before):
pass
def test_user2(self,function_before):
pass
运行图:尽管在每个方法中都设置了前置条件,但是前置条件和后置条件只执行一次
3.模块作用域, @Pytest.fixtrue(Scope=module)
整个文件,不管有多少个用例,只会执行一次
import pytest
@pytest.fixture(scope='module')
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
测试模块:
#-*-coding:utf-8-*-
'''
@Time :2021/1/8 0008下午 9:11
@Author : guojuanjuan
@File :test_demo.py
@Software :PyCharm
'''
import pytest
@pytest.fixture(scope='module')
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
#定义登录函数
def login(username,password):
"""
:param username: 用户名
:param password: 密码
:return:
"""
if username=='yuze' and password=='123456':
return '登录成功'
return '登录失败'
#设计测试用例
data=[{"username":"yuze","password":"123456","expect":"登录成功"},
{"username":"","password":"","expect":"登录失败"},
{"username":"haha","password":"123456","expect":"登录失败"},
{"username":"yuze","password":"111111","expect":"登录失败"},
{"username":"aa","password":"111111","expect":"登录失败"}
]
@pytest.mark.parametrize('info',data)
def test_login(info,function_before):
u=info['username']
p=info['password']
expect=info['expect']
assert login(u,p)==expect
#传入前置条件参数
def test_001(function_before):
#如何接受测试夹具中的值,打印夹具名称,会把返回值给函数
print(function_before)
assert 1+1==3
class Test_user:
def test_user(self,function_before):
pass
def test_user2(self,function_before):
pass
运行图:
4.session:@Pytest.fixtrue(Scope=session)
在文件夹执行pytest时,有多个模块时,前置条件和后置条件只运行一次
#encoding:utf-8
#测试夹具函数conftest.py
import pytest
@pytest.fixture(scope=’module’)
def function_before():
print('用例前置条件')
yield 'chrom'
print('用例后置条件')
5.autouse默认值是True,所有的函数都会自动调用夹具,不需要传夹具的参数名称了,可以自动调用
@pytest.fixture(scope='function',autouse=True)
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
测试用例:
def test_001():
assert 1+1==3
6.conftest.py
一般要将夹具函数放在conftest.py文件中,专门放置夹具函数的文件,一般放到项目的根目,方便测试夹具的维护
#conftest.py
import pytest
@pytest.fixture()
def function_before():
print('用例前置条件')
yield 'chrom'
#return '测试'
print('用例后置条件')
def test_001(function_before):
assert 1+1==3
allure:
安装
- 使用pip install allure-pytest安装
2.下载allure-2.13.8.zip文件,解压到对应的路径,将文件对应的bin目录配置到环境变量中
3.在控制台运行allure,出现如下场景,说明allure安装成功
4.运行测试用例:
pytest --alluredir=目录
5.生成报告:
allure serve 目录
报告如下图: