• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest框架的使用

武飞扬头像
Fweilian
帮助1

pytest框架

一, pytest框架环境搭建

  1. Pip install pytest

二,pytest框架的使用

1,pytest测试需要遵守的规则:

测试文件要以test_开头

测试类要以 Test开头,并且不能有init方法

测试方法必须要以test_开头

断言使用assert

setup和teardown---这个可以在类中使用,也可以在类外进行使用,该方法每条用例都会执行

setup_module和teardown_module--- 表示只能类外面执行用例过程中,只执行1次

setup_function和teardown_function--- 表示在类外面执行用例过程中,每次都会执行前置和后置

setup_calss和teardown_calss--- 表示在类中执行测试用例前,只执行1次测试前置和测试后置

setup_method和teardown_method--- 表示在类中每次执行测试用例前,测试前置和测试后置都会执行一次

三, 运行命令

运行所有:pytest

运行指定模块: pytest -vs test_0616.py

运行指定目录:pytest -vs ./api/testcase

通过nodeID运行指定的测试函数:

    pytest -vs ./testcase/test_0616.py : : test_04_func

    pytest -vs ./testcase/test_0616.py : : TestLogin : : test_04_func

-q --安静模式,不输出环境信息

-s ---显示打印内容

-sq--简化打印信息

-v  ---节点

    pytest -v ./testcase/test_0616.py : : TestLogin : : test_04_func

-k --匹配用例名称,执行测试用例名称包含(关键字)的所有用例,如pytest test.py -k 关键字

    pytest -k leson---执行测试用例中包含lesson字符的用例

-m --mark,运行带有标记的测试用例

-n --多线程运行(需要先安装pytest-xdist),如pytest test.py -n 2

-x --遇到错误时停止

skip---跳过---跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选原因

skipif---有条件的跳过

四 , 参数化

① mark.parametrize参数化

    @pytest.mark.parametrize('变量名',参数列表)

@pytest.mark.parametrize('a,b,c',[(1,2,3),(4,5,6),(7,8,9)])

1.自定义参数

@pytest.mark.parametrize('a,b,expect',[

   [1,1,2],

   [2,2,4],

   [3,3,6],

   [4,4,8],

   [5,5,10]

])

def test_add_list(a,b,expect):

   assert add(a,b)==expect

2.Yaml文档

#用户名请求为空

"url": "http://localhost:5000/login"

"body": '{

           "password":"admin",

           "sex":"男",

           "age":18

        }'

"expect": '{

              "message": {

                  "username": "用户名不能为空"

              }

          }'

def readJson():

   return json.load(open('login.json','r'))['item']

@pytest.mark.parametrize('data',readJson())

def test_json_login(data):

   r=requests.post(

      url=data['request']['url'],

      json=data['request']['body'])

   assert r.json()==data['response'][0]

3.excel表格

import  xlrd

def readExcel():

   data=list()

   book=xlrd.open_workbook('login.xls')

   sheet=book.sheet_by_index(0)

   for item in range(1,sheet.nrows):

      data.append(sheet.row_values(item))

   return data

@pytest.mark.parametrize('data',readExcel())

def test_excel_login(data):

   r=requests.post(

      url=data[0],

      json=json.loads(data[1]))

   assert r.json()==json.loads(data[2])

4.Json数据格式

{

  "item":

  [

    {

      "request":

      {

        "url": "http://localhost:5000/login",

        "body":

        {

          "password":"admin",

          "sex":"男",

          "age":18

        }

      },

      "response":

      [

        {

          "message":

          {

            "username": "用户名不能为空"

          }

        }

      ]

},

import  requests

import  json

def readJson():

   return json.load(open('login.json','r'))['item']

@pytest.mark.parametrize('data',readJson())

def test_json_login(data):

   r=requests.post(

      url=data['request']['url'],

      json=data['request']['body'])

   assert r.json()==data['response'][0]

②fixture参数化

fixture的使用: 用于在测试之前和之后执行设置方法

    @pytest.fixtrue()

    def get_sum():

        return [(1,2,3),(4,5,6)]

    def test_login(get_sum):

        if  "1" in get_sum:

        asster  2==2

    

    pytest.fixtrue(Scope=function)

    什么都不填,默认function

    function: 每次测试运行一次

    class: 每类运行一次

    module:每个模块运行一次

    session:  每个会话运行一次

    权重:function< class<module<session

    @pytest.fixtrue

    在用例中可以直接写上带有fixtrue的函数名,作为参数传入

    使用Tools库里的exccoler函数来读取excel表格

五, 用例执行定制化:

@pytest.mark--给用例类或函数贴标签,可以在每一个模块,每一类,每一个方法和用前添加,方便后续定制化执行测试用例

例:

    @pytest.mark.shop---

    class Testshop:

        pass

@pytest.mark.shop

@pytest.mark.shop_list

装饰器:跳过/有条件跳过

skip---跳过---跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选原因

skipif---有条件的跳过

@pytest.mark.skip()--跳过这个函数

@pytest.mark.skipif(1==1)--条件跳过,当1==1时,跳过这个函数

运行时:-m, 加上标签名即可运行指定标签的用例

  pytest  -m “shop_list” 单个标签运行

  pytest  -m “shop_list or shop" 多个标签运行

  pytest  -m “not shop_list” 不运行这个标签

  pytest  -m “not (shop_list or shop_df)” 不运行这些标签

指定用例运行的顺序:

默认是按照字母来执行顺序的,但是我们可以使用pytest_ordering来定制执行顺序

@pytest.mark.run(order=2)

def test_百度():

    pass

@pytest.mark.run(order=3)

def test_百度():

    pass

@pytest.mark.run(order=1)

def test_百度():

    pass

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgceeka
系列文章
更多 icon
同类精品
更多 icon
继续加载