目录

unittest核心工作原理

unittest示例

示例1:简单示例

示例2:setUpClass()和tearDownClass()

示例3:通过在测试用例方法名中添加数字test_N指定执行顺序

示例4:skip跳过测试用例执行的3种方式

示例5:组织TestSuite

示例6:assert断言

断言方法说明

示例7:输出HTML报告

示例8:第一个webDriver测试

示例9:ddt数据驱动


unittest核心工作原理

转载于文章Python必会的单元测试框架 —— unittest

unittest中最核心的四个概念是:test case, test suite, test runner, test fixture

  • 一个TestCase的实例就是一个测试用例。什么是测试用例呢?就是一个完整的测试流程,包括测试前准备环境的搭建(setUp),执行测试代码(run),以及测试后环境的还原(tearDown)。单元测试(unit test)的本质也就在这里,一个测试用例是一个完整的测试单元,通过运行这个测试单元,可以对某一个问题进行验证。
  • 而多个测试用例集合在一起,就是TestSuite,而且TestSuite也可以嵌套TestSuite。
  • TestLoader是用来加载TestCase到TestSuite中的,其中有几个loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例。
  • TextTestRunner是来执行测试用例的,其中的run(test)会执行TestSuite/TestCase中的run(result)方法。
  • 测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息。
  • 而对一个测试用例环境的搭建和销毁,是一个Fixture

一个class继承了unittest.TestCase,便是一个测试用例,但如果其中有多个以 test 开头的方法,那么每有一个这样的方法,在load的时候便会生成一个TestCase实例,如:一个class中有四个test_xxx方法,最后在load到suite中时也有四个测试用例。

到这里整个流程就清楚了:

写好TestCase,然后由TestLoader加载TestCase到TestSuite,然后由TextTestRunner来运行TestSuite,运行的结果保存在TextTestResult中,我们通过命令行或者unittest.main()执行时,main会调用TextTestRunner中的run来执行,或者我们可以直接通过TextTestRunner来执行用例。这里加个说明,在Runner执行时,默认将执行结果输出到控制台,我们可以设置其输出到文件,在文件中查看结果(你可能听说过HTMLTestRunner,是的,通过它可以将结果输出到HTML中,生成漂亮的报告,它跟TextTestRunner是一样的,从名字就能看出来)。

unittest示例

示例1:简单示例

#encoding=utf-8
import unittest
import random
class TestSequenceFunctions(unittest.TestCase):def setUp(self):# 初始化一个递增序列self.seq = list(range(10))print ("setup completed!")def test_run(self):# 从序列seq中随机选取一个元素element = random.choice(self.seq)# 验证随机元素确实属于列表中self.assertTrue(element in self.seq)def test_sth(self):assert 1==2def tearDown(self):print ("tearDown completed")class TestDictValueFormatFunctions(unittest.TestCase):def setUp(self):self.seq = list(range(10))print("setup is ready!")def test_shuffle(self):# 随机打乱原seq的顺序random.shuffle(self.seq)self.seq.sort()self.assertEqual(self.seq, list(range(10)))# 验证执行函数时抛出了TypeError异常self.assertRaises(TypeError, random.shuffle, (1, 2, 3))def tearDown(self):print ("tearDown is Done!")if __name__ == '__main__':unittest.main()

执行结果:

setup is ready!
tearDown is Done!
.setup completed!
tearDown completed
.setup completed!
tearDown completed
F
======================================================================
FAIL: test_sth (__main__.TestSequenceFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):File "F:\unittest\e1_unittest_demo.py", line 17, in test_sthassert 1==2
AssertionError----------------------------------------------------------------------
Ran 3 tests in 0.027sFAILED (failures=1)

说明:

  • 可以看到一共运行了3个测试用例,2个成功,1个失败,并给出失败原因:1==2
  • 每一个用例执行的结果的标识,成功是 .,失败是 F,出错是 E,跳过是 S
  • setUp()和tearDown()分别执行了3次,在每个测试用例执行前后分别执行了setUp和tearDown方法
  • 每个测试方法均以 test 开头,否则是不被unittest识别的
  • 可以看到测试的执行跟方法写的位置顺序没有关系,测试用例的默认执行顺序是根据用例名的字母顺序来的,先执行了TestDictValueFormatFunctions类中的test_shuffle,然后执行了TestSequenceFunctions类中的test_run,最后执行了TestSequenceFunctions中类的test_sth

示例2:setUpClass()和tearDownClass()

#encoding=utf-8
import unittest# 被测试类
class myclass(object):@classmethoddef sum(self, a, b):return a + b #将两个传入参数进行相加操作@classmethoddef sub(self, a, b):return a - b #将两个传入参数进行相减操作class mytest(unittest.TestCase):@classmethoddef setUpClass(cls):#每个测试类的setUpClass方法,只会执行一次"初始化类固件"print ( "----setUpClass")@classmethoddef tearDownClass(cls):#每个测试类的tearDownClass方法,只会执行一次"重构类固件"print ( "----tearDownClass")# 初始化工作def setUp(self):#每个测试方法均会执行一次self.a = 3self.b = 1print ( "--setUp")# 退出清理工作def tearDown(self):#每个测试方法均会执行一次print ( "--tearDown")# 具体的测试用例,一定要以test开头def testsum(self):# 断言两数之和的结果是否是4self.assertEqual(myclass.sum(self.a, self.b), 4, 'test sum fail')def testsub(self):# 断言两数之差的结果是否是2self.assertEqual(myclass.sub(self.a, self.b), 2, 'test sub fail')if __name__ == '__main__':unittest.main() # 启动单元测试

执行结果:

----setUpClass
--setUp
--tearDown
.--setUp
--tearDown
.----tearDownClass----------------------------------------------------------------------
Ran 2 tests in 0.011sOK

说明:

setUpClass()和tearDownClass()分别在每个测试类执行前后各执行一次(不管这个测试类中有多少个测试方法,它们都仅执行一次);而setUp()和tearDown()分别在每个测试方法执行前后各执行一次。

示例3:通过在测试用例方法名中添加数字test_N指定执行顺序

Calc.py

# coding=utf-8
class Calc(object):def add(self, x, y, *d):# 加法计算result = x + yfor i in d:result += ireturn resultdef sub(self, x, y, *d):# 减法计算result = x - yfor i in d:result -= ireturn result@classmethoddef mul(cls, x, y, *d):# 乘法计算result = x * yfor i in d:result *= ireturn result@staticmethoddef div(x, y, *d):# 除法计算if y != 0:result = x / yelse:raise  ZeroDivisionErrorreturn -1for i in d:if i != 0:result /= ielse:raise  ZeroDivisionErrorreturn -1return resultif __name__=="__main__":c=Calc()print (c.add(1,2,3,4))#print c.add(1,2,[3,4],5,a=3))print (c.sub(1,2,3,4))print (c.mul(2,3,4))print (c.div(10,5,1))print (c.div(1,0,0))print (c.div(1,1,0))print (Calc.mul(1,2,3,4))print (Calc.div(100,10,5,1))

e3_Test_Calc_by_specific_order.py

#encoding=utf-8
import unittest
from Calc import Calcclass MyTest(unittest.TestCase):@classmethoddef setUpClass(self):print ("单元测试前,创建Calc类的实例")self.c = Calc()def test_3div(self):print ("run div()")self.assertEqual(Calc.div(8, 2, 4), 1, 'test div fail')# 具体的测试用例,一定要以test开头,执行顺序按照字母顺序开头def test_0add(self):print ("run add()")self.assertEqual(self.c.add(1, 2, 12), 15, 'test add fail')def test_1sub(self):print ("run sub()")self.assertEqual(self.c.sub(2, 1, 3), -2, 'test sub fail')def test_2mul(self):print ("run mul()")self.assertEqual(Calc.mul(2, 3, 5), 30, 'test mul fail')if __name__ == '__main__':unittest.main()# 启动单元测试

执行结果:

单元测试前,创建Calc类的实例
run add()
.run sub()
.run mul()
.run div()
.
----------------------------------------------------------------------
Ran 4 tests in 0.020sOK

可以发现执行顺序变成我们指定的0add-1sub-2mul-3div顺序

示例4:skip跳过测试用例执行的3种方式

# coding=utf-8
import random
import unittest
import sysclass TestSequenceFunctions(unittest.TestCase):a = 6def setUp(self):self.seq = list(range(10))@unittest.skip("skipping") # 无条件忽略该测试方法def test_shuffle(self):random.shuffle(self.seq)self.seq.sort()self.assertEqual(self.seq, list(range(10)))self.assertRaises(TypeError, random.shuffle, (1, 2, 3))# 如果变量a > 5,则忽略该测试方法@unittest.skipIf(a > 5, "condition is not satisfied!")def test_choice(self):element = random.choice(self.seq)self.assertTrue(element in self.seq)# 除非执行测试用例的平台是Linux平台,否则忽略该测试方法  win32是windows@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")def test_sample(self):with self.assertRaises(ValueError):random.sample(self.seq, 20)for element in random.sample(self.seq, 5):self.assertTrue(element in self.seq)if __name__ == '__main__':# unittest.main()suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)# 加载TestSequenceFunctions测试类suite = unittest.TestSuite(suite)#unittest.TextTestRunner(verbosity = 2).run(suite)

执行结果:

test_choice (__main__.TestSequenceFunctions) ... skipped 'condition is not satisfied!'
test_sample (__main__.TestSequenceFunctions) ... skipped 'requires Linux'
test_shuffle (__main__.TestSequenceFunctions) ... skipped 'skipping'----------------------------------------------------------------------
Ran 3 tests in 0.001sOK (skipped=3)

可以看到上面3个测试方法都被跳过。并打印了自定义的跳过原因信息

  • skip装饰器一共有三个 unittest.skip(reason)、unittest.skipIf(condition, reason)、unittest.skipUnless(condition, reason),skip无条件跳过,skipIf当condition为True时跳过,skipUnless当condition为False时跳过。
  • verbosity 参数可以控制输出的错误报告的详细程度,默认是 1,如果设为 0,则不输出每一用例的执行结果,即没有. F S E这种结果输出;如果设为 2,则输出详细的执行结果

示例5:组织TestSuite

testCalc.py

#encoding=utf-8
import unittest
import random
from Calc import Calcclass TestCalcFunctions(unittest.TestCase):def setUp(self):self.c=Calc()print ("setup completed!")def test_sum(self):"""TestCalcFunctions.test_sum()"""self.assertTrue(self.c.add(1,2,3,4)==10)def test_sub(self):self.assertTrue(self.c.sub(100,20,30,40)==10)def test_mul(self):self.assertTrue(self.c.mul(1,2,3,40)==240)def test_div(self):self.assertTrue(self.c.div(100,10,2)==5)def tearDown(self):print ("test completed!")def tearDown(self):print ("tearDown completed")if __name__ == '__main__':unittest.main()

e5_unittest_suite.py

#encoding=utf-8
import random
import unittest
from TestCalc import TestCalcFunctionsclass TestSequenceFunctions(unittest.TestCase):def setUp(self):self.seq = list(range(10))def tearDown(self):passdef test_choice(self):"""TestSequenceFunctions.test_choice()"""# 从序列seq中随机选取一个元素element = random.choice(self.seq)# 验证随机元素确实属于列表中self.assertTrue(element in self.seq)def test_sample(self):"""TestSequenceFunctions.test_sample()"""# 验证执行的语句是否抛出了异常with self.assertRaises(ValueError):random.sample(self.seq, 20)for element in random.sample(self.seq, 5):self.assertTrue(element in self.seq)class TestDictValueFormatFunctions(unittest.TestCase):def setUp(self):self.seq = list(range(10))def tearDown(self):passdef test_shuffle(self):"""TestDictValueFormatFunctions.test_shuffle()"""# 随机打乱原seq的顺序random.shuffle(self.seq)self.seq.sort()self.assertEqual(self.seq, list(range(10)))# 验证执行函数时抛出了TypeError异常self.assertRaises(TypeError, random.shuffle, (1, 2, 3))if __name__ == '__main__':# 根据给定的测试类,获取其中的所有以“test”开头的测试方法,并返回一个测试套件suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)suite2 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions)suite3 = unittest.TestLoader().loadTestsFromTestCase(TestCalcFunctions)# 加载当前目录下所有有效的测试模块(以test开头的文件),“.”表示当前目录# testSuite = unittest.TestLoader().discover('.') #.可以改为绝对路径# 将多个测试类加载到测试套件中suite = unittest.TestSuite([suite2, suite1,suite3])  #通过调整suit2和suite1的顺序,可以设定执行顺序# 设置verbosity = 2,可以打印出更详细的执行信息unittest.TextTestRunner(verbosity = 2).run(suite)

执行结果:

test_shuffle (__main__.TestDictValueFormatFunctions)
TestDictValueFormatFunctions.test_shuffle() ... ok
test_choice (__main__.TestSequenceFunctions)
TestSequenceFunctions.test_choice() ... ok
test_sample (__main__.TestSequenceFunctions)
TestSequenceFunctions.test_sample() ... ok
test_div (TestCalc.TestCalcFunctions) ... setup completed!
tearDown completed
ok
test_mul (TestCalc.TestCalcFunctions) ... setup completed!
tearDown completed
ok
test_sub (TestCalc.TestCalcFunctions) ... setup completed!
tearDown completed
ok
test_sum (TestCalc.TestCalcFunctions)
TestCalcFunctions.test_sum() ... setup completed!
tearDown completed
ok----------------------------------------------------------------------
Ran 7 tests in 0.011sOK

说明:

  • 我们添加到TestSuite中的case是会按照添加的顺序执行的。通过调整suite2和suite1的顺序,指定了执行的顺序。先执行的是TestDictValueFormatFunctions.test_shuffle(),然后依次执行的TestSequenceFunctions类中的test_choice()和test_sample(),最后执行的TestCalcFunctions类中的test_div()、test_mul()、test_sub()、test_sum()
  • 在测试方法下加代码示例中的”“”Doc String”“”,在用例执行时,会将该字符串作为此用例的描述并输出,加合适的注释能够使输出的测试报告更加便于阅读

示例6:assert断言

#encoding=utf-8
import unittest
import random# 被测试类
class MyClass(object):@classmethoddef sum(self, a, b):return a + b@classmethoddef div(self, a, b):return a / b@classmethoddef retrun_None(self):return None# 单元测试类
class MyTest(unittest.TestCase):# assertEqual()方法实例def test_assertEqual(self):# 断言两数之和的结果a, b = 1, 2sum = 4self.assertEqual(a + b, sum, '断言失败,%s + %s != %s' %(a, b, sum))# assertNotEqual()方法实例def test_assertNotEqual(self):# 断言两数之差的结果a, b = 5, 2res = 1self.assertNotEqual(a - b, res, '断言失败,%s - %s != %s' %(a, b, res))# assertTrue()方法实例def test_assertTrue(self):# 断言表达式的为真self.assertTrue(1 == 1, "表达式为假")# assertFalse()方法实例def test_assertFalse(self):# 断言表达式为假self.assertFalse(3 == 2, "表达式为真")# assertIs()方法实例def test_assertIs(self):# 断言两变量类型属于同一对象a = 12b = aself.assertIs(a, b, "%s与%s不属于同一对象" %(a, b))# test_assertIsNot()方法实例def test_assertIsNot(self):# 断言两变量类型不属于同一对象a = 12b = "test"self.assertIsNot(a, b, "%s与%s属于同一对象" %(a, b))# assertIsNone()方法实例def test_assertIsNone(self):# 断言表达式结果为Noneresult = MyClass.retrun_None()self.assertIsNone(result, "not is None")# assertIsNotNone()方法实例def test_assertIsNotNone(self):# 断言表达式结果不为Noneresult = MyClass.sum(2, 5)self.assertIsNotNone(result, "is None")# assertIn()方法实例def test_assertIn(self):# 断言对象A是否包含在对象B中strA = "this is a test"strB = "is"self.assertIn(strB, strA, "%s不包含在%s中" %(strB, strA))# assertNotIn()方法实例def test_assertNotIn(self):# 断言对象A不包含在对象B中strA = "this is a test"strB = "Selenium"self.assertNotIn(strB, strA, "%s包含在%s中" %(strB, strA))# assertIsInstance()方法实例def test_assertIsInstance(self):# 测试对象x的类型是否是指定的类型yx = MyClassy = objectself.assertIsInstance(x, y, "%s的类型不是%s" %(x, y))# assertNotIsInstance()方法实例def test_assertNotIsInstance(self):# 测试对象A的类型不是指定的类型a = 123b = strself.assertNotIsInstance(a, b, "%s的类型是%s" %(a, b))# assertRaises()方法实例def test_assertRaises(self):# 测试抛出的指定的异常类型# assertRaises(exception)# with self.assertRaises(TypeError) as cm:# random.sample([1,2,3,4,5], "j")# 打印详细的异常信息# print("===", cm.exception)# assertRaises(exception, callable, *args, **kwds)self.assertRaises(ZeroDivisionError, MyClass.div, 3, 0)# assertRaisesRegex()方法实例def test_assertRaisesRegex(self):# 测试抛出的指定异常类型,并用正则表达式具体验证# assertRaisesRegexp(exception, regexp)# with self.assertRaisesRegex(ValueError, 'literal') as ar:# int("xyz")# 打印详细的异常信息# print(ar.exception)# 打印正则表达式# print("re:",ar.expected_regex)# assertRaisesRegexp(exception, regexp, callable, *args, **kwds)# try:self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$", int, 'XYZ')# except AssertionError as e:# print (e)if __name__ == '__main__':# 执行单元测试unittest.main(verbosity=2)

执行结果:

test_assertEqual (__main__.MyTest) ... FAIL
test_assertFalse (__main__.MyTest) ... ok
test_assertIn (__main__.MyTest) ... ok
test_assertIs (__main__.MyTest) ... ok
test_assertIsInstance (__main__.MyTest) ... ok
test_assertIsNone (__main__.MyTest) ... ok
test_assertIsNot (__main__.MyTest) ... ok
test_assertIsNotNone (__main__.MyTest) ... ok
test_assertNotEqual (__main__.MyTest) ... ok
test_assertNotIn (__main__.MyTest) ... ok
test_assertNotIsInstance (__main__.MyTest) ... ok
test_assertRaises (__main__.MyTest) ... ok
test_assertRaisesRegex (__main__.MyTest) ... ok
test_assertTrue (__main__.MyTest) ... ok======================================================================
FAIL: test_assertEqual (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):File "F:\unittest\e6_unittest_assert-2.py", line 26, in test_assertEqualself.assertEqual(a + b, sum, '断言失败,%s + %s != %s' %(a, b, sum))
AssertionError: 3 != 4 : 断言失败,1 + 2 != 4----------------------------------------------------------------------
Ran 14 tests in 0.013sFAILED (failures=1)

断言方法说明

断言方法 断言描述 备注
assertEqual(arg1, arg2, msg=None) 验证arg1=arg2,不等则fail arg1 = arg2
assertNotEqual(arg1, arg2, msg=None) 验证arg1 != arg2, 相等则fail arg1 != arg2
assertTrue(expr, msg=None) 验证expr是true,如果为false,则fail bool(expr) IS true
assertFalse(expr,msg=None) 验证expr是false,如果为true,则fail bool(expr) IS false
assertIs(arg1, arg2, msg=None) 验证arg1、arg2是同一个对象,不是则fail arg1 is arg2
assertIsNot(arg1, arg2, msg=None) 验证arg1、arg2不是同一个对象,是则fail arg1 not is arg2
assertIsNone(expr, msg=None) 验证expr是None,不是则fail expr is one
assertIsNotNone(expr, msg=None) 验证expr不是None,是则fail expr not is one
assertIn(arg1, arg2, msg=None) 验证arg1是arg2的子串,不是则fail arg1 in arg2
assertNotIn(arg1, arg2, msg=None) 验证arg1不是arg2的子串,是则fail arg1 not in arg2
assertIsInstance(obj, cls, msg=None) 验证obj是cls的实例,不是则fail IsInstance(obj,cls)
assertNotIsInstance(obj, cls, msg=None) 验证obj不是cls的实例,是则fail not IsInstance(obj,cls)

更多方法说明请参考python的unittest单元测试框架断言整理汇总

示例7:输出HTML报告

HTMLTestRunner是一个第三方的unittest HTML报告库,首先我们下载HTMLTestRunner.py,并放到当前目录下,或者你的’C:\Python310\Lib’下,就可以导入运行了。

下载地址:

官方原版:HTMLTestRunner - tungwaiyip's software

# coding=utf-8
import unittest
import HTMLTestRunner
import mathclass Calc(object):def add(self, x, y, *d):# 加法计算result = x + yfor i in d:result += ireturn resultdef sub(self, x, y, *d):# 减法计算result = x - yfor i in d:result -= ireturn resultclass SuiteTestCalc(unittest.TestCase):def setUp(self):self.c = Calc()@unittest.skip("skipping")def test_Sub(self):print ("sub")self.assertEqual(self.c.sub(100, 34, 6), 61, u'求差结果错误!')def testAdd(self):print ("add")self.assertEqual(self.c.add(1, 32, 56), 89, u'求和结果错误!')class SuiteTestPow(unittest.TestCase):def setUp(self):self.seq = list(range(10))# @unittest.skipIf()def test_Pow(self):print ("Pow")self.assertEqual(pow(6, 3), 2161, u'求幂结果错误!')def test_hasattr(self):print ("hasattr")# 检测math模块是否存在pow属性self.assertTrue(hasattr(math, 'pow1'), u"检测的属性不存在!")if __name__ == "__main__":suite1 = unittest.TestLoader().loadTestsFromTestCase(SuiteTestCalc)suite2 = unittest.TestLoader().loadTestsFromTestCase(SuiteTestPow)suite = unittest.TestSuite([suite1, suite2])#unittest.TextTestRunner(verbosity=2).run(suite)filename = "test.html"  # 定义个报告存放路径,支持相对路径。# 以二进制方式打开文件,准备写fp = open(filename, 'wb')# 使用HTMLTestRunner配置参数,输出报告路径、报告标题、描述,均可以配runner = HTMLTestRunner.HTMLTestRunner(stream = fp,title = '测试报告', description = '测试报告内容')# 运行测试集合runner.run(suite)fp.close()

执行结果:

示例8:第一个webDriver测试

#encoding=utf-8
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import HTMLTestRunnerclass GloryRoad(unittest.TestCase):def setUp(self):# 启动Firefox浏览器self.driver = webdriver.Chrome(executable_path = r"E:\Programs\Driver\chromedriver")def testSoGou(self):# 访问搜狗首页self.driver.get("http://sogou.com")# 清空搜索输入框默认内容self.driver.find_element(By.ID,"query").clear()# 在搜索输入框中输入“光荣之路自动化测试”self.driver.find_element(By.ID,"query").send_keys("WebDriver实战宝典")# 单击“搜索”按钮self.driver.find_element(By.ID,"stb").click()# 等待3秒time.sleep(3)assert "吴晓华" in self.driver.page_source, "页面中不存在要寻找的关键字!".encode("gbk")def testBing(self):# 访问bing首页self.driver.get("http://cn.bing.com")# 清空搜索输入框默认内容self.driver.find_element(By.ID, "sb_form_q").clear()# 在搜索输入框中输入“光荣之路自动化测试”self.driver.find_element(By.ID, "sb_form_q").send_keys("WebDriver实战宝典")# 单击“搜索”按钮,目前已经不能点击该label# self.driver.find_element(By.ID, "sb_form_go").click()# 按"回车"搜索self.driver.find_element(By.ID, "sb_form_q").send_keys(Keys.ENTER)# 等待3秒time.sleep(3)assert "吴晓华" in self.driver.page_source, "页面中不存在要寻找的关键字!".encode("gbk")def tearDown(self):# 退出浏览器self.driver.quit()if __name__ == '__main__':# unittest.main()suite = unittest.TestLoader().loadTestsFromTestCase(GloryRoad)suite = unittest.TestSuite(suite)#unittest.TextTestRunner(verbosity=2).run(suite)filename = "e9_test.html"  # 定义个报告存放路径,支持相对路径。# 以二进制方式打开文件,准备写fp = open(filename, 'wb')# 使用HTMLTestRunner配置参数,输出报告路径、报告标题、描述,均可以配runner = HTMLTestRunner.HTMLTestRunner(stream = fp,title = '测试报告', description = '测试报告内容')# 运行测试集合runner.run(suite)fp.close()

执行结果:

示例9:ddt数据驱动

安装:

py -3 -m pip install ddt

import unittest
import ddt@ddt.ddt
class Praddt(unittest.TestCase):def setUp(self):print("my test start!")def tearDown(self):print("my test complete!")@ddt.data(["wulaoshi","pass123", "success"],["lilaoshi","pwd123", "success"],["zhanglaoshi", "1qaz", "ERROR"]) #一个方括号是一组测试数据@ddt.unpackdef test_ddt(self, user, passwd, expect_value):login_info = {"wulaoshi":"pass123","lilaoshi":"pwd123"}print(user,passwd,expect_value)if user in login_info:if passwd == login_info[user]:result = "success" else:result = "ERROR"else:result = "ERROR"assert result==expect_valueif __name__ == '__main__':# 执行单元测试unittest.main()

执行结果:

my test start!
wulaoshi pass123 success
my test complete!
.my test start!
lilaoshi pwd123 success
my test complete!
.my test start!
zhanglaoshi 1qaz ERROR
my test complete!
.
----------------------------------------------------------------------
Ran 3 tests in 0.026sOK

python单元测试框架—unittest相关推荐

  1. python单元测试框架Unittest详解

    前言 我们今天来聊聊Python中的单元测试框架unittest,大家都知道在Python中比较热门的测试框架除了pytest就是unittest,我之前有讲过pytest所以今天想讲unittest ...

  2. python单元测试框架unittest介绍和使用_Python+Selenium框架设计篇之-简单介绍unittest单元测试框架...

    前面文章已经简单介绍了一些关于自动化测试框架的介绍,知道了什么是自动化测试框架,主要有哪些特点,基本组成部分等.在继续介绍框架设计之前,我们先来学习一个工具,叫unittest. unittest是一 ...

  3. Python单元测试框架 unittest详解

    一 整体结构概览 unittest原名为PyUnit,是由java的JUnit衍生而来.对于单元测试,需要设置预先条件,对比预期结果和实际结果. TestCase :通过继承TestCase类,我们可 ...

  4. python单元测试框架unittest介绍和使用_Python单元测试框架unittest简明使用实例

    测试步骤1. 导入unittest模块 import unittest 2. 编写测试的类继承unittest.TestCase class Tester(unittest.TestCase) 3. ...

  5. Python单元测试及unittest框架用法实例解析

    例题取用登录模块:代码如下 def login_check(username,password):''' 登录校验的函数:param username:账号:param password: 密码:re ...

  6. python写软件测试用例_Python单元测试框架unittest:单个测试用例编写步骤及实例...

    一.Python单元测试框架的编写步骤 导入模块 必须继承unittest.TestCase 主要是配置环境:进行测试前的初始化工作,比如在接口测试前面做一些前置的参数赋值,数据库操作等等 定义测试用 ...

  7. python单元测试框架之unittest和pytest的区别

    前言 今天呢笔者想和大家来聊聊Python单元测试框架,我们都知道python单元测试框架有很多,大家平时经常使用的是unittest,因为它比较基础,并且可以进行二次开发,如果你的开发水平很高,集成 ...

  8. Python单元测试框架Pyunit 的使用

    Python单元测试框架Pyunit 使用示例: 1 import unittest 2 3 class Person: 4 def age(self): 5 return 34 6 def name ...

  9. 【整理】Python 单元测试框架 - PyUnit

    1 概况 Python单元测试框架(The Python unit testing framework),简称为PyUnit, 是Kent Beck和Erich Gamma这两位聪明的家伙所设计JUn ...

最新文章

  1. OSChina 周六乱弹 ——土肥圆装高富帅相亲节目现场拆穿
  2. 合作伙伴:VMware收购Wavefront提供强大的多云应用管理渠道
  3. iOS_11_tableViewCell使用alertView变更数据
  4. socketserver 源码浅读
  5. drools 7.x定时器
  6. mysql 存储过程参数集合_MySQL存储过程
  7. 解决MySQL无法正常启动的问题 Can't connect to MySQL server on 'localhost'(10061)
  8. ul在Firefox和IE下的不同表现
  9. Python_Mix*生成器,生成器函数,推导式,生成器表达式
  10. EasyExcel 并发读取文件字段并进行校验,数据写入到新文件,批量插入数据到数据库
  11. 妙趣横生的算法(C语言实现 第2版)pdf
  12. JS实现继承的几种方法总结
  13. Ubuntu下安装小企鹅fcitx输入法
  14. 萤石云视频Android SDK接口使用说明
  15. android10获取WiFi名称 已经连接的WiFi名称 SSID
  16. git commit三种回退的方式
  17. 美国的网络安全战略和人才战略简析
  18. 【python】数据分析实战:分析全国旅游景点数据,暑假还不知道去哪玩的看过来
  19. 咖啡店招牌:燕麦奶咖啡商用教程 | 埃德珈培训出品
  20. git_v1.txt

热门文章

  1. 区块链技术如何应用于版权保护?
  2. Android实现GridView的item长按拖动删除完美实现(带动画效果)
  3. 【振动幅值信号分析】Fluke 810测振仪、您贴身的振动诊断专家
  4. SAP 物料的分割评估
  5. Linux圈子里的“鲁大师“vmstat-尚文网络xUP楠哥
  6. 从手机到平板,华为凭啥总能超越苹果?
  7. android 屏幕录制方案,ShareREC for Android全系统录屏原理解析
  8. Google Play Game Services
  9. 吹牛的资本之Hibernate框架,五分钟搞定Hibernate...
  10. ULID 与 UUID:用于 JavaScript 的可排序随机 ID 生成器