python3+unittest+HTMLTestRunner

Posted 强仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3+unittest+HTMLTestRunner相关的知识,希望对你有一定的参考价值。

参考博客1

参考博客2

python3版HTMLTestRunner.py见博客园‘链接’(已经上传到博客园)

import unittest


class operatinon_unittest(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_one(self):
        print(\'test_one函数\')
    def test_two(self):
        print(\'test_two函数\')


if __name__ == \'__main__\':
    unittest.main
demo

 

 

import unittest
import HTMLTestRunner


class operatinon_unittest(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_one(self):
        print(\'test_one函数\')
    def test_two(self):
        print(\'test_two函数\')


class operatinon_unittest1(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_three(self):
        print(\'test_three函数\')
    def test_four(self):
        print(\'test_four函数\')


if __name__ == \'__main__\':
    suite = unittest.TestLoader().loadTestsFromTestCase(operatinon_unittest)
    unittest.TextTestRunner(verbosity=2).run(suite)
运行单个类
import unittest
import HTMLTestRunner


class operatinon_unittest(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_one(self):
        print(\'test_one函数\')
    def test_two(self):
        print(\'test_two函数\')


class operatinon_unittest1(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_three(self):
        print(\'test_three函数\')
    def test_four(self):
        print(\'test_four函数\')


if __name__ == \'__main__\':
    suite1 = unittest.TestLoader().loadTestsFromTestCase(operatinon_unittest)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(operatinon_unittest1)
    suite=unittest.TestSuite([suite1,suite2])
    unittest.TextTestRunner(verbosity=2).run(suite)
运行多个类

 

import HTMLTestRunner
import unittest
class MyTest(unittest.TestCase):#继承unittest.TestCase
    def tearDown(self):
        #每个测试用例执行之后做操作
        print(\'111\')
    def setUp(self):
        #每个测试用例执行之前做操作
        print(22222)
    def test_run(self):
        # self.assertEqual(1,1)
        self.assertIs(1,1)
        #测试用例
    def test_run2(self):
        # self.assertEqual(1,1)
        self.assertIs(1,1)
        #测试用例
    def test_run3(self):
        # self.assertEqual(1,1)
        self.assertIs(1,1)
        #测试用例
    def test_run1(self):
        # self.assertEqual(1,1)
        self.assertIs(1,1)
        #测试用例
if __name__ == \'__main__\':
    test_suite = unittest.TestSuite()#创建一个测试集合
    # test_suite.addTest(MyTest(\'test_run1\'))#测试套件中添加测试用例
    test_suite.addTest(MyTest())#测试套件中添加测试用例
    #test_suite.addTest(unittest.makeSuite(MyTest))#使用makeSuite方法添加所有的测试方法
    fp = open(\'res.html\',\'wb\')#打开一个保存结果的html文件
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=\'api测试报告\',description=\'测试情况\')
    #生成执行用例的对象
    runner.run(test_suite)
    #执行测试套件
测试报告demo
import unittest
import HTMLTestRunner


class operatinon_unittest(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_one(self):
        print(\'test_one函数\')
    def test_two(self):
        print(\'test_two函数\')


class operatinon_unittest1(unittest.TestCase):
    def setUp(self):
        print(\'每个用例之前执行setup\')
    def tearDown(self):
        print(\'每个用例执行之后\')

    def test_three(self):
        print(\'test_three函数\')
    def test_four(self):
        print(\'test_four函数\')


if __name__ == \'__main__\':
    suite1 = unittest.TestLoader().loadTestsFromTestCase(operatinon_unittest)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(operatinon_unittest1)
    suite=unittest.TestSuite([suite1,suite2])
    fp = open(\'res.html\', \'wb\')  # 打开一个保存结果的html文件
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=\'测试报告\', description=\'测试情况\')
    # 生成执行用例的对象
    runner.run(suite)
    # 执行测试套件
多个类测试报告

 

以上是关于python3+unittest+HTMLTestRunner的主要内容,如果未能解决你的问题,请参考以下文章

Python3的unittest用例按编写顺序执行

python3 unittest框架失败重跑加截图支持python2,python3

Python3+selenium-unittest执行顺序

Python3-unittest测试框架之测试用例类

Python3+selenium-unittest之装饰器(@classmethod)

python3+unittest单元测试框架