#coding:utf-8import unittest def div(a,b): return a/b #1 / 2 = 1 class MyfristTestCase(unittest.TestCase): def setUp(self): print("run!") def tearDown(self): print("over!") def test_sample(self): print("test_sample") if __name__ == ‘__main__‘: unittest.main()
setUp的作用:在每个测试用例执行之前预置场景。比如,打开浏览器,截图,等等
tearDown的作用:在每个测试用例之后执行。比如:关闭浏览器,截图,记录等等
#coding:utf-8 import unittest def div(a,b): return a/b #1 / 2 = 1 class MyfristTestCase(unittest.TestCase): def setUp(self): print("run!") def tearDown(self): print("over!") def test_sample(self): print("test_sample") def test_1_div_1(self): self.assertEqual(div(1,1),1/1) def test_3_div_4(self): self.assertEqual(div(3,4),3/4) def test_3_div_0(self): print("3/0") #assertRaises断言的方法,抛出异常的处理 self.assertRaises(ZeroDivisionError,div,3,0) if __name__ == ‘__main__‘: unittest.main()
时间: 2024-10-26 08:59:33