所有的用例都必须继承unittest.TestCase
setUp:测试之前准备的工作放在前面,比如打开浏览器,
tearDown:测试用例执行完要做的,如关闭浏览器
断言:
#first_case.py
#coding: utf-8
import unittest
def div(a, b):
return a / b
# 1 / 2 = 1
class MyfirstTestCase(unittest.TestCase):
def setUp(self):
print ‘run before every test‘
def tearDown(self):
print ‘run after every test‘
def test_1_div_1(self):
print ‘1 div 1‘
self.assertEqual(div(1, 1), 1 / 1)
def test_3_div_4(self):
print ‘3 div 4‘
self.assertEqual(div(3, 4), 3 / 4)
def test_3_div_0(self):
print ‘3 / 0‘
self.assertRaises(ZeroDivisionError, div, 3, 0)
if __name__ == ‘__main__‘:
unittest.main()
原文地址:https://www.cnblogs.com/bzdmz/p/10333134.html
时间: 2024-10-09 09:51:27