文章目录

PyTest1. args参数2. pytest-ordering3. fixture(前置函数)4. parametrize(参数化)5. fixture 与 parametrize 结合6. pyyaml(数据源)7. pytest-xdist(分布式测试)8. allure(测试报告)

PyTest

1. args参数

参数作用-v详细输出测试信息。-q简要输出测试信息。-s输出测试用例中的print语句。-x遇到失败用例时立即停止测试。-k根据条件指定用例去测试,如:‘-k’ ,‘TestCase and not test_case_1’(可指定类名&函数名)-m根据修饰器指定用例去测试,如:‘-m’ , ‘skip’(可指定测试种类)

# args参数运用

if __name__ == '__main__':

pytest.main(args=['-q', '-s','pytest_fixture.py'(测试文件名)])

2. pytest-ordering

修饰器:@pytest.mark.run(order=1)说明:order的值越小,测试用例越先被执行!作用:控制用例的执行顺序依赖:pytest-ordering 模块

@pytest.mark.run(order=4)

def test_case_1(login):

print('Test case 1')

assert True

3. fixture(前置函数)

修饰器:@pytest.fixture()说明:将被@pytest.fixture()修饰的方法传入测试用例参数作用:完成测试之前的初始化,也可以返回数据给测试函数。参数一:scope(作用域参数)

作用域:

function 函数或者方法级别都会被调用.

class 类级别调用一次.

module 模块级别调用一次.

session 是多个文件调用一次(可以跨.py文件调用,每个.py文件就是module).

@pytest.fixture(scope='作用域')

def login():

print('Login Operation!')

@pytest.mark.run(order=4)

def test_case_1(login):

print('Test case 1')

assert True

参数二:params(传递数据参数)

测试过程中需要大量的测试数据,如果每条测试数据都编写一条测试用例,用例数量将是非常宠大的。

一般我们在测试过程中会将测试用到的数据以参数的形式传入到测试用例中,并为每条测试数据生成一个测试结果数据.

@pytest.fixture(params=[1, 2, 3,])

def past_data(request):

print(f"get data : {request.param}")

return request.param

def test_case_9(past_data):

print(f"past_data : {past_data}")

参数三:autouse(自动执行参数)

如果每条测试用例都需要添加 fixture 功能,则需要在每一要用例方法里面传入这个fixture的名字.

这里就可以在装饰器里面添加一个参数 autouse=‘true’,它会自动应用到所有的测试方法中,只是这里没有办法返回值给测试用例.

@pytest.fixture 里设置 autouse 参数值为 true(默认 false),每个测试函数都会自动调用这个前置函数

@pytest.fixture(autouse=True)

def login():

print('Login Operation!')

4. parametrize(参数化)

修饰器:@pytest.mark.paramtrize(‘data’, param) **Parametrize()**方法主要参数说明: argsnames :参数名,是个字符串,如中间用逗号分隔则表示为多个参数名. argsvalues :参数值,参数组成的列表,列表中有几个元素,就会生成几条用例. 作用:实现测试数据的传参。 方式一:单次使用 parametrize

@pytest.mark.parametrize('x', [1, 2, 3])

def test_case_10(x):

print(f"Parameters : {x}")

assert True

方式二:多次使用 parametrize

# 参数按照 (笛卡尔积方式)组合

@pytest.mark.parametrize('x', [1, 2, 3])

@pytest.mark.parametrize('y', [4, 5, 6])

def test_case_10(x, y):

print(f"Parameters : {x} and {y}")

assert True

5. fixture 与 parametrize 结合

适用场景:测试数据需要在 fixture 方法中使用,同时也需要在测试用例中使用。可以在使用 parametrize 的时候添加一个参数 indirect=True,pytest 可以实现将参数传入到 fixture 方法中,也可以在当前的测试用例中使用.结合方法:indirect 参数设置为 True,pytest 会把 argnames 当作函数去执行,将 argvalues 作为参数传入到 argnames 这个函数里.

@pytest.fixture()

def login_app(request):

print("Login browser...")

print(f"value is {request.param}")

return request.param

@pytest.mark.parametrize('login_app', ['zhangsan', 'lisi', 'wangwu'], indirect=True)

def test_case_11(login_app):

print("Test_case_11!")

print(f"value is {login_app}")

assert True

# 运行结果:

# Login browser...

# value is zhangsan(方法输出)

# Test_case_11!

# value is zhangsan(用例输出)

6. pyyaml(数据源)

适用场景:在实际的测试工作中,通常需要对多组不同的输入数据,进行同样的测试操作步骤,以验证我们的软件质量。这种测试,在功能测试中非常耗费人力物力,但是在自动化中,却比较好实现,只要实现了测试操作步骤,然后将多组测试数据以数据驱动的形式注入,就可以实现了.当数据量非常大的时候,我们可以将数据存放到外部文件中,使用的时候将文件中的 依赖:PyYaml 模块

# 数据源

---

-

- 张三

- 李四

- 王五

-

---

Python 代码

import yaml

file_name=os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)),'data/data.yaml'))

@pytest.mark.parametrize('x, y, z', yaml.safe_load(open(file_name, encoding='utf-8')))

def test_case_12(x, y, z):

print("Testing case 12!")

print(f"Value is {x} and {y} and {z}")

assert True

# 运行结果:

# Testing case 12!

# Value is 张三 and 李四 and 王五

7. pytest-xdist(分布式测试)

介绍:pytest-xdist 是 pytest 分布式执行插件,可以多个 CPU 或主机执行,这款插件允许用户将测试并发执行(进程级并发), 插件是动态决定测试用例执行顺序的。依赖:pytest-xdist 模块

# 参数 -n atuo(可以指定内核数)

if __name__ == "__main__":

pytest.main(args=['-s', '-v', '-n', 'auto'])

8. allure(测试报告)

依赖:allure-pytest 模块用法:

# 导包

import pytest,os

allure_result_path = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'report/json'))

allure_report_path = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'report/html'))

if __name__ == "__main__":

pytest.main(args=['-s', '-v', '--alluredir', allure_result_path])

cmd = 'allure generate %s -o %s -c' % (allure_result_path, allure_report_path)

os.system(cmd)

# 注意:

# 这里可能会有报错现象,解决方法:将pycharm软件设置成以管理员模式运行

# 使用步骤:

1. 将pytest配置文件中的命令行参数加上如下代码

--alluredir report

2. 编写好测试脚本后,在命令行行中运行pytest

[pytest]

addopts = -s --alluredir report

testpaths = ./scripts

python_files = test*.py

python_classes = Test*

python_functions = test*

3. 程序运行结束后,会在项目的report目录中生成一些json文件

# 增加allure特性,丰富测试报告内容:

@allure.epic() epic描述 敏捷里面的概念,定义史诗,往下是feature

@allure.feature() 模块名称 功能点的描述,往下是story

@allure.story() 用户故事 用户故事,往下是title

@allure.title(用例的标题) 用例的标题 重命名html报告名称

@allure.testcase() 测试用例的链接地址 对应功能测试用例系统里面的case

@allure.issue() 缺陷 对应缺陷管理系统里面的链接

@allure.description() 用例描述 测试用例的描述

@allure.step() 操作步骤 测试用例的步骤

@allure.severity() 用例等级 blocker,critical,normal,minor,trivial

@allure.link() 链接 定义一个链接,在测试报告展现

@allure.attachment() 附件 报告添加附件

参考文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: