单元测试dome.py
class Calc:
def add(self,a,b):
return a + b
def minus(self,a,b):
return a - b
def multi(self,a,b):
return a * b
def devision(self,a,b):
return a / b
TestCalc.py
from 单元测试dmeo import Calc
from unittest import TestCase
class TestCalcAdd(TestCase): # 在括号内,写你想继承的类即可
def testAdd1(self):
# 1.准备测试数据
a = 6
b = -6
c = 0
# 2.执行测试
calc = Calc()
s = calc.add(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testAdd2(self):
# 1.准备测试数据
a = -6
b = -6
c = -12
# 2.执行测试
calc = Calc()
s = calc.add(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testAdd3(self):
# 1.准备测试数据
a = 6
b = -6
c = 0
# 2.执行测试
calc = Calc()
s = calc.add(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testAdd4(self):
# 1.准备测试数据
a = 10000000000000000000000000000000000000
b = 5
c = 10000000000000000000000000000000000005
# 2.执行测试
calc = Calc()
s = calc.add(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testMinus1(self):
# 1.准备测试数据
a = 6
b = 3
c = 3
# 2.执行测试
calc = Calc()
s = calc.minus(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testMinus2(self):
# 1.准备测试数据
a = 6
b = 8
c = -2
# 2.执行测试
calc = Calc()
s = calc.minus(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testMinus3(self):
# 1.准备测试数据
a = -6
b = -6
c = 0
# 2.执行测试
calc = Calc()
s = calc.minus(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testMinus4(self):
# 1.准备测试数据
a = 100000000000000000000000000000000
b = 5
c = 99999999999999999999999999999995
# 2.执行测试
calc = Calc()
s = calc.minus(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s,c)
def testMulti1(self):
# 1.准备测试数据
a = 6
b = 6
c = 36
# 2.执行测试
calc = Calc()
s = calc.multi(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testMulti2(self):
# 1.准备测试数据
a = -6
b = -6
c = 36
# 2.执行测试
calc = Calc()
s = calc.multi(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testMulti3(self):
# 1.准备测试数据
a = -6
b = 6
c = -36
# 2.执行测试
calc = Calc()
s = calc.multi(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testMulti4(self):
# 1.准备测试数据
a = 1000000000000000000000
b = 6
c = 6000000000000000000000
# 2.执行测试
calc = Calc()
s = calc.multi(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testMulti5(self):
# 1.准备测试数据
a = 6
b = 0
c = 0
# 2.执行测试
calc = Calc()
s = calc.multi(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testDevision1(self):
# 1.准备测试数据
a = 6
b = 6
c = 1
# 2.执行测试
calc = Calc()
s = calc.devision(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testDevision2(self):
# 1.准备测试数据
a = 6
b = -6
c = -1
# 2.执行测试
calc = Calc()
s = calc.devision(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testDevision3(self):
# 1.准备测试数据
a = 0
b = 6
c = 0
# 2.执行测试
calc = Calc()
s = calc.devision(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testDevision4(self):
# 1.准备测试数据
a = -600000000000000000
b = -6
c = 100000000000000000
# 2.执行测试
calc = Calc()
s = calc.devision(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
def testDevision5(self):
# 1.准备测试数据
a = 60000000000000000000
b = 6
c = 10000000000000000000
# 2.执行测试
calc = Calc()
s = calc.devision(a, b)
# 3.对比期望结果和实际结果,一样,通过,不一样,是bug,提交之
self.assertEqual(s, c)
main.py
import unittest
import HTMLTestRunner
# 1.扫描4条用例
tests = unittest.defaultTestLoader.discover("E:\hkr\hongruan\单元测试",pattern="Test*.py")
# 2.创建执行器
runner = HTMLTestRunner.HTMLTestRunner(
title = "这是计算器报告",
description="这是加减乘除测试报告",
verbosity=2,
stream = open(file="计算器.html",mode="w+",encoding="utf-8")
)
# 3.生成测试报告
runner.run(tests)
# 任务一:4.使用pyhthon把测试报告发送到邮箱里 13552648187@163.com
# 任务二: 减法,除法,乘法三种用例开发出来
import smtplib
from email.mime.text import MIMEText # 创建文本内容的邮件内容
from email.mime.multipart import MIMEMultipart # 创建带附件的实例
from email.mime.application import MIMEApplication # 用于创建附件的
from email.mime.image import MIMEImage # 用于创建图片,与添加附件一样的,下面不做举例
# 第一步: 连接到smtp服务器
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtp.login("3251145150@qq.com", "授权码") #通过QQ邮箱生成授权码
# 第二步:构建邮件
smg = MIMEMultipart()
text_smg = MIMEText("加减乘除测试报告", "plain", "utf8") # 邮件内容
smg.attach(text_smg) # 添加到邮件
# 添加附件
file_msg = MIMEApplication(open(r"E:\hkr\hongruan\单元测试\计算器.html", "rb").read())
file_msg.add_header('content-disposition', 'attachment', filename='计算器.html')
smg.attach(file_msg) # 添加到邮件
smg["Subject"] = "加减乘除测试报告" # 主题
smg["From"] = "3251145150@qq.com" # 邮件内显示的发件人
smg["To"] = "13552648187@163.com" # 邮件内显示的收件人
# 第三步发送邮件
smtp.send_message(smg, from_addr="3251145150@qq.com", to_addrs="13552648187@163.com")
python单元测试,生成测试报告并发送邮件
最新推荐文章于 2024-09-05 14:43:33 发布