Pytest自动化测试框架实战

🍅 点击文末小卡片,免费获取软件测试全套资料,资料在手,涨薪更快

1、运行方式

命令行模式:

复制代码
pytest -s login.py

主函数模式:

复制代码
if __name__ == '__main__':
	pytest.main(["-s", "login.py"])

pytest.ini运行:

在 pytest.ini 文件中配置 pytest 的运行参数。

注意点:

位置:配置文件一般会放在项目的根目录下;

编码:必须是ANSI,可以使用文本编辑工具修改编码格式;

规则:无论是哪种运行方式,都会读取配置文件,是最高级的;

文件开头第一行除了注释,一定为 [pytest] ,这是固定格式,文件名 pytest.ini 也是固定的,不能自己重命名;

常用参数:

addopts 命令行的参数,用空格分隔;

testpaths 测试用例的路径;

markers 标记参数,赋值方式为 key:value;

python_files 模块的命名规则 xx.py

python_classes 类名的命名规则 Xxx;

python_functions 方法的命名规则 **

required_plugins 插件的使用;

xfail_strict = true 禁用xpass;

2、断言

pytest 单元测试框架并没有提供专门的断言方法,而是直接使用Python 的 assert 进行断言。

复制代码
import pytest

# 功能:用于计算 a 与 b 相加的和
def add(a, b):
    return a + b

# 功能:用于判断素数
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
        return True

# 测试相等
def test_add_1():
    assert add(3, 4) == 7

# 测试不相等
def test_add_2():
    assert add(17, 22) != 50

# 测试大于或等于
def test_add_3():
    assert add(17, 22) <= 50

# 测试小于或等于
def test_add_4():
    assert add(17, 22) >= 38

# 测试包含
def test_in():
    a = "hello"
    b = "he"
    assert b in a

# 测试不包含
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

# 判断是否为 True
def test_true_1():
    assert is_prime(13)

# 判断是否为 True
def test_true_2():
    assert is_prime(7) is True

# 判断是否不为 True
def test_true_3():
    assert not is_prime(4)

# 判断是否不为 True
def test_true_4():
    assert is_prime(6) is not True

# 判断是否为 False
def test_false_1():
    assert is_prime(8) is False


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

3、Fixture

Fixture 通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境。

setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行。

setup_function/teardown_function:在每个测试函数之前与之后执行。

setup/teardown:在每个测试函数之前与之后执行。这两个方法同样可以作用于类方法。

4、参数化

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

示例:

复制代码
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

5、运行测试

pytest 提供了丰富的参数运行测试用例,通过"pytest --help"可以查看帮助。

运行名称中包含某字符串的测试用例:

例如:通过"-k"来指定在名称中包含"add"的测试用例

复制代码
pytest -k add test.py

if __name__ == '__main__':
	pytest.main(["-k", "add", "test.py"])

减少测试的运行冗长:

运行日志少了很多信息,"-q"用来减少测试运行的冗长;也可以使用"--quiet"代替。

复制代码
pytest -q test.py

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

如果出现一条测试用例失败,则退出测试:

这在测试用例的调试阶段是有用的,当出现一条失败的测试用例时,应该先通过调试让这条测试用例运行通过,而不是继续执行后面的测试用例。

复制代码
pytest -x test.py

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

运行测试目录:

测试目录既可以指定相对路径(如 ./test_dir ) , 也可以指定绝对路径(如D:\pytest_sample\test_dir)。

复制代码
pytest ./test_dir

指定特定类或方法执行:

这里指定运行 test_fixtures_02.py 文件中 TestMultiply 类下的 test_numbers_5_6()方法,文件名、类名和方法名之间用"::"符号分隔。

复制代码
pytest test_fixtures_02.py::TestMultiply::test_numbers_5_6

6、跳过测试

使用方式:在需要跳过的测试脚本之上加上装饰器 @pytest.mark.skipif(condition, reason="xxx")

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

7、生成测试报告

生成 JUnit XML 文件:

复制代码
pytest ./test_dir --junit-xml=./report/log.xml

生成在线测试报告:

复制代码
pytest ./test_dir --pastebin=all

上述代码可生成一个 session-log 链接,复制链接,通过浏览器打开,会得到一张 HTML格式的测试报告。

8、pytest 插件

pytest-html:

pytest-html 可以生成 HTML 格式的测试报告。

首先,通过 pip 命令安装 pytest-html 扩展。

复制代码
pip install pytest-html

其次,运行测试用例,并生成测试报告.

复制代码
pytest ./ --html=./report/result.html

pytest-rerunfailures:

pytest-rerunfailures 可以在测试用例失败时进行重试。

复制代码
pip install pytest-rerunfailures

通过"--reruns"参数设置测试用例运行失败后的重试次数。

复制代码
pytest -v test.py --reruns 3

pytest-parallel:

pytest-parallel 扩展可以实现测试用例的并行运行。

复制代码
pip install pytest-parallel

参数"--tests-per-worker"用来指定线程数,"auto"表示自动分配。

复制代码
pytest -q test.py --tests-per-worker auto

示例:

复制代码
from time import sleep

def test_01():
    sleep(3)

def test_02():
    sleep(5)

def test_03():
    sleep(6)

不使用线程运行测试用例花费14.05s,使用后被缩短到 6.02s。

pytest-ordering:

控制函数执行顺序。

复制代码
pip3 install pytest-ordering

使用:

标记于被测试函数,@pytest.mark.run(order=x);

根据order传入的参数来解决运行顺序;

order值全为正数或全为负数时,运行顺序:值越小,优先级越高;

正数和负数同时存在:正数优先级高;

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。

相关推荐
weixin_433179332 小时前
python - 函数 function
开发语言·python
llilian_162 小时前
NTP时间同步服务器 卫星时钟同步服务器在气象监测方向的深度应用 授时服务器
服务器·功能测试·单片机·嵌入式硬件·测试工具
小袁顶风作案2 小时前
leetcode力扣——452. 用最少数量的箭引爆气球
学习·算法·leetcode·职场和发展
不吃鱼的小时喵2 小时前
【Python】关于python多进程
python
喵手2 小时前
Python爬虫零基础入门【第六章:增量、去重、断点续爬·第1节】增量采集:只抓新增/更新(新手也能做)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·增量、去重·增量采集
万粉变现经纪人3 小时前
如何解决 pip install pyodbc 报错 缺少 ‘cl.exe’ 或 ‘sql.h’(ODBC 头文件)问题
数据库·python·sql·网络协议·bug·ssl·pip
llilian_163 小时前
延迟信号发生器 延迟脉冲信号发生器在激光触发领域的应用 高速脉冲信号发生器
功能测试·单片机·嵌入式硬件·测试工具·自动化
dazzle3 小时前
Python数据结构(五):队列详解
数据结构·python
翱翔的苍鹰3 小时前
完整的“RNN + jieba 中文情感分析”项目之一:需要添加添加 JWT 认证
人工智能·python·rnn