zoukankan      html  css  js  c++  java
  • python3+requests接口自动化--ddt数据驱动

    DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据),file_data(可以从json或者yaml中获取测试数据)

    只有yaml和yml结尾的文件以yaml形式上传,其他情况下默认为json

    通常情况下,data中的数据按照一个参数传递给测试用例,如果data中含有多个数据,以元组,列表,字典等数据,需要自行在脚本中对数据进行分解或者使用unpack分解数据

    @data(a,b)

    那么a和b各运行一次用例

    @data([a,d],[c,d])

    如果没有unpack,那么[a,b]当成一个参数传入用例运行

    如果有unpack,那么[a,b]被分解开,按照用例中的两个参数传递

    @file_data(filename)

    对于json的文件,每一个json元素按照一个用例运行,可以依照python分解元组,列表或者字典的方式分解传入

     1 import unittest
     2 from ddt import ddt,data,file_data,unpack
     3 
     4 @ddt
     5 class demotest(unittest.TestCase):
     6     def setup(self):
     7         print ("this is the setup")
     8 
     9     @data(2,3)
    10     def testb(self,value):
    11         print (value)
    12         print ("this is test b")
    13 
    14     @data([2,3],[4,5])
    15     def testa(self,value):
    16         print (value)
    17         print ("this is test a")
    18 
    19     @data([2, 3], [4, 5])
    20     @unpack
    21     def testc(self, first,second):
    22         print (first)
    23         print (second)
    24         print ("this is test c")
    25 
    26     @file_data('d:/data_dic.json')
    27     def test_dic(self,value):
    28         print (value)
    29         print ('this is dic')
    30 
    31     @file_data('d:/data.yml')
    32     def test_yml(self, value):
    33         print (value)
    34         print ('this is yml')
    35 
    36     def teardown(self):
    37         print ("this is the down")
    38 
    39 if __name__ == '__main__':
    40     unittest.main()
  • 相关阅读:
    pptpvpn链接问题
    nginx网站架构优化思路(原)
    KEEPALIVED 检测RS原理
    linux 做gw(nat)详细配置
    pptpvpn 连接后 无法上外网
    网站最常见的错误
    Python服务器开发 -- 网络基础
    python高性能编程方法一
    一步步来用C语言来写python扩展
    http响应Last-Modified和ETag
  • 原文地址:https://www.cnblogs.com/jayson-0425/p/9920932.html
Copyright © 2011-2022 走看看