全网火爆,pytest自动化测试框架从0-1精通实战,你的进阶之路...


前言

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"])

D1

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

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

D2

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

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

D3

运行测试目录:
测试目录既可以指定相对路径(如 ./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值全为正数或全为负数时,运行顺序:值越小,优先级越高;
正数和负数同时存在:正数优先级高;

下面是我整理的2023年最全的软件测试工程师学习知识架构体系图

一、Python编程入门到精通

请添加图片描述

二、接口自动化项目实战

请添加图片描述

三、Web自动化项目实战

请添加图片描述

四、App自动化项目实战

请添加图片描述

五、一线大厂简历

请添加图片描述

六、测试开发DevOps体系

请添加图片描述

七、常用自动化测试工具

请添加图片描述

八、JMeter性能测试

请添加图片描述

九、总结(尾部小惊喜)

只有付出汗水和努力,才能收获辉煌的成就;只有拼搏奋斗,才能创造属于自己的精彩人生。坚持不懈,追逐梦想,你将会成为无人可及的强者!

只有坚持不懈的努力,才能成就辉煌;只有勇往直前的决心,才能逐梦成功;不要畏惧困难,勇敢面对挑战,你一定能书写属于自己的壮丽篇章!

只有拼尽全力,才能绽放辉煌;只有坚持不懈,才能创造奇迹;只有勇往直前,才能实现梦想。相信自己,脚踏实地,努力奋斗,未来必将属于你!

猜你喜欢

转载自blog.csdn.net/csdnchengxi/article/details/131810534
今日推荐