unittest 进行导出可视化报告

import unittest
import requests
import json
import xlrd
from HTMLTestRunner import HTMLTestRunner
from BeautifulReport import BeautifulReport as bf
class myapi():
    def login(self,name):
        data = {
            'loginType': 'xxxx',
            'name': name,
            'passwd': 'xxxxxxxxxx',
            'yzm': '7104'
        }
        r = requests.post('xxxxxxxxxxxxxxxxxxxxxxxxx', data=data)
        dict = json.loads(r.text)
        # print(dict['message'])
        self.messege = dict['message']
        return self.messege#返回’登陆成功‘
    def name(self,table,nrows):#将返回的name 添加到list
            TestName = []
            for i in range(0, nrows):
                TestName.append(table.cell(i, 0).value)
            return TestName
    def open_excel(self, path):
        workbook = xlrd.open_workbook(path)
        table = workbook.sheets()[0]
        return table     #返回第一页
    def get_nrows(self, table):
        nrows = table.nrows
        return nrows#获取到一共有多少行

class TestUserLogin(unittest.TestCase):  # 继承unittest.TestCase
        api = myapi()
        excel_table = api.open_excel(r'C:\Users\Administrator\Desktop\login.xlsx')
        nrows = api.get_nrows(excel_table)
        name = api.name(excel_table, nrows)
        def test_1(self): #用例一
            messge = self.api.login(self.name[0])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[0] + '通过')

        def test_2(self):#用例二
            messge = self.api.login(self.name[1])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[1]+'通过')
        def test_3(self):
            messge = self.api.login(self.name[2])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[2] + '通过')
        def test_4(self):
            messge = self.api.login(self.name[3])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[3] + '通过')
        def test_5(self):
            '''这是成功的用例'''
            messge = self.api.login(self.name[4])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[4] + '通过')
            print('这是一条通过的用例')
        def test_6(self):
            '''这是成功的用例'''
            # 上面一行是注释,显示在测试报告的用例描述列,不能用#注释,只能用'''
            messge = self.api.login(self.name[5])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[5] + '通过')
        def test_7(self):
            messge = self.api.login(self.name[6])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[6] + '通过')
        def test_8(self):
            messge = self.api.login(self.name[7])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[7] + '通过')
        def test_9(self):
            messge = self.api.login(self.name[8])
            self.assertEqual('登录采购商成功!', messge)
            print(self.name[8])

if __name__ == '__main__':
    suite = unittest.TestSuite() # 继承unittest.TestCase
    for i in range(1,10):
        suite.addTest(TestUserLogin('test_'+str(i)))   #将所有的测试用例加载进去
    # with open('执行结果.txt', 'a+') as f:
    #     runner = unittest.TextTestRunner(stream=f,verbosity=2)  # 关键在于stream=f   f是保存
    #     runner.run(suite)
    '''from HTMLTestRunner import HTMLTestRunner  较丑'''

    # path='C:/Users/Administrator/PycharmProjects/untitled1/测试报告.html';
    # fp=open(path,'wb')
    # runner=HTMLTestRunner(stream=fp,title=u"这是第一个测试报告",description=u"用例执行情况")
    # runner.run(suite)
    # fp.close()

    '''from BeautifulReport import BeautifulReport as bf  比较美观'''
    run = bf(suite)  # 实例化BeautifulReport模块
    run.report(filename='测试报告html', description='招采测试')#description用例名称

你可能感兴趣的:(单元测试)