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 =======================
原文地址:https://www.cnblogs.com/shenh/p/11593256.html