Pytest使用

1. Pytest安装

1.1 pip安装

pip install pytest
也可以选择进入pycharm进行安装

1.2 安装校验

pytest --version

2. 运行方式

运行方式有三种

2.1 命令行

pytest -s 文件名.py

例如:

pytest -s test_demo.py

2.2 主函数

在测试文件中增加主函数

if __name__ == '__main__':  		
	pytest.main(["-s", "test_demo.py"])

-s 表示支持控制台打印,如果不加,print 不会出现任何内容

2.3 pycharm运行

首先配置运行方式:
在这里插入图片描述
在这里插入图片描述
注意:
如之前运行过其它模式,清楚记录再运行,清除步骤如下:

在这里插入图片描述
在这里插入图片描述

3. setup 和 teardown

应用场景

pytest 在运行自动化脚本的前后会执行两个特殊的方法,分别是 setup 和 teardown 。
在执行脚本 之前会执行 setup方法,在执行脚本之后会执行 teardown 方法。有了这两个方法,我们可以在 setup 中进行获取驱动对象的操作,在 teardown中进行关闭驱动对象的操作。

3.1 函数级别方法

运行于测试方法的始末,运行一次测试函数会运行一次 setup 和 teardown。

3.1.1 示例代码:
import pytest 

class TestLogin: 
# 函数级开始 
def setup(self): 
	print("------->setup_method") 
# 函数级结束 
def teardown(self): 
	print("------->teardown_method") 

def test_a(self): 
	print("------->test_a") 
	
def test_b(self): 
	print("------->test_b")
3.1.2 执行结果:

scripts/test_login.py ------->setup_method # 第一次 setup()
------->test_a
.------->teardown_method # 第一次 teardown()
------->setup_method # 第二次 setup()
------->test_b
.------->teardown_method # 第二次 teardown()

3.2 类级别方法

运行于测试类的始末,在一个测试内只运行一次setup_class 和 teardown_class,不关心测试类 内有多少个测试函数。

3.2.1 示例代码:
class TestLogin: 
# 测试类级开始 
def setup_class(self): 
	print("------->setup_class")

# 测试类级结束 
def teardown_class(self): 
	print("------->teardown_class") 

def test_a(self): 
	print("------->test_a") 

def test_b(self): 
	print("------->test_b")
3.2.2 执行结果:

scripts/test_login.py ------->setup_class # 第一次 setup_class()
------->test_a
.------->test_b
.------->teardown_class # 第一次 teardown_class()

4. 断言和优化

4.1unittest的三种断言:

  1. assertIn(expect,result)断言包含(被包含的写前面)

  2. assertEqual(expect,result)断言相等

  3. assertTure(条件)断言是否为真。返回Ture或False

4.2 python断言:

  1. assert xx:判断xx为真

  2. assert not xx:判断xx不为真

  3. assert a in b:判断b包含a

  4. assert a == b:判断a等于b

  5. assert a !=b:判断a不等于b

4.3 优化

  • 我们可以在异常的时候,输出一些提示信息。这样报错后。可以方便我们来查看原因。

如下:

在这里插入图片描述

在这里插入图片描述
如果我们加上一点说明,如下:

在这里插入图片描述

在这里插入图片描述

5. 跳过测试函数

5.1 应用场景

同一个软件在不同的设备上可能会有不同的效果,如果设备不支持,那么根本没有必要去测试这个功能。 此时,我们可以让这种函数进行跳过。

5.2 方法名:

参数
condition:跳过的条件,必传参数
reason:标注原因,必传参数 @pytest.mark.skipif(condition, reason=None)

5.3 使用方式

在需要跳过的测试脚本之上加上装饰器@pytest.mark.skipif(condition, reason=“xxx”)

5.4 示例代码:

import pytest class TestLogin: 

	def test_a(self): 
	# test开头的测试函数 
		print("------->test_a") 
		assert 1 
		# 断言成功 

	@pytest.mark.skipif(condition=True, reason="测试跳过") 
	def test_b(self): 
		print("------->test_b") 
		assert 0 
		# 断言失败

4.5 结果:

scripts/test_login.py ------->test_a
.s

6. 数据参数化

6.1 方法名:

参数
argnames:参数名
argvalues:参数对应值,类型必须为可迭代类型,一般使用list
@pytest.mark.parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)

6.2 一个参数使用方式

  1. argnames 为字符串类型,根据需求决定何时的参数名
  2. argvalues 为列表类型,根据需求决定列表元素中的内容
  3. 在测试脚本中,参数,名字与 argnames 保持一致
  4. 在测试脚本中正常使用

argvalues 列表有多少个内容,这个脚本就会运行几次

6.2.1 示例代码:
import pytest 
class TestLogin: 
	@pytest.mark.parametrize("name", ["xiaoming", "xiaohong"]) 
	def test_a(self, name): 
		print(name) 
		assert 1
6.2.2 结果:

scripts/test_login.py xiaoming
.xiaohong
.

6.3 多个参数使用方式

6.3.1 示例代码:
import pytest 

class TestLogin: 
	@pytest.mark.parametrize(("username", "password"), [("zhangsan", "zhangsan123"), (" xiaoming", "xiaoming123")]) 
	def test_a(self, username, password): 
		print(username) 
		print(password) 
		assert 1
6.3.2 结果:

scripts/test_login.py zhangsan
zhangsan123
.xiaoming
xiaoming123
.

多个参数还可以将装饰器写成:
@pytest.mark.parametrize(“username,password”, [(“zhangsan”, “zhangsan123”), (“xiaoming”, “xiaoming123”)])
效果是一样的。

7. 配置文件

使用配置文件后可以快速的使用配置的项来选择执行哪些测试模块。
配置完成后,在命令行中输入pytest即可运行。

7.1 使用方式:

  1. 项目下新建 scripts 模块
  2. 将测试脚本文件放到 scripts 中
  3. pytest 的配置文件放在自动化项目目录下
  4. 名称为 pytest.ini
  5. 命令行运行时会使用该配置文件中的配置
  6. 第一行内容为 [pytest]

7.2 示例:

[pytest]
# 添加命令行参数 
addopts = -s 
# 文件搜索路径 
testpaths = ./scripts 
# 文件名称 
python_files = test_*.py 
# 类名称 
python_classes = Test* 
# 方法名称 
python_functions = test_*

  • addopts = -s 表示命令行参数
  • testpaths,python_files,python_classes,python_functions 表示 哪一个文件夹 下的 哪一个文件 下的 哪一个类 下的 哪一个函数 表示执行 scripts 文件夹下的 test 开头 .py 结尾的文件下的 Test开头的类下的 test开头的函 数

8. 测试报告(不推荐)

8.1 安装

pip install pytest-html

8.2 使用

在配置文件中的命令行参数中增加
html=用户路径/report.html

8.3 示例:

pytest.ini

addopts = -s --html=…/report/report.html

8.4 结果:

在项目目录下会对一个 report 文件夹,里面有个 report.html 即为测试报告

9. 控制函数执行顺序

9.1 安装

pip install pytest-ordering

9.2 使用

  1. 标记于被测试函数,@pytest.mark.run(order=x)
  2. 根据order传入的参数来解决运行顺序
  3. order值全为正数或全为负数时,运行顺序:值越小,优先级越高
  4. 正数和负数同时存在:正数优先级高

9.3 示例:

import pytest 

class TestLogin: 

def test_hello_001(self): 
	print("test_hello_001") 
	
@pytest.mark.run(order=1) 
def test_hello_002(self): 
	print("test_hello_002")

@pytest.mark.run(order=2)
def test_hello_003(self): 
	print("test_hello_003")

9.4 结果:

scripts/test_login.py test_hello_002 # 先运行order=1的方法
.test_hello_003 # 再运行order=2的方法
.test_hello_001 # 最后才是没有设置order的方法

10. 失败重试

10.1 安装

pip install pytest-rerunfailures

10.2 使用

在配置文件中的命令行参数中增加 --reruns n

10.3 示例:

pytest.ini

addopts = -s --reruns 3

class TestLogin: 

def test_a(self): 
# test开头的测试函数 
	print("------->test_a") 
	assert 1 
# 断言成功 

def test_b(self): 
	print("------->test_b") 
	assert 0 
# 断言失败

10.4 结果:

scripts/test_login.py ------->test_a
.------->test_b
R------->test_b
R------->test_b
R------->test_b
F

R 表示重试
重试时,如果脚本通过,那么后续不再重试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值