微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest 常用方法介绍

前言

  之前一篇文章简单介绍了 pytest 以及 pytest.fixture 装饰器 :https://www.cnblogs.com/shenh/p/11572657.html 。实际在写自动化测试脚本中,还会有一些很实用的方法,下文就来讲述下这些用法

 

 

一.pytest.mark.parametrize 装饰器

 pytest 内置装饰器 @pytest.mark.parametrize 可以让测试数据参数化,把测试数据单独管理,类似 ddt 数据驱动的作用,方便代码和测试数据分离。

1.一次传多个参数

复制代码

import pytest

@pytest.mark.parametrize('x,y',[(1,2),(3,4)])
def test_sum(x,y):
    sum = x + y
    print(sum)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

复制代码

执行结果:

复制代码

test_sample.py 
3
.
7
.

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

复制代码

 

2.组合传参:

 注意:这种方式一共传递了4组参数  (1,3)、(1,4)、(2,3)、(2,4)。这种方式可以简化测试数据,不用手动再将参数组合。

复制代码

import pytest

@pytest.mark.parametrize('x',[1,2])
@pytest.mark.parametrize('y',[3,4])
def test_sum(x,y):
    sum = x + y
    print(sum)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

复制代码

 执行结果:

复制代码

test_sample.py 
4
.
5
.
5
.
6
.

============================== 4 passed in 0.14s ==============================

复制代码

 

二、fixture返回值

1.获取调用函数返回值

复制代码

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'

    return accesstoken


def test_sum(login):
    token = login
    print(token)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

复制代码

 执行结果:

test_sample.py 
197ce8083c38467f
.

============================== 1 passed in 0.04s ==============================

 

 若被调用函数返回多个参数:

复制代码

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'
    customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'

    return accesstoken,customerguid


def test_sum(login):
    token = login[0]
    guid = login[1]
    print(token)
    print(guid)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

复制代码

 执行结果:

复制代码

test_sample.py 
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.

============================== 1 passed in 0.07s ==============================

复制代码

 

2.单个用例调用多个函数

复制代码

import pytest

@pytest.fixture(scope='function')
def login():
    print('登录')

@pytest.fixture(scope='function')
def conn():
    print('连接数据库')

def test_1(login,conn):
    print('测试用例1')

def test_2():
    print('测试用例2')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

复制代码

 执行结果:

复制代码

test_sample.py 
登录
连接数据库
测试用例1
.
测试用例2
.

============================== 2 passed in 0.05s ==============================

复制代码

 

三、测试用例分类

 有时候我们只需执行部分测试用例,比如从用例集当中挑选 smoke 测试,要怎么做呢?通过装饰器 @pytest.mark.smoke,smoke 是可以自定义的,运行时加上命令‘-m=smoke’,pytest 就会挑选带有装饰器的类或函数运行。

复制代码

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'
    customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'

    return accesstoken,customerguid

@pytest.mark.smoke
def test_sum(login):
    token = login[0]
    guid = login[1]
    print(token)
    print(guid)

def test_2():
    print('测试用例')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s','-m=smoke'])

复制代码

 执行结果:

复制代码

test_sample.py 
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.

======================= 1 passed, 1 deselected in 0.02s =======================

复制代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐