Python 用pytest发送电子邮件
我有一个发送电子邮件的方法,我想使pytest测试参数化,但当我运行测试时,它不会运行,并且在日志中显示Python 用pytest发送电子邮件,python,pytest,Python,Pytest,我有一个发送电子邮件的方法,我想使pytest测试参数化,但当我运行测试时,它不会运行,并且在日志中显示没有运行测试。我的考试怎么了 pytest场景: import pytest import new_version @pytest.mark.parametrize("email", ['my_email@gmail.com']) def send_email(email): new_version.send_mail(email) 发送电子邮件的方法: # Imports i
没有运行测试
。我的考试怎么了
pytest场景:
import pytest
import new_version
@pytest.mark.parametrize("email", ['my_email@gmail.com'])
def send_email(email):
new_version.send_mail(email)
发送电子邮件的方法:
# Imports
import smtplib
import time
# Language
error_title = "ERROR"
send_error = "I'm waiting {idle} and I will try again."
send_error_mail = "Mail invalid"
send_error_char = "Invalid character"
send_error_connection_1_2 = "Connection problem..."
send_error_connection_2_2 = "Gmail server is down or internet connection is instabil."
login_browser = "Please log in via your web browser and then try again."
login_browser_info = "That browser and this software shood have same IP connection first time."
# Gmaild ID fro login
fromMail = "myemail@gmail.com"
fromPass = "pass"
# Some configurations
mailDelay = 15
exceptionDelay = 180
# SEND MAILS
def send_mail(email, thisSubject="Just a subject",
thisMessage="This is just a simple message..."):
# To ho to send mails
mailTo = [
email
]
# If still have mails to send
while len(mailTo) != 0:
sendItTo = mailTo[0] # Memorise what mail will be send it (debug purpose)
try:
# Connect to the server
server = smtplib.SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
# Sign In
server.login(fromMail, fromPass)
# Set the message
message = f"Subject: {thisSubject}\n{thisMessage}"
# Send one mail
server.sendmail(fromMail, mailTo.pop(0), message)
# Sign Out
server.quit()
# If is a problem
except Exception as e:
# Convert error in a string for som checks
e = str(e)
# Show me if...
if "The recipient address" in e and "is not a valid" in e:
print(f"\n>>> {send_error_mail} [//> {sendItTo}\n")
elif "'ascii'" in e and "code can't encode characters" in e:
print(f"\n>>> {send_error_char} [//> {sendItTo}\n")
elif "Please" in e and "log in via your web browser" in e:
print(f"\n>>> {login_browser}\n>>> - {login_browser_info}")
break
elif "[WinError 10060]" in e:
if "{idle}" in send_error:
se = send_error.split("{idle}");
seMsg = f"{se[0]}{exceptionDelay} sec.{se[1]}"
else:
seMsg = send_error
print(f"\n>>> {send_error_connection_1_2}\n>>> {send_error_connection_2_2}")
print(f">>> {seMsg}\n")
# Wait 5 minutes
waitTime = exceptionDelay - mailDelay
if waitTime <= 0:
waitTime = exceptionDelay
time.sleep(waitTime)
else:
if "{idle}" in send_error:
se = send_error.split("{idle}");
seMsg = f"{se[0]}{exceptionDelay} sec.{se[1]}"
else:
seMsg = send_error
print(f">>> {error_title} <<<", e)
print(f">>> {seMsg}\n")
# Wait 5 minutes
time.sleep(exceptionDelay)
# If are still mails wait before to send another one
if len(mailTo) != 0:
time.sleep(mailDelay)
#导入
导入smtplib
导入时间
#语言
错误\u title=“错误”
send_error=“我正在等待{idle},我将重试。”
发送错误邮件=“邮件无效”
发送错误字符=“无效字符”
发送\u错误\u连接\u 1\u 2=“连接问题…”
send_error_connection_2_2=“Gmail服务器关闭或internet连接为instabil。”
login\u browser=“请通过web浏览器登录,然后重试。”
login\u browser\u info=“该浏览器和该软件第一次应具有相同的IP连接。”
#用于登录的Gmail ID
fromMail=”myemail@gmail.com"
fromPass=“pass”
#一些配置
mailDelay=15
例外显示=180
#寄信
def send_mail(电子邮件,thisSubject=“只是一个主题”,
thisMessage=“这只是一条简单的消息…”):
#向总行发送邮件
mailTo=[
电子邮件
]
#如果还有邮件要发送
而len(mailTo)!=0:
sendItTo=mailTo[0]#记住要发送的邮件(调试目的)
尝试:
#连接到服务器
server=smtplib.SMTP(“SMTP.gmail.com:587”)
server.ehlo()
server.starttls()
#登录
server.login(fromMail,fromPass)
#设置消息
message=f“主题:{thisSubject}\n{thisMessage}”
#发一封信
server.sendmail(fromMail,mailTo.pop(0),message)
#注销
server.quit()
#如果有问题
例外情况除外,如e:
#som检查字符串中的转换错误
e=str(e)
#告诉我是否。。。
如果e中的“收件人地址”和e中的“无效”:
打印(f“\n>>>{send\u error\u mail}[/>{sendItTo}\n”)
e中的elif“'ascii'”和e中的“代码无法编码字符”:
打印(f“\n>>>{send\u error\u char}[/>{sendItTo}\n”)
如果e中的“请”和e中的“通过web浏览器登录”:
打印(f“\n>>>{login\u browser}\n>>>>-{login\u browser\u info}”)
打破
e中的elif“[WinError 10060]”:
如果发送错误中的“{idle}”:
se=发送错误。拆分({idle});
seMsg=f“{se[0]}{exceptionDelay}秒{se[1]}”
其他:
seMsg=发送错误
打印(f“\n>>>{send\u error\u connection\u 1\u 2}\n>>>{send\u error\u connection\u 2}”)
打印(f“>>>{seMsg}\n”)
#等5分钟
waitTime=例外播放-邮件延迟
如果waitTime,除非您选择Pytest,否则测试函数必须命名为test.*
:
导入pytest
导入新版本
@pytest.mark.parametize(“email”['my_email@gmail.com'])
def测试发送电子邮件(电子邮件):
新版本。发送邮件(电子邮件)