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

    1、代码驱动:

    ——用例完全是靠代码来执行的,不执行代码,用例不会执行。

    import requests
    
    def test_more(self,name,age):
        data = {'stu_name':name,'age':age}
        r = requests.get(self.url,data)
        print(r.json())

    2、数据驱动:

    ——用例都是代码写的,有几条就运行几次,没用数据,就会运行不了;如:xiaohei,xiaobai,dahuang,三条数据就会执行3次。

    3、关键字驱动

    UI  自动化的时候使用;原理:{'点击’:click, '双击:dbclick,'退出‘:exit},就是点击一个就会执行对应的函数下边的方法。



    方法:

    I . 文件中读取数据,使用@parameterized.parameterized.expand(),判断测试的数据是否存在

    ——参数类型都一样,数据过多时,可使用

    import unittest
    import requests
    import parameterized
    import os
    
    class GetData:
    
        @staticmethod #连接txt文件,因为类中的几个方法都是互相之间没有关联,所以使用了@staticmethod
        def read_data_to_file(filename):
            data = []
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as fr:
                    for line in fr:
                        d1 = line.strip().split(',')  #每行分隔完之后变成一个数组
                        data.append(d1)
            else:
                raise FileNotFoundError('参数化文件找不到')
            return data
    
        @staticmethod #连接Excel
        def read_data_to_excel(file_name):
            pass
    
        @staticmethod #连接Redis
        def read_data_to_redis(key):
            pass
    
        @staticmethod #连接Mysql
        def read_data_to_mysql(sql):
            pass
    
    class StuInfoTest(unittest.TestCase):
        url = 'http://api.nnzhp.cn/api/user/stu_info'
    
        @parameterized.parameterized.expand(GetData.read_data_to_file('stu_info.txt'))
    #
    parameterized.parameterized.expand()是有多少个参数,调用多少次,可自动的循环进行调用
    def test_single(self,name,age):
            print(name,age)
            data = {'stu_name':name,'age':age}
            r = requests.get(self.url,data)
            print(r.json())
  • 相关阅读:
    【bzoj3566】[SHOI2014]概率充电器 树形概率dp
    【bzoj1419】Red is good 期望dp
    【bzoj2698】染色 期望
    【bzoj2134】单选错位 期望
    【bzoj1022】[SHOI2008]小约翰的游戏John 博弈论
    【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系
    【bzoj2338】[HNOI2011]数矩形 计算几何
    【bzoj2085】[Poi2010]Hamsters Hash+倍增Floyd
    【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分
    【bzoj2795】[Poi2012]A Horrible Poem Hash+分解质因数
  • 原文地址:https://www.cnblogs.com/brf-test/p/14891402.html
Copyright © 2011-2022 走看看