TestCase:

一段Testcase代码示例:

# -*- coding: utf-8 -*-
'''
Created on 2019-6-27

@author: adminstrator
'''
import unittest


class UnitTestDemo1(unittest.TestCase):


    def setUp(self):
        print('Run before test')


    def tearDown(self):
        print('Run after test')


    def testCase(self):
        print('test case....')


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

断言:

# -*- coding: utf-8 -*-
'''
Created on 2019-6-27

@author: adminstrator
'''
import unittest


class AssertTestCase(unittest.TestCase):
    '''
          断言演示
    '''


    def setUp(self):
        print('Run befor testcase')


    def tearDown(self):
        print('Run after testcase')


    def testAssert(self):
        num1=12
        num2=10

        self.assertEqual(num1, num2, '两个数不相等') #最后一个参数msg是当测试用例不通过时输出的内容,当断言不通过时,不会继续执行下面的内容
        self.assertNotEqual(num1, num2, '两个数相等') #self.调用类的私有方法或私有变量

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

TestSuite:

# -*- coding: utf-8 -*-
'''
Created on 2019-6-28

@author: adminstrator
'''
import unittest


class UserCase(unittest.TestCase):


    def testAddUser(self):
        print('add a user')


    def testDelUser(self):
        print('delete a user')



if __name__ == "__main__":
    
    #下面是3种将TestCase加到TestSuite中的方法
    
    suite1=unittest.TestSuite(map(UserCase,['testAddUser','testDelUser']))
    
    suite2=unittest.TestSuite()
    suite2.addTests(map(UserCase,['testAddUser','testDelUser']))
    
    suite3=unittest.TestSuite()
    suite3.addTest(UserCase('testAddUser'))
    suite3.addTest(UserCase('testDelUser'))
    
    trunner=unittest.TextTestRunner() #通过TextTestRunner执行TestSuite
    trunner.run(suite1)
    

TextTestRunner:

 TestCase与TestSuite单独文件的示例:

# -*- coding: utf-8 -*-
'''
Created on 2019-6-28

@author: adminstrator
'''
import unittest


class UnitTestDemoShow(unittest.TestCase):


    def setUp(self):
        print('before testcase run')


    def tearDown(self):
        print('after testcase run')


    def testOne(self):
        str1='hello'
        str2='hello everyone'
        self.assertIn(str1, str2, '判断包含')
        
    def testTwo(self):
        num1='10'
        num2='10'
        self.assertEqual(num1, num2, '判断相等')


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
# -*- coding: utf-8 -*-
'''
Created on 2019-6-28

@author: adminstrator
'''
import unittest
from UnitTestDemoShow import UnitTestDemoShow


class TestSuiteFile(unittest.TestCase):


    def RunTestSuite(self):
        testsuite=unittest.TestSuite()
        testsuite.addTest(UnitTestDemoShow('testOne'))
        testsuite.addTest(UnitTestDemoShow('testTwo'))
        
        runner=unittest.TextTestRunner()
        runner.run(testsuite)


if __name__ == "__main__":
    
    t=TestSuiteFile()
    t.RunTestSuite()

 HTMLTestRunner下载地址:

http://tungwaiyip.info/software/HTMLTestRunner.html

下载后存放在\python\Lib\site-packages下

验证是否导入成功:

 

python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决

1.原因是官网的是python2语法写的,看官手动把官网的HTMLTestRunner.py改成python3的语法:

2.修改汇总:

第94行,将import StringIO修改成import io

第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()

第642行,将if not rmap.has_key(cls):修改成if not cls in rmap:

第766行,将uo = o.decode('latin-1')修改成uo = e

第772行,将ue = e.decode('latin-1')修改成ue = e

第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

生成测试报告(TestCase还是使用之前的):

# -*- coding: utf-8 -*-
'''
Created on 2019-6-28

@author: adminstrator
'''
import unittest
from UnitTestDemoShow import UnitTestDemoShow
import time,os
import HTMLTestRunner

class HTMLReportDemo(unittest.TestCase):


    def RunTestSuite(self):
        testsuite=unittest.TestSuite()
        testsuite.addTest(UnitTestDemoShow('testOne'))
        testsuite.addTest(UnitTestDemoShow('testTwo'))
        
        #利用HTMLTestRunner生成测试报告
        reportfile="./reports/"+str(int(round(time.time()*1000)))
        os.mkdir(reportfile)
        filename=reportfile+"report.html"
        fp=open(filename,'wb')
        runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'HTMLTestRunner测试报告',description=u'这是一个HTMLTestRunner测试报告')
        result=runner.run(testsuite)

if __name__ == "__main__":
    
    t=HTMLReportDemo()
    t.RunTestSuite()