【接口测试】4_代码实现 _pytest框架

文章目录

一、pytest简介和安装

1、pytest 是开发人员用来实现 "单元测试" 的框架。测试工程师,可以在自动化 "测试执行" 时使用。

2、使用 pytest 的好处:

  • 方便管理、维护测试用例。
  • 简单的断言方法。
  • 生成测试报告。

3、安装 pytest

python 复制代码
pip install pytest 

pip install requests -i https://pypi.douban.com/simple/     # 指定镜像源安装

二、定义测试类、测试方法

  • 定义测试类管理测试用例
  • 测试类中的测试方法和excel写的测试用例一一对应。(excel表格写一条测试用例,在测试类中添加一条测试方法)

如:登录接口,共 15 条测试用例。但,需要15个测试方法。

python 复制代码
# 测试类名,建议以Test开头。(类名首字母大写)
class TestXxx(object):  
    
	# 测试方法名,必须以小写test开头。 
    def test_xxx(self): 
        pass 

三、执行测试方法(重点)

yacas 复制代码
单文件执行(了解)

	方法1:终端命令 pytest -s xxx.py。 
	方法2:①方法定义后,右键->运行 此方法。在方法后面去运行,只运行这一个方法。
          ②类定义后,右键->运行 所有方法。在类后面去运行,则运行类里面的所有方法。
          注:方法2需要pycharm2020以上版本,才可以。

	方法3:导包 import pytest
		  if __name__ == '__main__':
			  pytest.main(['-s', 'xxx.py'])


多文件执行【重点】

	1. 直接在项目目录下,创建文件 pytest.ini
	2. 编写 pytest.ini 文件
			[pytest]					固定内容
			addopts = -s				添加命令行参数 add options
			testpaths = ./xxx			.py 测试用例文件,相对于 pytest.ini 位置
			python_files = xxx*.py		.py 文件名。 可用 通配符
			python_classes = Test*		测试类名。  可用 通配符
			python_functions = test*	测试方法名。 可用 通配符
	3. 终端执行命令 pytest

四、Fixture风格

python 复制代码
# 1.方法级别方法 
	def setup(self):    在 每个 普通测试方法执行之前,自动被调用一次。
    def teardown(self): 在 每个 普通测试方法执行之后,自动被调用一次。
        
# 2.类级别方法
	def setup_class(self):    在 类中 所有测试方法 执行之前,自动被调用一次。
    def teardown_class(self): 在 类中 所有测试方法 执行结束之后,自动被调用一次。

五、断言

断言错误,会在控制台显示断言错误(AssertionError)。

python 复制代码
# 方法1:	
	assert 预期结果 == 实际结果		------------ 断言相等
# 方法2:	
	assert 预期结果 in 实际结果		------------ 断言包含

5.1 断言-案例

python 复制代码
# 定义测试类
class TestAdd(object):
    # 定义测试方法
    def test01_add(self):
        print("第 1 个 测试方法!!!")
        assert 10 == 2 + 8

    def test02_add(self):
        print("第 2 个 测试方法!!!")
        assert "hello" in "helloword"      # hello是包含在hellohelloword里面

结果:

bash 复制代码
# 前面已经配置了 pytest.ini 文件,才能直接在终端中使用pytest。

PS C:\XM1\requestsTestSh27> pytest
================================================================================= test session starts =================================================================================
platform win32 -- Python 3.7.4, pytest-7.4.4, pluggy-0.13.0
rootdir: C:\XM1\requestsTestSh27
configfile: pytest.ini
testpaths: ./
plugins: arraydiff-0.3, doctestplus-0.4.0, html-3.2.0, metadata-3.0.0, openfiles-0.4.0, remotedata-0.3.2
collected 2 items                                                                                                                                                                      

pytest01_review.py 第 1 个 测试方法!!!
.第 2 个 测试方法!!!
.

---------------------------------------- generated html file: file:///C:/XM1/requestsTestSh27/report/%E6%88%91%E7%9A%84%E6%8A%A5%E5%91%8A.html ----------------------------------------
================================================================================== 2 passed in 0.05s ==================================================================================
PS C:\XM1\requestsTestSh27>

六、测试报告

必须在项目所在的目录下,执行pytest命令,生成测试报告。

python 复制代码
# 1. 安装插件 
pip install pytest-html


# 2. 修改 pytest.ini 文件的 addopts 的值。
"""
report:表示目录,当前项目下得有这个report目录。
- ①测试报告名.html:自己起测试报告名。
- ②--self-contained-html:这个参数如果不加,每次生成测试报告会有一个中间文件,这个中间文件没用。
这个参数就是不加这个缓存文件。
"""
addopts = -s --html=report/测试报告名.html --self-contained-html

# 3. 使用命令 pytest 生成测试报告 
相关推荐
屋顶那猫3 小时前
使用pyinstaller打包pytest项目
python·pytest
安冬的码畜日常4 小时前
【玩转 Postman 接口测试与开发2_020】(完结篇)DIY 实战:随书示例 API 项目本地部署保姆级搭建教程(含完整调试过程)
python·测试工具·django·接口测试·postman·fastapi·api项目
爱吃 香菜4 小时前
一文掌握接口测试三大工具:Jmeter、Postman、PyCharm
自动化测试·软件测试·测试工具·jmeter·接口测试·postman·职场经验
Wpa.wk11 小时前
接口测试 - 了解接口测试和接口协议
经验分享·接口测试·接口协议
Wpa.wk11 小时前
接口测试 - 接口测试用例设计
经验分享·测试用例·接口测试·postman
百度测试开发12 小时前
【最细】软件测试面试项目讲解,项目经验,功能到接口到自动化...
自动化测试·软件测试·功能测试·软件测试工程师·接口测试·软件测试项目·软件测试面试
zyx没烦恼1 天前
pytest框架
pytest
百度测试开发1 天前
测试经验分享,Web自动化测试+性能测试面试项目讲解(详情)
自动化测试·软件测试·软件测试工程师·接口测试·软件测试项目·软件测试面试·性能测试
流星白龙2 天前
7.YAML和JSON Schema
json·接口测试