--判断是否不为None:obj is not None unittest所有断言方法 1.下面是unittest框架支持的所有断言方法,有兴趣的同学可以慢慢看。 | assertAlmostEqual(self, first, second, places=None, msg=None, delta=None) | Fail if the two objects are unequal as determined by their | difference rounded to the give...
我们可以使用assertRaises来测试这个方法。 classCalculator:defdivide(self,x,y):ify==0:raiseZeroDivisionError("除数不能为0")returnx/yimportunittestclassCalculatorTestCase(unittest.TestCase):deftest_divide(self):calculator=Calculator()self.assertRaises(ZeroDivisionError,calculator.divide,10,0)if__name__=='...
assertRaises(ValueError): Calculator.divide(6, 0) class MyTestCase(unittest.TestCase): @classmethod def setUpClass(cls): # 这个方法会在所有测试方法执行之前被调用一次 print("Setting up the test class") @classmethod def tearDownClass(cls): # 这个方法会在所有测试方法执行之后被调用一次 print("...
--判断是否不为None:obj is not None 四、unittest所有断言方法 1.下面是unittest框架支持的所有断言方法,有兴趣的同学可以慢慢看。 | assertAlmostEqual(self, first, second, places=None, msg=None, delta=None) | Fail if the two objects are unequal as determined by their | difference rounded to the...
self.assertRaises(KeyError, self.test_class.raise_error) def tearDown(self): del self.test_class 这个测试用例包含几个需要关注的点: 继承 测试方法名称 setUp tearDown 断言 下面以此来说一下上述提及的这几点。 继承 unittest提供一个基类TestCase,如果我们要编写一个测试用例,就需要继承这个抽象基类,这样当...
the_exception = cm.exception self.assertEqual(the_exception.error_code,3) 在3.1 版中更改:增加了使用能力unittest.TestCase.assertRaises作为上下文管理器。 在3.2 版中更改:添加了exception属性。 在3.3 版中更改:添加了msg用作上下文管理器时的关键字参数。
import unittest from prime_v1 import is_prime class TestIsPrime(unittest.TestCase): def test_prime_number(self): self.assertTrue(is_prime(17)) def test_non_prime_number(self): self.assertFalse(is_prime(10)) if __name__ == "__main__": unittest.main(verbosity=2) In this example,...
assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main() 上述示例中,通过继承 unittest.TestCase[4] 来创建一个测试用例。在这个类中,定义以 test 开头的方法,测试框架将把它作为独立的测试去执行。 每个用例都采用 unittest 内置的断言方法来判断被测对象的行为是否符合预期,...
Example #7Source File: test_program.py From jawfish with MIT License 6 votes def testCatchBreakInstallsHandler(self): module = sys.modules['unittest.main'] original = module.installHandler def restore(): module.installHandler = original self.addCleanup(restore) self.installed = False def fake...
Example #16Source File: test_program.py From ConTroll_Remote_Access_Trojan with Apache License 2.0 5 votes def test_ExitAsDefault(self): self.assertRaises( SystemExit, unittest2.main, argv=["foobar"], testRunner=unittest2.TextTestRunner(stream=StringIO()), testLoader=self.FooBarLoader()) ...