Pytest基本用法详解

pytest简介:

1、pytest是一个非常成熟的python单元测试框架,比unitetest更灵活

2、可以和selenium,requests,appium结合实现自动化

3、可以实现测试用例的跳过以及reruns失败用例重跑

4、可以和allure结合生成测试报告

5、可以和jenkins持续集成

6、有很多强大的插件,可以用来实现很多操作

pytest

pytest-xdist 测试用例分布执行,多CPU分发

pytset-ordering 用于改变测试用例的执行顺序

pytest-rerunfailures 失败用例重跑

pytest-html (生成html格式的自动化测试报告)一般不用

allure-pytest 用于生成测试报告

使用pytest,默认的测试用例规则以及基础应用

1、模块名必须以test_开头,或者_test结尾;

2、测试类必须以Test开头,并且不能有init方法;

3、测试方法必须以test开头

pytest测试用例的运行方式

1、主函数运行

1)运行所有:pytest.main()

2)指定模块:pytest.mian(["-vs", test_xxx.py])

3)指定目录:pytest.main(['-vs', './testcase'])

4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成。

pytest main(‘-vs', './ testcase/test_xxx.py::类名::方法名')

pytest main([’-vs' , './testcase/test_xxx.py::类名::方法名']

2.命令行模式

1)运行所有: pytest

2)指定模块:pytest -vs test_xxx.py

3)指定目录:pytest -vs ./testcase

4)指定目录:pytest -vs ./testcase/test_xxx.py::类名::方法名

参数详解:

-s:表示输出调试信息,包括print打印的信息

-v:显示更详细的信息

-vs:这两个参数一起用

-n:支持多线程或者分布式运行测试用例。

例子 : pytest -vs ./testcase/test_.py -n 2

-m:选择特定标记的测试用例进行执行。例如:pytest -m "slow"

--reruns NUM : 失败用例重跑

例子:主函数

pytest.main(["--reruns=2"])

命令行

pytest --reruns 2

-x: 表示只要要一个用例报错,那么测试停止。

--maxfail=2出现两个用例失败就停止。

-k: 根据测试用例的部分字符串指定测试用例。

例子 :

def test_01_ao(self):

print('\n', "------001------")

pytest -vs ./testcase -k "ao"

--html ./report/report.html: 生成html的测试报告。

3.通过读取pytestini配置文件运行。

pytest.ini这个文件它是pytest单元测试框架的核心配置文件。

1.位置:一般放在项目的根目录

2.编码:必须是ANSI,可以使用notpad++修改编码格式。

3.作用:改变ptest默认的行为。

4.运行的规则,不管是主函数的模式运行,命令行模式运行,都会去读取这个配置文件

[pytest]

addopts = -vs #命令行参数,用空格分隔

testpaths = ./testcase #测试用例的路径

python_files = test_*.py #模块的命名规则

python_calsses = Test* #类名的命名规则

python_funtions = test #方法的命名规则

pytest执行测试用例的顺序

unittest ascll的大小来绝对的执行的顺序[0-9][A-Z][a-z]

pytest 默认从上至下

改变默认的执行顺序:使用mark标记。

@pytest.mark.run(order=2)

如何分组执行(冒烟,分模块执行,分接口和web执行)

smoke:冒烟用例,分布在各个模块里面

pytest -m "smoke"

pytest -m "smoke or usermanage or productmanage'

示例:

在pytest.ini文件增加

smokes =

smoke:冒烟用例

api:接口用例

代码:

@pytest.mark.smoke

def test_01():

print("123")

主函数模式:

pytest.main(["-m" 'smoke or api'])

命令模式:

pytest -vs -m "smoke" 单个

pytest -vs -m "smoke" or “api” 多个用例用"or"连接

跳过测试用例:

@pytest.mark.skip(resason="xxxx")无条件跳过

@pytest.mark.skipif() 根据条件跳过

Pytest实现前后置处理(夹具、固件)

一、setup/teardown,setup_class/teardown_class

class Testdemo: def setup_class(self): print("每个测试类执行前执行一次") def teardown_class(self): print("每个测试类执行后执行一次") def setup(self): print("每个测试用例(方法)执行前执行") def teardown(self): print("每个测试用例(方法)执行完成后执行") def test_01(self): print('\n', "------001------") def test_02(self): print('\n', "------002------") def test_03(self): print('\n', "------003------")

二、使用@pytest.fixture()装饰器来实现部分用例的前后置

@pytest.fixture(scope="params="autouse="ids="name=m)

(1)scope表示的是被@pytest.fixture标记的方法的作用域。function(默认),class,module

package/session.

示例:

@pytest.fixture(scope="function", autouse=False) def fix_a(): print("前置") yield print("后置") class Testdemo: def test_01(self): print('\n', "------001------") def test_02(self, fix_a): print('\n', "------002------") def test_03(self): print('\n', "------003------")

(2)params:参数化(支持,列表[],元祖(),字典列表[{},{}],字典元祖({},{}))

import time import pytest @pytest.fixture(scope="function", params=["张三", "李四"], ids=["zs", "ls"], autouse=False) def fix_a(request): print("前置") yield request.param print("后置") class Testdemo: def test_01(self): print("------001------") def test_02(self, fix_a): print("------002------") print("_____________" + str(fix_a)) def test_03(self): print("------003------") print("------003------")

(3)autouse=True:自动执行,默认False

(4)ids:当使用params参数化时,给每一个值设置一个变量名。意义不大。

(5)name:给表示的是被@pytest.fixture标记的方法取一个别名。如果取了别名之后,之前的函数名称就不能用了

三、通过conftest.py+@pytest.fixture()结合使用实现全局的前后置应用

1.conftest.py文件是单独存放的一个夹具配置文件,名称是不能更改。

2.可以在不同的py文件中使用同一个fixture函数。

3.原则上conftest.p需要和运行的用例放到统一层。并也可以放到用例里边的包里,多个文件可以同时使用,且不需要做任何的imprt导入的操作.

示例:

class Testdemo: def test_01(self): print("------001------") def test_02(self, all_fix, first_page): # all_fix全局前置 first_page为firstpage前置 print("------002------") print(all_fix) print(first_page) def test_03(self): print("------003------")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值