python——pytest基础

一、pytest简介

pytest是第三方提供的单元测试框架,提供了更多的扩展,方便使用。

下载:pip install pytest
与unittest区别:unittest在定义测试用例时需在测试类中进行定义,而pytest可以直接定义测试用例函数,但为了代码规范,建议还是在特定测试类内集中定义测试用例。

二、文件命名规则

测试文件和测试函数必须以“test”开头,测试类必须以“Test”开头,python对大小写敏感。

三、基本使用方法

1.断言

pytest都是使用assert进行断言,而unittest是用assertEqual()、assertIn()、assertTrue()、assertIs()等方法断言。下方列举几个pytest断言方法:
assert b in a :测试包含
assert b not in a:测试不包含
assert b is True:判断是否为True
assert b :判断是否为True
assert not b:判断是否不为True
assert b is not True:判断是否不为True
assert b is False:判断是否为False

2.Fixtrue

对测试方法、测试函数、测试类、测试模块、整个测试文件进行初始化和还原环境。
(1)模块级别和函数级别
setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行。
setup_function/teardown_function:在每个测试函数之前与之后执行。
setup()/teardown():在每个测试函数之前与之后执行,执行在setup_function()之后、teardown_function()之前。
(2)类级别和方法级别
setup_class/teardown_class:在当前测试类的开始与结束时执行
setup_method/teardown_method:在每个测试方法开始与结束执行。
setup()/teardown():在每个测试方法开始与结束执行,执行在setup_method()之后、teardown_method()之前。

3.参数化

pytest通过pytest.mark.parametrize()方法设置参数,示例代码:

import pytest
import math

#python 参数化
@pytest.mark.parametrize(
    "base,exponent,expexted",
    [(2,2,4),
     (2,3,8),
     (1,9,1),
     (0,9,0)],
    ids=["case1","case2","case3","case4"]
)
def test_pow(base,exponent,expected):
    assert math.pow(base,exponent) == expected

先定义参数名称,即“base,exponent,expexted”,接着就是测试用例列表,列表内包含一个个的测试用例数据元祖,ids是用以定义测试用例的名称,默认为None。注意:参数化定义的参数名称,必须和测试函数的参数名字相同,否则无法正常获取数据。

运行截图:
在这里插入图片描述

4.运行测试

(1)Mark机制:在每一个测试用例前加一个marker,在运行时可以只运行带有该marker的测试用例,相当于用marker实现分类,运行时就能只跑某一类的测试用例。
注:支持一个测试用例有多个marker
示例:

import pytest
class TestA(object):
    def inc(self,x):
        return x+2
    @pytest.mark.webtest
    def test_anser(self):
        assert self.inc(3) == 5
    def test_bnser(self):
        assert self.inc(4) == 7

在测试用例前方加上:@pytest.mark.webtest, webtest即标注该测试用例的marker;
命令行执行:pytest -v -m "webtest" test2.py
在这里插入图片描述
(2)选择运行特定的测试用例
命令行执行:

pytest -v test.py::TestClass::test_method

(3)选择运行特定的某个类,即某个类所有的测试用例

pytest -v test.py::TestClass

(4)用-k进行关键字匹配来运行测试用例名字子串

pytest -v -k http test.py

表示运行test模块中,名称含“http”子串的测试用例集合

(5)运行某个目录下的测试用例
pytest +路径(绝对路径/相对路径)

5.参数

s:关闭捕捉,从而输出打印信息。
-v:增加测试用例冗长,即 将每个测试用例的运行情况展示出来。
-q:减少测试用例冗长,只展示运行结果情况。
-k:运行名称包含某字符串的用例。
-x:如果出现一条测试用例失败,则退出测试。

四、测试报告生成

1.生成JUnit XML文件:主要用于存放测试结果
例:pytest ./test_dir --junit-xml=./report/log.xml
2.生成在线测试报告:即生成一个链接,浏览器打开链接查看报告。
例:pytest ./test_dir --pastebin=all
3.生成HTML格式的测试报告
~需按照pytest-html扩展:pip install pytest-html
例:pytest ./ --html=./report/result.html

五、扩展

pytest-rerunfailures:在测试用例失败时进行重试,通过“–reruns”参数设置测试用例运行失败后的重试次数。

pytest-parallel:实现测试用例的并行运行,在每个测试用例中分别设置sleep()模拟运行时间较长的测试用例。
运行时通过“–test-per-worker”指定线程数,“auto”表示自动分配
例:pytest -q test.py --test-per-worker auto

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python接口测试中,pytest是一个常用的测试框架。使用pytest进行测试非常简单。下面是一个示例代码: ```python import pytest def test_001(): print("test_01") def test_002(): print("test_02") if __name__ == '__main__': pytest.main(\["-v","test_1214.py"\]) ``` 在cmd中输入pytest命令可以运行测试用例。例如: ``` D:\workspace\pythonc\demo>pytest ``` 运行结果会显示测试用例的执行情况,包括通过的用例和失败的用例。 另外,你可以使用pip安装pytest,命令如下: ``` pip install pytest ``` 安装完成后,可以使用pytest --version命令查看pytest的版本。 对于入门级的使用,你可以创建一个简单的测试方法,如下所示: ```python def func(x): return x + 1 def test_a(): print("test_a") assert func(3) == 5 def test_b(): print("test_b") assert 1 ``` 在命令行中运行pytest_demo.py文件,可以执行测试用例。例如: ``` pytest pytest_demo.py ``` 这是pytest基础使用方法,你可以根据需要编写更多的测试用例和断言来进行接口测试。 #### 引用[.reference_title] - *1* [Python测试框架Pytest基础入门](https://blog.csdn.net/m0_67695717/article/details/126202016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python接口自动化系列--pytest入门](https://blog.csdn.net/m0_75277660/article/details/129364869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [基于python接口自动化测试——pytest](https://blog.csdn.net/you_well/article/details/130254767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值