生成自動化測試報告

第一步:打開pycharm.點擊file-》new project,輸入項目名稱gxtest,點擊確定

第二步:右擊新建的項目,點擊new->python package,輸入文件夾名稱case,點擊確定

第三步:右擊case文件夾,新建兩個文件夾counter、print;

第四步:開始創建用例文件。分別在counter和print文件夾下面新建 test_01.py,test_02.py,test_03.py文件,用來寫用例

第五步:寫用例。test_01.py代碼:

import unittest
import time
class Test(unittest.TestCase):
    def testMinus(self):
        u'''這裏是減法測試'''
        result = 6-5
        hope = 1
        self.assertEqual(result, hope)
    def testAdd(self):
        u'''這裏是加法測試'''
        result = 6 + 5
        hope = 11
        self.assertEqual(result, hope)
if __name__=='__main__':
    unittest.main()

test_02.py的代碼

import unittest
import time
class Test(unittest.TestCase):
    def testDivide(self):
        u'''這裏是除法測試'''
        result = 7/2
        hope = 3
        self.assertEqual(result, hope)
    def testcheng(self):
        u'''這裏是乘法測試'''
        result = 7 * 2
        hope = 14
        self.assertEqual(result, hope)
if __name__=='__main__':
    unittest.main()

test_03.py的代碼

import unittest
import time
class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("start!")
    @classmethod
    def tearDownClass(cls):
        time.sleep(1)
        print("end!")
    def test01(self):
        print("執行測試用例01")
    def test02(self):
        print("執行測試用例02")
if __name__ == "__main__":
        unittest.main()

第六步:批量執行用例

右擊項目gxtest,新建一個run_all_case.py文件,代碼如下:

import unittest
import os
import HTMLTestRunner
# 用例路徑
case_path = os.path.join(os.getcwd(), "case")
# 報告存放路徑
report_path = os.path.join(os.getcwd(), "report")
def all_case():
    discover = unittest.defaultTestLoader.discover(case_path, pattern="test*.py", top_level_dir=None)
    print(discover)
    return discover
if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    runner.run(all_case())

第七步:生成HTML的測試報告

1.下載HTMLTestRunner,放在Python安裝目錄lib下面,步驟參考https://www.cnblogs.com/feiquan/p/8525903.html。如果無法下載,可自行在lib下面新建HTMLTestRunner.py文件,把代碼拷進去。(python3.X需要修改代碼,參考上述網址,其中775行應該是772行)

2.右擊新建的項目,點擊new->python package,輸入文件夾名稱report,點擊確定,在report下面新建result.html文件

第八步:在run_all_case中修改如下代碼即可,點擊運行

第九步:查看報告

找到result.html文件,在瀏覽器中打開,即可查看網頁版自動化測試報告

下面是網頁版自動化測試報告:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章