1.方法一:针对每条测试用例进行初始化与还原
import unittest
from UnittestDemo.mathfunc import *
class TestMathFunc(unittest.TestCase):
"""测试 mathfunc.py"""
# 在每条测试用例执行之前准备好测试环境
def setUp(self):
print("do something before test!")
# 在每条测试用例执行结束之后准备好测试环境
def tearDown(self):
print("do something after test!")
def test_add(self):
"""测试加法 add()"""
self.assertEqual(3,add(1,2))
self.assertNotEqual(3,add(2,2))
def test_minus(self):
"""测试减法 minus"""
self.assertEqual(1,minus(3,2))
def test_multi(self):
"""测试乘法 multi"""
self.assertEqual(6,multi(2,3))
def test_divide(self):
"""测试除法 divide"""
self.assertEqual(2,divide(6,3))
self.assertEqual(2,divide(5,2))
2.方法二:1.方法一:针对每条测试用例进行初始化与还原
import unittest
from UnittestDemo.mathfunc import *
class TestMathFunc(unittest.TestCase):
"""测试 mathfunc.py"""
@classmethod
def setUpClass(cls):
print("do something before testClass,only run once!")
@classmethod
def tearDownClass(cls):
print("do something after testClass,only run once!")
def test_add(self):
"""测试加法 add()"""
self.assertEqual(3,add(1,2))
self.assertNotEqual(3,add(2,2))
def test_minus(self):
"""测试减法 minus"""
self.assertEqual(1,minus(3,2))
def test_multi(self):
"""测试乘法 multi"""
self.assertEqual(6,multi(2,3))
def test_divide(self):
"""测试除法 divide"""
self.assertEqual(2,divide(6,3))
self.assertEqual(2,divide(5,2))