python调用其他类中的方法规范

在给自动测试框架加邮件发送功能的时候,一直提示发送邮件方法的参数不正确。参考了文章https://blog.csdn.net/qq_21703215/article/details/81702928终于解决了问题。

在runCase.py(用例驱动文件)文件中,调用了send_email.py中的类send_emaild

if __name__ == "__main__":
    for device in devices:
        test = Air_Case_Handler(device)
        test.run_air(air_path,device)

    # 发送测试报告到邮箱
    email = send_email()
    new_report = email.new_report(report_path)
    email.send_mail(new_report)

提示new_report方法所传参数不正确,然后我看了看send_email.py中的new_report

# -*- coding: utf-8 -*-
import os
import smtplib
import unittest
from HTMLTestRunner import HTMLTestRunner
import time
from email.header import Header
from email.mime.text import MIMEText
from conf.settings import report_path, send_email_path, receive_email_path, email_authorization_code, message_title

class send_email():
    #定义发送邮件
    def send_mail(file_new):
        f = open(file_new,"rb")
        mail_body = f.read()
        f.close()
        #编写HTML类型邮件正文
        msg = MIMEText(mail_body,"html","utf-8")
        msg["Subject"] = Header(message_title,"utf-8")

        #发送邮箱服务器
        smtp = smtplib.SMTP()
        smtp.connect("smtp.qq.com")
        #发送邮箱用户/qq邮箱smtp的授权码
        smtp.login(send_email_path,email_authorization_code)
        #发送邮箱/接收邮箱/邮件主题
        smtp.sendmail(send_email_path,receive_email_path,msg.as_string())
        smtp.quit()
        print("邮件已发送")

    #查找测试报告目录,找到最新生成的测试报告文件
    def new_report(test_report):
        lists = os.listdir(test_report)
        #重新按时间对目录下的文件进行排序
        lists.sort(key=lambda fn: os.path.getmtime(test_report +"\\"+fn))
        file_new = os.path.join(test_report,lists[-1])
        print(file_new)
        return file_new

new_report和send_mail都要求传一个参数,调用时我也传了一个参数,为什么还说我传参不正确?

原因是要在new_report和send_mail的参数中加上self,否则无法调用自身。

new_report(self,test_report)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值