python呼叫其他類中的方法規範

2021-10-25 03:48:29 字數 1882 閱讀 7732

在給自動測試框架加郵件傳送功能的時候,一直提示傳送郵件方法的引數不正確。參考了文章終於解決了問題。

在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)

python 在類中主動呼叫其他類的成員

將兩個類的函式合在一起時這麼寫 方法一 class base object deff1 self print 5個功能 class foo object deff1 self print 3個功能 用類名呼叫例項方法不能自動傳self,需要手動傳參 base.f1 self obj foo obj....

Python 類方法中呼叫父類的類方法

3.super 測試 推薦使用,安全 如果類的物件方法呼叫父類的物件方法,可以使用super 函式獲取父類的物件方法 如果在類方法中呼叫父類的類方法,則可以使用super 推薦,暫時未發現問題 或者cls.base 屬性 不推薦,我用的時候出現未知錯誤 獲取父類的類方法。為什麼使用屬性會出現問題?因...

python類呼叫方法 在Python類中呼叫方法

您尚未建立上述類的物件。類中的任何函式 方法都只能由該類的物件訪問。有關物件導向程式設計基礎的詳細資訊,請檢視this頁。同時,要使其生效,請按以下方式定義類 class time def init self,x none,y none,z none self.hour x self.minute ...