Python + HTML testrunner + single and multiple test sets

1. Single test set

# coding:utf-8
import json, os, sys, unittest,HTMLTestRunner,requests

reload(sys)
sys.setdefaultencoding('utf8')
class ServiceTest(unittest.TestCase):

    def setUp(self):
        self.url ='www.baidu.com'

    def test_get_service_list(self):
        data={}
        r=requests.get('http://www.cnblogs.com/xiyozhang136/p/7427730.html')

    def tearDown(self):
        pass

if __name__ =='__main__':
    testsuite = unittest.TestSuite()
    testsuite.addTest(ServiceTest("test_get_service_list"))
    runner = unittest.TextTestRunner()
    fp = file('ttt.html', 'wb')
    #You can use print
    runner.run = unittest.TextTestRunner(verbosity=2).run(testsuite)
    #Output test report
    # runner = HTMLTestRunner.HTMLTestRunner(
    #     stream=fp,
    #     title=u 'interface test report',
    #     description=u 'execution of use case:')

    # fp = open('res.html','wb')#Open an html file to save the results
    # Runner = htmltestrunner.htmltestrunner (stream = FP, title = API test report ', description = test status')
    # runner.run(testsuite)

Here, select unittest.TextTestRunner(verbosity=2).run(testsuite), the console can display the print ed content. If HTML testrunner is selected, the console will not output the content, and output the content in the test report

  1. Run multiple test sets
    The directory structure is as follows:

test1 is as follows

#coding:utf-8
import unittest
import time

class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("Start test 1")
    @classmethod
    def tearDownClass(cls):
         print("End of test")
    def setUp(self):
         print("Start a single test case")
    def tearDown(self):
         print("End a single test case")


    def test01(self):
        '''Test 0201'''
        print("Execute test case 1")
    def test02(self):
        '''Test 0202'''
        print("Execute test case 01")

if __name__=="__main__":
    unittest.main()

test2 is as follows

#coding:utf-8
import unittest
import time

class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("Start test 2")
    def tearDown(self):
        print("End of test 2")
    def test01(self):
        '''Test 0201'''
        print("Execute test case 2")
    def test02(self):
        '''Test 0202'''
        print("Execute test case 02")

if __name__=="__main__":
    unittest.main()
  • setUpClass the start of a test set
  • Teardown class the end of a test set
  • setUp starts a single test case run
  • tearDown ends a single test case run

Run all test as follows

#coding:utf-8
import unittest
import os,sys
import HTMLTestRunner
#from send_email import main2
#Catalog of use cases to be executed
reload(sys)
sys.setdefaultencoding('utf8')
def allcase():
    #Just write the path
    case_dir="/test"
    #case_path=os.path.join(os.getcwd(),"case")
    testcase=unittest.TestSuite()
    discover=unittest.defaultTestLoader.discover(case_dir,
                                                 pattern='test*.py',
                                                 top_level_dir=None)
    #Use cases filtered by discover method are added to the test suite circularly
    #print(discover)
    for test_suite in discover:
        for test_case in test_suite:
            #Add use case to testcase
            print(test_case)
            testcase.addTest(test_case)
    return testcase
if __name__=="__main__":
    runner=unittest.TextTestRunner()
    runner.run(allcase())
    report_path="test.html"
    fp=open(report_path,"wb")
    runner=HTMLTestRunner.HTMLTestRunner(stream=fp,
                                         title="automated testing  unittest Test Framework Report",
                                         description="Case execution:")

    runner.run(allcase())
    fp.close()

Tags: JSON

Posted on Sun, 05 Jan 2020 20:28:07 -0800 by denniston