TestCase,Testsuit,TestLoder,TextTestRunner实现对测试用例全部执行或部分执行

 
 
Test_Request_Module.py

import
requests class Test_Request: def __init__(self,ip): self.ip = ip def ip_address(self): url = "http://apis.juhe.cn/ip/ipNew" data = {"ip":self.ip, "key": "c1ee32d46c0bb8f5938008ff1d4bb28c"} return_data = requests.post(url,data) # print(return_data.json()) print("memeda",return_data.json()['result']['City']) # if __name__ == '__main__': # Test_Request("27.18.198.204").ip_address() #方法需要传参数ip进去
 
 
Test_Case_Unitest_Module.py

import
unittest from Test_Summarize import Test_Request_Module #导入 from Test_Summarize import Test_Openpyxl_Module from ddt import ddt,data #这里需要把Config_Parse的参数传递过来,放在需要执行的测试用例中 from Test_Summarize import Test_Config_Parse_Module return_config_data = Test_Config_Parse_Module.Config_Parse().config_parse() #返回值一 return_config_data2 = Test_Config_Parse_Module.Config_Parse().config_parse2() #返回值2 #这里需要把excel的数据传递过来,类里填写需要执行的用例参数(上面参数的返回值) return_excel_data = Test_Openpyxl_Module.Excel_Class(return_config_data2).Use_Excel() @ddt() class Test_Case_Module(unittest.TestCase): #继承TestCase编写测试用例 def setUp(self): print("我要开始执行用例了") def tearDown(self): print("用例执行结束啦") @data(*return_excel_data) #*号加参数表示脱外套,会把最外面的符号脱掉 def test_in_ip(self,return_excel_data): #函数必须以test开头,函数里面不能传参数,如果有ddt是可以传参数的 #向Test_Request类中写入IP地址 try: Test_Request_Module.Test_Request(return_excel_data['ipdizhi']).ip_address() # self.assertEqual("期望值","实际值") except AssertionError as e: raise e if __name__ == '__main__': unittest.main()

Test_Suit_Loder_Case.py
#获取测试用例类
from Test_Summarize import Test_Case_Unitest_Module
import unittest
#创建加载TestSuit用来存储测试用例
un_suit = unittest.TestSuite()

#创建TestLoder用例加载测试用,可按照方法,类,模块三种方式加载
un_loder = unittest.TestLoader()
jiazai = un_loder.loadTestsFromTestCase(Test_Case_Unitest_Module.Test_Case_Module) #按照类加载

un_suit.addTest(jiazai)
ff = open("cc.txt","a+",encoding="utf-8")
unittest.TextTestRunner(ff,verbosity=2).run(un_suit)
ff.close()
 
 
Test_Openpyxl_Module.py
import openpyxl

class Excel_Class:

    def __init__(self,mode):  #从config里面读取的[1,2]属于字符串
        self.mode = mode

    def Use_Excel(self):
        #打开excel
        open_excel = openpyxl.load_workbook("ww.xlsx")

        #选择那个表单名
        choose_from_name= open_excel["Sheet1"]

        #定位单元格,并且以键值对的形式显示
        max_row = choose_from_name.max_row  #最大行

        max_column = choose_from_name.max_column #最大列
        # choose_from_name.cell(1,1).value
        all_data = [] #新建一个空列表把所有的excel的字典都放在这里
        for i in range(2,max_row+1):
            for j in range(2,max_column+1):
                #以键值对的形式保存,新建一个空的字典集合
                dict_data = {}
                dict_data['ip'] = choose_from_name.cell(i, j-1).value
                dict_data['ipdizhi'] = choose_from_name.cell(i,j).value
                # print(dict_data)

                all_data.append(dict_data)
        # 怎么实现,想测试那几条就测试那几条,可以就输入加判断
        if self.mode == 'all':
            return all_data
        else:
            #循环遍历all_data里面的输入,发现id有和输入进来的mode相同的就保存,之后作为返回值
            choose_all = []
            for item in all_data:
                if str(item['ip']) in self.mode:

                    choose_all.append(all_data[item['ip']-1]) #把合适的放入choose_all中
            return choose_all

if __name__ == '__main__':
    print(Excel_Class('all').Use_Excel())
Test_Config_Parse_Module.py
import configparser

class Config_Parse:
    config = configparser.ConfigParser()
    config.read('cc.config', encoding='utf-8')

    def config_parse(self):
        # print(config.sections())
        # print(self.config['WANGWEI']['aa'])
        return self.config.get('WANGWEI','aa')

    def config_parse2(self):
        # print(self.config['WANGWEI']['bb'])
        return self.config.get('WANGWEI','bb')


if __name__ == '__main__':
    print(type(Config_Parse().config_parse()))
    print(type(Config_Parse().config_parse2()))
cc.config
[WANGWEI]
aa = [1,3,5]
bb = all
 

猜你喜欢

转载自www.cnblogs.com/TKOPython/p/13195246.html