Pytest(复习)

Pytest

pytest插件

1.pytest         			pytest框架
2.pytest-html				生成html
3.pytest-xdist				多线程插件
4.pytest-ordering			控制测试用例的执行顺序
5.pytest-rerunfailures		失败重跑
6.pytest-allure				生成美观的测试报告

将命令放入requirements.txt文件中
在执行pip install -r requirements.txt一键安装所有插件 

参数详解:

-s: 表示输出调试信息
-v: 显示更详细的信息
-vs: 这两个参数一起用
-n: 支持多线程或者分布式运行测试用例
--reruns: 失败用例:重跑
-x :表示只要一个用例报错,那么测试停止
--maxfail=2 出现两个用例失败就停止
-k :根据测试用例的部分字符串指定测试用例
--html :生成html的测试报告

Pyttest实现前后置的处理,常用三种:

一,setup_method/teardown_method,setup_class/teardown_class

class TestMashang:
    def setup_class(self):
        print("\n 创建数据库链接")


    def setup_method(self):
        print("\n打开浏览器,加载页面")

    def test_01_baili(self):
        print("测试百里")

    def test_02_baili(self):
        print('我是帅哥')

    def teardown_method(self):
        print("\n关闭浏览器")
        
    def teardown_class(self):
        print("\n 创建数据库链接")

二, 使用@pytest.fixture()装饰器来实现部分用例的前后置
1.scope:表示标记的方法的作用域
2.params:参数化
3.autouse=True:自动执行,默认False
4.ids:当使用params参数时,给每一个值设置变量名
5. name:给表示的是被@pytest.fixture标记的方法去一个别名

1.scpe示例

import pytest

@pytest.fixture(scope='function')
def my_fixture():
    print("这是一个fixture前置方法")
    yield
    print("这是一个fixture后置方法")

class TestMashang:

    def test_01_baili(self):
        print("测试百里")

    def test_02_baili(self,my_fixture):
        print('我是帅哥')

2.params示例

import pytest

@pytest.fixture(scope='function',params=['成龙','甄子丹','蔡依林'])
def my_fixture(request):
    return request.param


class TestMashang:

    def test_01_baili(self):
        print("测试百里")

    def test_02_baili(self,my_fixture):
        print('我是帅哥')
        print('----------'+str(my_fixture))

三.通过conftest.py和@pytest.fixture()结合使用实现全局的前置应用(比如项目的全局登录,模块的全局处理)

1.conftest.py文件是单独存放在一个夹具配置文件,名称不能更改
2.用处可以在不同的py文件中使用一个fixture函数
3.原则上需要和运行的用例放在同一层,并且不需要任何的import导入的操作

断言

import locust

class MyUser(locust.HttpUser):
wait_time = locust.between(1,2)

@locust.task
def test_api(self):
    resp = self.client.get('http://127.0.0.1:5000/auth/login')
    assert resp.status_code == 200
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值