selenium之pytest框架下的参数化处理:参数装饰器,解析列表,元组,字典操作

在这里插入图片描述
在这里插入图片描述

列表:只有一个参数data

import pytest

#列表  这样会运行两次
data=['123','456']
@pytest.mark.parametrize('pwd',data)
def test1(pwd):
    print(pwd)

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items

test05.py::test1[123] PASSED                                             [ 50%]123

test05.py::test1[456] PASSED                                             [100%]456


============================== 2 passed in 0.06s ==============================

元组参数化:

#元组 也是遍历两次 123456
data2=[
    (1,2,3),
    (4,5,6)
]
@pytest.mark.parametrize('a,b,c',data2)
def test2(a,b,c):
    print(a,b,c)

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items

test05.py::test2[1-2-3] PASSED                                           [ 50%]1 2 3

test05.py::test2[4-5-6] PASSED                                           [100%]4 5 6


============================== 2 passed in 0.25s ==============================

Process finished with exit code 0

字典类型:

#字典类型
data3=(
    {'user':1,
    'pwd':2
     },

    {'age':21,
     'name':'byzhang'
     }
)

@pytest.mark.parametrize('dic',data3)
def test3(dic):
    print(dic)

运行结果:


============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items

test05.py::test3[dic0] PASSED                                            [ 50%]{'user': 1, 'pwd': 2}

test05.py::test3[dic1] PASSED                                            [100%]{'age': 21, 'name': 'byzhang'}


============================== 2 passed in 0.06s ==============================

Process finished with exit code 0

添加id方式来执行参数化:

#添加id id的值可以自定义 只要方便理解每个值是干什么的就行
data01=[
    pytest.param(1,2,3,id="a+b:pass"),
    pytest.param(4,5,10,id="a+b:fail")
]
def add(a,b):
    return a+b
#接下来看怎么测试
class TestParametrize(object):
    @pytest.mark.parametrize('a,b,expect',data01)
    def test_paramerrize(self,a,b,expect):
        assert add(a,b)==expect

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items

test05.py::TestParametrize::test_paramerrize[a+b:pass] 
test05.py::TestParametrize::test_paramerrize[a+b:fail] PASSED            [ 50%]FAILED            [100%]
test05.py:51 (TestParametrize.test_paramerrize[a+b:fail])
9 != 10

Expected :10
Actual   :9
<Click to see difference>

self = <seelenium_project.testcases.pytest.test05.TestParametrize object at 0x000002328970AA58>
a = 4, b = 5, expect = 10

    @pytest.mark.parametrize('a,b,expect',data01)
    def test_paramerrize(self,a,b,expect):
>       assert add(a,b)==expect
E       assert 9 == 10

test05.py:54: AssertionError

Assertion failed

Assertion failed

后面会具体运行 在这里就是举例简单介绍一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值