zoukankan      html  css  js  c++  java
  • pytest参数化

    #!/usr/local/bin/python3
    # -*- coding: utf-8 -*-
    
    import pytest
    
    __author__ = "Carp-Li"
    __date__ = "2020/10/10"
    
    
    class TestClassCase:
    
        @pytest.mark.parametrize("phone,code,msg", [
            ("13250813191", "1234", "应该验证通过"),
            ("1325081319", "1234", "手机号长度不足11位,应该验证失败"),
            ("13250813191", "123", "验证码长度不足4位,应该验证失败"),
        ])
        def test_param_case(self, phone, code, msg):
            """ 1、基于pytest.mark.parametrize装饰器实现测试用例参数化 """
            assert len(phone) == 11 and len(code) == 4, msg
    
        # 2、使用fixture中的param参数,完成前置条件的参数化
        @pytest.fixture(params=[
            {"username": "admin", "scope": 1},
            {"username": "user", "scope": 2}
        ], ids=["admin", "user"])
        # 4、ids参数只是对应params里面参数的标识,如果不传,则回去变量名或者函数名+序号
        # @加了ids的参数,pytest命令行执行用例时,可以使用-k id_name 来执行该条用例
        def init_func(self, request):
            # 3、只能通过request.param获取参数
            self.scope = request.param['scope']
            return self.scope
    
        def test_fixture_param_case(self, init_func):
            assert init_func == 1, "登录用户必须是管理员"
    
        @pytest.fixture()
        def get_auth(self, request):
            return request.param['scope']
    
        # indirect=True时,第一个参数就不再是变量名而是函数了
        @pytest.mark.parametrize("get_auth", [
            {"username": "admin", "scope": 1},
            {"username": "user", "scope": 2}
        ], indirect=True)
        def test_data_case(self, get_auth):
            # 使用pytest.mark.parametrize+fixture实现前置条件的参数化
            assert get_auth == 1, "登录用户必须是管理员"
    
    
  • 相关阅读:
    LeetCode(65) Valid Number
    LeetCode(57) Insert Interval
    Python 之scrapy框架58同城招聘爬取案例
    Python 之12306网站验证码校验案例
    Python 之selenium+phantomJS斗鱼抓取案例
    Python 之pytesseract模块读取知乎验证码案例
    Python 之糗事百科多线程爬虫案例
    Python 之beautifulSoup4解析库
    Python 之lxml解析库
    把一张的数据添加到另一张中
  • 原文地址:https://www.cnblogs.com/carp-li/p/13795643.html
Copyright © 2011-2022 走看看