zoukankan      html  css  js  c++  java
  • 单元测试

    import unittest

    def calc(a,b):
    return round(a/b,1)

    res=calc(1,2)
    calc()
    calc('s','22')
    calc(1,0)




    import unittest

    def calc(a,b):
    return round(a/b,1)

    class TestCalc(unittest.TestCase): #继承TestCase
    def testlw(self): #必须以小写test开头才能运行,不写test不运行用例
    res=calc(1,2)
    self.assertEqual(0.5,res,'计算不正确') #assert开头的校验结果

    def testkk(self):
    res=calc(1,0)
    self.assertIsNone(res,'原因')

    unittest.main() #会运行当前pythony文件里面的所有用例




    import unittest

    class TestCalc(unittest.TestCase): #继承TestCase
    def testlw(self): #必须以小写test开头才能运行,不写test不运行用例
    self.assertEqual(1,1) #assert开头的校验结果

    def testkk(self):
    self.assertEqual(1, 2)

    unittest.main() #会运行当前pythony文件里面的所有用例






    import unittest
    import HTMLTestRunner

    class TestCalc(unittest.TestCase): #继承TestCase
    def testlw(self): #必须以小写test开头才能运行,不写test不运行用例
    '''lW测试''' #加测试描述
    self.assertEqual(1,1) #assert开头的校验结果

    def testkk(self):
    '''KK测试'''
    self.assertEqual(1, 2)

    # 用例集 测试套件
    #存放测试用例的

    # unittest.main() #会运行当前pythony文件里面的所有用例

    # 1、先把所有的测试用例都放到用例集
    # 2、运行这些用例集
    # 3、产生报告
    suite=unittest.TestSuite() #测试集合
    suite.addTests(unittest.makeSuite(TestCalc))
    #把刚才写的用例加进来 #把类名传进来(先把用例放到用例集合里,才能把用例加进来)f=open('test.html','wb')runner=HTMLTestRunner.HTMLTestRunner(f,title='双鱼座测试标题',description='用例描述')runner.run(suite)#运行测试用例import unittestimport HTMLTestRunnerclass TestCalc(unittest.TestCase): #继承TestCase def testlw(self): #必须以小写n # test开头才能运行,不写test不运行用例 '''lW测试''' #加测试描述 self.assertEqual(1,1) #assert开头的校验结果 def testkk(self): '''KK测试''' #产生的报告按首字母进行排序 self.assertEqual(1, 2) def testa(self): '''testa测试''' print('testa') #产生的报告按首字母进行排序 self.assertEqual(1,1) def testc(self): '''tstc测试''' print('testc') #产生的报告按首字母进行排序 self.assertEqual(1,2)suite=unittest.TestSuite() #测试集合suite.addTests(unittest.makeSuite(TestCalc))#把刚才写的用例加进来 #把类名传进来(先把用例放到用例集合里,才能把用例加进来)f=open('test.html','wb')runner=HTMLTestRunner.HTMLTestRunner(f,title='双鱼座测试标题',description='用例描述')runner.run(suite)#运行测试用例import unittestimport HTMLTestRunnerclass TestCalc(unittest.TestCase): #继承TestCase def setUp(self): #初始化 每个用例运行之前运行的 (比如在数据库插入SQL) print('setup是什么时候运行') #setup初始化工作 def tearDown(self):#每个用例运行之后运行的 (删除插入的数据SQL) print('teardown是什么时候运行') #teardown收尾工作 def testlw(self): #必须以小写test开头才能运行,不写test不运行用例 print('lw') self.assertEqual(1,1) #assert开头的校验结果 def testkk(self): print('kk') self.assertEqual(2, 2) def testa(self): print('testa') #产生的报告按首字母进行排序 self.assertEqual(3,3) def testc(self): print('testc') #产生的报告按首字母进行排序 self.assertEqual(5,5)unittest.main() # 会运行当前pythony文件里面的所有用例import unittestimport HTMLTestRunnerclass TestCalc(unittest.TestCase): #继承TestCase def setUp(self): #初始化 每个用例运行之前运行的 (比如在数据库插入SQL) print('setup是什么时候运行') #setup初始化工作 def tearDown(self):#每个用例运行之后运行的 (删除插入的数据SQL) print('teardown是什么时候运行') #teardown收尾工作 @classmethod #类方法 def setUpClass(cls): #可以写个判断,比如都是测试登录接口,可以看看能否访问成功 print('setupclass什么时候运行') #在所有用例执行之前运行 @classmethod #类方法 def tearDownClass(cls): print('tearDownClass什么时候运行')#在所有用例执行完之后运行 def testlw(self): #必须以小写test开头才能运行,不写test不运行用例 print('lw') self.assertEqual(1,1) #assert开头的校验结果 def testkk(self): print('kk') self.assertEqual(2, 2) def testa(self): print('testa') #产生的报告按首字母进行排序 self.assertEqual(3,3) def testc(self): print('testc') #产生的报告按首字母进行排序 self.assertEqual(5,5)unittest.main() # 会运行当前pythony文件里面的所有用例import unittestimport HTMLTestRunnerfrom BeautifulReport import BeautifulReport as bf #起个别名class TestCalc(unittest.TestCase): #继承TestCase def setUp(self): #初始化 每个用例运行之前运行的 (比如在数据库插入SQL) print('setup是什么时候运行') #setup初始化工作 def tearDown(self):#每个用例运行之后运行的 (删除插入的数据SQL) print('teardown是什么时候运行') #teardown收尾工作 @classmethod #类方法 def setUpClass(cls): #可以写个判断,比如都是测试登录接口,可以看看能否访问成功 print('setupclass什么时候运行') #在所有用例执行之前运行 @classmethod #类方法 def tearDownClass(cls): print('tearDownClass什么时候运行')#在所有用例执行完之后运行 def testlw(self): #必须以小写test开头才能运行,不写test不运行用例 '''lw测试''' print('lw') self.assertEqual(1,1) #assert开头的校验结果 def testkk(self): '''KK测试''' print('kk') self.assertEqual(2, 2) def testa(self): '''testa测试''' print('testa') #产生的报告按首字母进行排序 self.assertEqual(3,3) def testc(self): '''testc测试''' print('testc') #产生的报告按首字母进行排序 self.assertEqual(5,5)suite=unittest.TestSuite() #测试集合suite.addTest(unittest.makeSuite(TestCalc)) #把刚才写的用例加进来run=bf(suite) #实例化BeautifulReport as bf 把suite传进来run.report(description='描述必须写',filename='test') #description必须写
  • 相关阅读:
    EntityFramework优缺点
    领导者与管理者的区别
    七个对我最好的职业建议(精简版)
    The best career advice I’ve received
    Difference between Stored Procedure and Function in SQL Server
    2015年上半年一次通过 信息系统项目管理师
    Difference between WCF and Web API and WCF REST and Web Service
    What’s the difference between data mining and data warehousing?
    What is the difference between a Clustered and Non Clustered Index?
    用new创建函数的过程发生了什么
  • 原文地址:https://www.cnblogs.com/jiadan/p/9201417.html
Copyright © 2011-2022 走看看