将测试结果写入到Excel表格中

#coding:utf-8
import ddt,os,unittest,warnings
from common.read_excel import ExcelUtil
from common.test_result import TestResult
from xlrd import open_workbook
from xlutils.copy import copy
from common.environment_variable import EnvironmentVariableNew
from common.set_mock import SetMock
from test_case.test_data.pests.days_forecast_data15 import PestsDaysforecastdata15
from test_case.test_data.pests.historyrainfall_data import PestsHistorydata
from test_case.test_data.pests.pests_probability_expect_result import PestsProbabilityExpectResult

#实例化环境变量
evn = EnvironmentVariableNew()
tr = TestResult()
set_mock = SetMock()
pdf = PestsDaysforecastdata15()
phd = PestsHistorydata()
pper = PestsProbabilityExpectResult()
#准备测试数据
xlsx_path = os.path.dirname(os.path.realpath(__file__))
filepath = xlsx_path + r"\test_data\pests_probability.xls"
#完整用例
workbook = open_workbook(filepath)                              #打开Excel工作簿
new_workbook = copy(workbook)                                   #复制Excel工作簿
sheetName = "Sheet1"
datas1 = ExcelUtil(filepath, sheetName).dict_data()             #获取Sheet1表格数据
new_sheet = new_workbook.get_sheet(0)                           #形成一个新的Sheet1工作簿

@ddt.ddt
class PestsProbability(unittest.TestCase):
    '''查询病虫害发生概率'''
    @classmethod
    def setUpClass(cls):
        warnings.filterwarnings(action="ignore")
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    @ddt.data(datas1[0])
    def test_01(self, testdata):
        '''数据1,15天预报数据1.txt,历史降雨数据1.txt'''
        #插入15天预报数据1.txt
        content = pdf.mock_data1()
        set_mock.mock_days(content)
        #插入历史降雨数据1.txt
        content1 = phd.mock_data1()
        set_mock.mock_days(content1)
        expect_result=pper.pests_probability_expect_result1()
        actural_result = tr.actural_result(testdata)
        print(expect_result==actural_result)    #判断预期结果是否与实际结果一致
        if expect_result==actural_result:
            print(actural_result)
            new_sheet.write(1,7,"测试通过")
        else:
            print("测试失败,实际结果与预期结果不一致!!\n实际结果:%s\n预期结果:%s\n" % (actural_result, expect_result))
            new_sheet.write(1,7,"测试失败")
        new_workbook.save(filepath)
        self.assertEqual(actural_result, expect_result)

 

posted @ 2021-01-19 17:09  Camillezxl  阅读(395)  评论(0编辑  收藏  举报