mathFunc.py
class mathFunc():
def __init__(self,a,b):
self.a = a
self.b = b
def add(self):
return self.a + self.b
def sub(self):
return self.a - self.b
if __name__ == "__main__":
mathFuncObject = mathFunc(10,1)
print(mathFuncObject.add())
print(mathFuncObject.sub())
testMathFunc.py
import unittest
from execise.mathFunc import mathFunc
class testMathFunc(unittest.TestCase): #测试用例类需要继承unittest.TestCase
def setUp(self):
print("开始测试了")
def tearDown(self):
print("结束测试了")
def test_positive_add_positive(self):
#1.准备测试数据
a = 1
b = 1
#2.测试步骤
mathFuncObject = mathFunc(a,b)
result = mathFuncObject.add()
#3.预期结果和实际结果对比(断言)
self.assertEqual(2,result) #unittest.TestCase是testMathFunc的子类,继承后调用可以用self调用
def test_positive_sub_positive(self):
# 1.准备测试数据
a = 1
b = 1
# 2.测试步骤
mathFuncObject = mathFunc(a, b)
result = mathFuncObject.sub()
# 3.预期结果和实际结果对比(断言)
self.assertEqual(0, result)
if __name__ == "__main__":
# 执行继承unittest.TestCase测试类中的以test_开头的测试用例0
unittest.main()
main.py
import unittest
import os
import time
import HTMLTestRunner
from execise import testMathFunc
if __name__ == "__main__":
#1、执行测试用例
#想要按照自己的意愿执行测试用例
#手动识别测试用例
#方法一:添加一个测试用例 suite.addTest(testMathFunc("test_positive_add_positive"))
# suite = unittest.TestSuite()
# suite.addTest(testMathFunc.testMathFunc("test_positive_add_positive")) #测试类实例("测试方法名称")
# runner = unittest.TextTestRunner()
# runner.run(suite)
#方法二:添加多个测试用例 suite.addTests([testMathFunc("test_positive_add_positive"),testMathFunc("test_positive_sub_positive")])
# suite = unittest.TestSuite()
# suite.addTests([testMathFunc.testMathFunc("test_positive_add_positive"),testMathFunc.testMathFunc("test_positive_sub_positive")])
# runner = unittest.TextTestRunner()
# runner.run(suite)
# 方法三:unittest.TestLoader() tests = loader.loadTestsFromModule(testMathFunc)
# loader = unittest.TestLoader()
# tests = loader.loadTestsFromModule(testMathFunc) # loader.loadTestsFromModule(模块名称)
# runner = unittest.TextTestRunner()
# runner.run(tests)
# 方法四:unittest.TestLoader()
# loader = unittest.TestLoader()
# tests = loader.loadTestsFromTestCase(testMathFunc.testMathFunc) #loader.loadTestsFromTestCase(测试类名称)
# runner = unittest.TextTestRunner()
# runner.run(tests)
#自动识别测试用例
# 1、设置待执行用例的目录
# test_dir = os.path.join(os.getcwd())
# print(test_dir)
# # 2、自动搜索指定目录下的case,构造测试集,执行顺序是命名顺序:先执行test_add,再执行test_sub
# discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
# # 实例化TextTestRunner类
# runner = unittest.TextTestRunner()
# # 使用run()方法运行测试套件(即运行测试套件中的所有用例)
# runner.run(discover)
#2、生成测试报告
# TextTestRunner
test_dir = os.path.join(os.getcwd())
print(test_dir)
# 2、自动搜索指定目录下的case,构造测试集,执行顺序是命名顺序:先执行test_add,再执行test_sub
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
txtFile = open(time.strftime("%y_%m_%d_%H_%M_%S") +".txt","w+") #文件名加上时间戳
# 实例化TextTestRunner类
runner = unittest.TextTestRunner(stream=txtFile, descriptions="这是一个测试报告", verbosity=2)
# 使用run()方法运行测试套件(即运行测试套件中的所有用例)
runner.run(discover)
# HTMLTestRunner
test_dir = os.path.join(os.getcwd())
print(test_dir)
# 2、自动搜索指定目录下的case,构造测试集,执行顺序是命名顺序:先执行test_add,再执行test_sub
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
txtFile = open(time.strftime("%y_%m_%d_%H_%M_%S") +".html","wb+") #文件名加上时间戳
# 实例化TextTestRunner类
runner = HTMLTestRunner.HTMLTestRunner(stream=txtFile,verbosity=2,title="这是一个测试报告", description="测试报告来了")
# 使用run()方法运行测试套件(即运行测试套件中的所有用例)
runner.run(discover)