tesecase文件夹是放测试用例的地方,现在我们完善testLogin.py文件,代码:
# coding: utf-8
import json
import logging
import unittest
import setting
import sys
reload(sys)
from common.service import excel_case_data
from common.integretion import HTMLTestRunner
from common.module import environment_module
class LoginTest(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# cls.
# cls.
#
# @classmethod
# def tearDownClass(cls):
# pass
def setUp(self):
self.sheet_index = 0
self.excel_data = excel_case_data.ExcelData()
# excel文件位置
self.file_name = "C:\\Users\\DELL\\Desktop\\InterfaceTest\\data\\testcase.xlsx"
logging.info("======This is setUp function======")
def tearDown(self):
logging.info("======This is tearDown function======")
def test_case_01(self):
inputData = self.excel_data.get_case_input(self.file_name, sheet_index=self.sheet_index, row_id=1)
if inputData != '':
excelData_input = json.loads(inputData)
else:
excelData_input = None
responseData = self.excel_data.get_case_data(self.file_name, sheet_index=self.sheet_index, row_id=1 ,data=excelData_input)
def test_case_02(self):
inputData = self.excel_data.get_case_input(self.file_name, sheet_index=self.sheet_index, row_id=2)
if inputData != '':
excelData_input = json.loads(inputData)
else:
excelData_input = None
responseData = self.excel_data.get_case_data(self.file_name, sheet_index=self.sheet_index, row_id=2 ,data=excelData_input)
if __name__ == '__main__':
# runner = unittest.TestLoader().loadTestsFromTestCase(LoginTest)
# unittest.TextTestRunner(verbosity=2).run(runner)
unittest.main()
用例写完以后,只需要写一个运行所有用例的方法就可以了,写了好多次了,run_all_tests.py文件:
# coding: utf-8
import unittest
from common.module import email_module
import time
def all_case():
# 你的文件路径
case_dir = r"C:\Users\DELL\Desktop\InterfaceTest\testcase"
discover = unittest.defaultTestLoader.discover(case_dir, pattern="test*.py", top_level_dir=None)
return discover
if __name__ == '__main__':
# 导入HTMLTestRunner模块
import HTMLTestRunner
# now = time.strftime("%Y-%m-%d-%H_%M_%S")
report_path = r"C:\Users\DELL\Desktop\InterfaceTest\report\report.html"
fp = open(report_path, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"测试报告", description=u"用例执行情况")
runner.run(all_case())
fp.close()
# 调用封装好的sendMail方法,参数为上面的文件
mail = email_module.Email_Send_Module().SendEmail(report_path)
print "Email sending Success"
OK,代码就这么多了。到现在为止,我们的一个接口测试框架就完成了~~当然这个框架其实还包括读testlink上面的接口,然后用我们的用例去测试,再自动发布到jira上面,但是考虑到大家刚刚入门,就把那些复杂的都切掉了。大家可以自己找一个接口试一下哦~~~如果遇到问题可以留言。明天我们再对这个框架进行详细的讲解一下,方便大家理解~~~
联系客服