pytest(四)

参数化parametrize

import pytest


@pytest.mark.parametrize('input, result',[('5-0',5),("2+3",5),])
def test_01(input, result):
    assert eval(input) == result

@pytest.mark.parametrize('x',[1,2,3,])
@pytest.mark.parametrize('y',[4,5,6,])
def test_02(x,y):
    print('x => %s,y => %s' % (x,y))
test_para.py ...x => 1,y => 4
.x => 2,y => 4
.x => 3,y => 4
.x => 1,y => 5
.x => 2,y => 5
.x => 3,y => 5
.x => 1,y => 6
.x => 2,y => 6
.x => 3,y => 6

通过编写的小案例发现:

parametrize参数化,第一种写法是对应的,第二种写法会将所有组合都跑一遍

自定义标记mark

# test_para.py
import pytest

@pytest.mark.web
@pytest.mark.parametrize('input, result',[('5-0',5),("2+3",5),])
def test_01(input, result):
    assert eval(input) == result
    print('断言通过')

@pytest.mark.parametrize('x',[1,2,3,])
@pytest.mark.parametrize('y',[4,5,6,])
def test_02(x,y):
    print('x => %s,y => %s' % (x,y))

# test_class.py
import pytest
import time

@pytest.mark.web
def test_1(driver,start):
    print('搜索python')
    driver.find_element_by_id('kw').send_keys('python')
    driver.find_element_by_id('su').click()
    time.sleep(5)

def test_2(driver,start):
    print('搜索地图')
    driver.find_element_by_id('kw').send_keys('地图')
    driver.find_element_by_id('su').click()
    time.sleep(5)


上述几个case我们标记 web,然后我们在文件目录中运行命令行, > pytest -m web

pytest就回自动运行 test_para.py/test_01和test_class/test_1 两条用例,别的case都会跳过不运行

如果相同时运行多个标记case,那么标记名之间用 or 相连, > pytest -m "web or webtest"

这时会运行所有标记为 web和webtest 的case

mark标记也可以用在类上,cmd运行类上的标记时,会运行类下的所有case

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值