1 安装Pytest
通过pip 安装
pip install -U pytest
通过Pycharm安装
备注
由于pip自带的仓库源为国外地址,可以换成国内地址
(1)在windows文件管理器中,输入 %APPDATA%
(2)新建文件夹,命名为pip
(3)文件夹pip内新建文本,命名为pip,输入一下内容
[global]
timeout=40
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url=
http://mirrors.aliyun.com/pypi/simple/
http://pypi.douban.com/simple
http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host=
pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
pypi.douban.com
pypi.mirrors.ustc.edu.cn
(4)将文本pip的属性修改为 .ini
pip show pytest #查看当前pytest的安装版本
2 pytest用例规则
- 文件名以test_*.py文件和*_test.py
- 以test_开头的函数
- 以Test开头的类,test_开头的方法,并且不能带有__init__ 方法
- 所有的包pakege必须要有__init__.py文件
- 断言使用assert
3 前置和后置
前/后置类型
-
模块级(setup_module/teardown_module)开始于模块始末,全局的
-
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
-
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
-
方法级(setup_method/teardown_method)开始于方法始末(在类中)
-
类里面的(setup/teardown)运行在调用方法的前后
模块级和函数级
import pytest
def setup_module():
print("==========setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器")
def teardown_module():
print("==========teardown_module:整个.py模块只执行一次")
print("比如:所有用例结束只最后关闭浏览器")
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束前都会执行")
def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在执行----test_two")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_04.py"])
4 fixture
什么是fixture
为可靠的和可重复执行的测试提供固定的基线(可以理解为测试的固定配置,使不同范围的测试都能够获得统一的配置),fixture提供了区别于传统单元测试(setup/teardown)风格的令人惊喜的功能,而且pytest做得更炫。
fixture的作用范围
fixture里面有个scope参数可以控制fixture的作用范围,当多个参数范围重叠时,会根据优先级执行:session>module>class>function。fixture默认是function
function:每一个函数和方法都会调用
class:每一个类调用一次,一个类有多个方法
module:每一个.py文件调用一次,该文件又有多个function和class
session:多个文件调用一次,可以跨.py文件,每个.py文件就是一个module
import pytest
@pytest.fixture
def test_01():
return 6
class TestNum:
def test_02(self, test_01): #此函数调用fixture
a=test_01
assert a < 7
print("===>>>>>>>断言成功")
if __name__ == "__main__":
pytest.main(["-s", "test_04.py"])
fixture参数化
单个参数
import pytest
data = ["https://www.baidu.com", "https://www.google.com"]
@pytest.fixture(scope='function', params=data)
def f_function(request):
return request.param
def test_ceshi(f_function):
print(f'测试用例:{f_function}')
if __name__ == "__main__":
pytest.main(["-s", "test_04.py"])
'''
@pytest.fixture中默认使用request.param接收一个参数序列(列表或元组都可以)。request是内置的fixture之一,它的一个属性param可以每次被传入params序列中的一个元素。所以data列表中有两个元素,此用例便执行了两次
'''
多个参数
fixture的参数params接受的是一个参数序列,那么字典列表、字典元组这些都是序列,所以,我们可以借助字典对多个参数进行参数化。同时ids可以为每组数据打上标识。
fixture使用yield达到teardown的效果
import pytest
from pytest import fixture
@fixture(scope="module")
def open():
print("\n打开浏览器,并且打开百度首页")
yield
print("执行teardown!")
print("最后关闭浏览器")
def test_s1(open):
print("用例1:搜索python-1")
def test_s2(open):
print("用例2:搜索python-2")
def test_s3(open):
print("用例3:搜索python-3")
if __name__ == "__main__":
pytest.main(["-s", "test_04.py"])
5 parametrize参数化
pytest.mark.parametrize装饰器可以实现测试用例参数化。
import pytest
@pytest.mark.parametrize("account,pwd",[("cc",123),("tt",321)])
def test_login_success(account,pwd):
print(f'账户:{account} , 密码:{pwd}')
if __name__ == "__main__":
pytest.main(["-s", "test_04.py"])
#**传入的数据必须为列表,若是字符串,会被转换成列表。**
6 assert断言
常用断言
-
assert xx 判断xx为真
-
assert not xx 判断xx不为真
-
assert a in b 判断b包含a
-
assert a == b 判断a等于b
-
assert a != b 判断a不等于b
import pytest
def test_zero_division():
'''断言异常'''
with pytest.raises(ZeroDivisionError) as excinfo:
1 / 0
# 断言异常类型type
assert excinfo.type == ZeroDivisionError
# 断言异常value值
print("\n",str(excinfo.value))
print(str(excinfo.type))
print(str(excinfo.traceback))
assert "division by zero" in str(excinfo.value)
'''
excinfo 是一个异常信息实例,它是围绕实际引发的异常的包装器。
主要属性是 .type、 .value 和 .traceback
注意:断言type的时候,异常类型是不需要加引号的,断言value值的时候需转str
'''
7 配置文件pytest.ini
pytest.ini是pytest的主要配置文件,可以改变pytest的默认行为。
mark标记可以针对class、function、module等。
mark标记主要是用来分类测试。
addopts:运行时参数(可添加多个命令行参数,空格分隔,所有参数与命令行一致)
常用参数
-
-s 详细输出
-
-v 输出具体运行的case
-
-q 简要输出
-
-m 指定标签(pytest.mark.标签)
-
-k 模糊匹配,测试用例的部分字符串,指定执行测试用例
-
-x:表示只要有一个测试用例报错,则执行停止
-
–maxfail=2:表示出现2个用例报错,则执行停止
-
-reruns X:失败用来重跑,跑几次。
8 Xdist分布式运行
正常模式下Pytest的用例都是一条一条执行的。但用例条数过多时,一条条的执行所需要的时间过长。Xdist分布式插件可以采用并行执行的方法减少测试用例执行的时间。
环境安装
pip install pytest-xdist
使用的命令参数
要执行分布式运行,在运行命令后加参数即可。
1.pytest -n num(CPU的数量) #这里是需要使用的CPU数量
2.pytest -n auto #如果传auto 的话啊,它会自动检测系统cpu的数量,并且用来运行测试。所以此模式下CPU负载较高