import unittest
from test import add,sub
def setUpModule():
print("加载文件")
def tearDownModule():
print("关闭文件")
class mytest(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# print("环境搭建")
# @classmethod
# def tearDownClass(cls):
# print("环境销毁")
def test_add(self):
'''test add'''
print("test_add")
self.assertEqual(add(10,20),30)
def test_sub(self):
'''test sub'''
print("test_sub")
self.assertEqual(sub(30,20),110)
class myn(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# print("环境搭建")
# @classmethod
# def tearDownClass(cls):
# print("环境销毁")
def testdd(self):
print("hdufhuwe")
if __name__=="__main__":
unittest.main()
注:三种环境搭建,销毁
'''会在运行每一个TestCase前都创建一个环境,结束后进行销毁'''
def setUp(self):
print("环境搭建")
def tearDown(self):
print("环境销毁")
'''在运行每一个测试类时创建环境,在我销毁环境'''
def setUpClass(cls):
print("环境搭建")
def tearDownClass(cls):
print("环境销毁")
'''在运行一个模块前加载环境,整个模块运行完后销毁环境''''
def setUpModule():
print("环境搭建")
def tearDownModule():
print("环境销毁")