最新出炉 --python3+requests+unittest:接口自动化测试_requests unittest 自动化测试 github(1)

1 requests.post(url=‘’,data={‘key1’:‘value1’,‘key2’:‘value2’},headers={‘Content-Type’:‘application/x-www-form-urlencoded’})


(2)请求正文是multipart/form-data



1 requests.post(url=‘’,data={‘key1’:‘value1’,‘key2’:‘value2’},headers={‘Content-Type’:‘multipart/form-data’})


(3)请求正文是raw



传入xml格式文本



1 requests.post(url=‘’,data=‘<?xml ?>’,headers={‘Content-Type’:‘text/xml’})



传入json格式文本



1 requests.post(url=‘’,data=json.dumps({‘key1’:‘value1’,‘key2’:‘value2’}),headers={‘Content-Type’:‘application/json’})


或者



1 requests.post(url=‘’,json={{‘key1’:‘value1’,‘key2’:‘value2’}},headers={‘Content-Type’:‘application/json’})


(4)请求正文是binary



1 requests.post(url=‘’,files={‘file’:open(‘test.xls’,‘rb’)},headers={‘Content-Type’:‘binary’})


【2】请求之后对返回结果进行断言:assertEqual()等,断言方法很多,可上网查询


![](https://img-blog.csdnimg.cn/img_convert/59e4361eaf3de17d52a88adb218576e2.png)


(2)实际中,有些参数或方法在执行用例之前必须说明,这就涉及到setUp、tearDown、setUpClass、tearDownClass的用法


【1】setup():每个测试函数运行前运行;teardown():每个测试函数运行完后执行;setUpClass():必须使用@classmethod 装饰器,所有test运行前运行一次;tearDownClass():必须使用@classmethod装饰器,所有test运行完后运行一次


【2】也可以对某些用例执行过程中跳过:unittest.skip(),具体使用方法可上网查询


![](https://img-blog.csdnimg.cn/img_convert/162182ce8359523e70f0c57c60d74553.png)


(3)如果每次接口请求时都使用requests/get、post请求实在是繁琐,我们可以对该方法进行封装,接口请求时直接调用即可


![](https://img-blog.csdnimg.cn/img_convert/20f91a6c764e0726572e2574711cad33.png)


(4)测试用例编写完之后,主函数run.py直接使用unittest模块将所有用例一并执行


【1】unittest模块:TestSuite 多个测试用例集合在一起;TestRunner 是来执行测试用例的,测试的结果会保存到TestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息(unittest.TextTestRunner().run(suite))


【2】suite.addTests()和suite.addTest()均可实现



2种用法:第一种suite.addTest()

suite.addTest(Case(‘test_case01’))
suite.addTest(Case(‘test_case02’))
suite.addTest(Test(‘test_01’))
suite.addTest(Test(‘test_02’))

#2种用法:第二种suite.addTests()
suite.addTests(map(Test, [“test_01”, “test_02”]))
suite.addTests(map(Case, [“test_case01”, “test_case02”]))


【3】TestRunner测试结果输出到控制台,要将测试结果输出到report中,使用HTMLTestRunner.py文件(该文件参考地址:[GitHub - huilansame/HTMLTestRunner\_PY3: 针对PY3做了修改,增加对subTest的支持,用Echarts加了执行统计表]( ))


![](https://img-blog.csdnimg.cn/img_convert/e4604dfe6f0d7a2a7b3ea77a66ae6e4b.png)


(5)测试完成之后,想要将测试结果发送邮件给相关人员


发送邮件具体实现方法,


## [python3:利用SMTP协议发送QQ邮件+附件]( )


转载请表明出处:[python3:利用SMTP协议发送QQ邮件+附件 - Shapelei - 博客园]( )


1.发送QQ邮件,首先必须知道QQ邮箱的SMTP服务器


http://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1


![](https://img-blog.csdnimg.cn/img_convert/a5e37c848cca7ddc0693f616c66533f2.png)


2.发送邮件之前,必须开启qq邮箱的smtp服务


设置路径:邮箱设置--账户--开启截图服务--保存更改


![](https://img-blog.csdnimg.cn/img_convert/1d3667a545f7920bbc7215407100bbd2.png)


3.代码抛出异常分析


(1)邮箱密码传入值为日常登录密码,报错


![](https://img-blog.csdnimg.cn/img_convert/f239ac03b515fccc16543adbf2bce928.gif)



global send_user
global email_host
global password

password = ‘xxx92’
email_host = “smtp.qq.com”
send_user = “11xxx@qq.com”


![](https://img-blog.csdnimg.cn/img_convert/369f08f6d2e1bd35536ab4da54444905.gif)


抛出异常:


smtplib.SMTPAuthenticationError:(535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')


打开抛出异常中的链接:是关于授权码的介绍,根据介绍,登录时应该使用授权码作为登录密码,该处的授权码是开启服务时收到的16位授权码


http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256


修改代码:


password = "lunkbrgwqxhfjgxx"(对应的16位授权码)


(2)安全邮件,需要通过SSL发送



server = smtplib.SMTP()
server.connect(email_host,25)


抛出异常:


smtplib.SMTPServerDisconnected: Connection unexpectedly closed


QQ邮箱是支持安全邮件的,需要通过SSL发送的邮件:使用标准的25端口连接SMTP服务器时,使用的是明文传输,发送邮件的整个过程可能会被窃听。要更安全地发送邮件,可以加密SMTP会话,实际上就是先创建SSL安全连接,然后再使用SMTP协议发送邮件


修改代码:



server = smtplib.SMTP_SSL()
server.connect(email_host,465)# 启用SSL发信, 端口一般是465


4.附上完整代码


![](https://img-blog.csdnimg.cn/img_convert/6b62b20fdc4a836bb088ce76e3d58add.gif)



#coding:utf-8
import smtplib
from email.mime.text import MIMEText

class SendEmail:
global send_user
global email_host
global password
password = “lunkbrgwqxhfjgxx”
email_host = “smtp.qq.com”
send_user = “11xx@qq.com”

def send_mail(self,user_list,sub,content):
    user = "shape" + "<" + send_user + ">"
    message = MIMEText(content,_subtype='plain',_charset='utf-8')
    message['Subject'] = sub
    message['From'] = user
    message['To'] = ";".join(user_list)
    server = smtplib.SMTP_SSL()
    server.connect(email_host,465)
    server.login(send_user,password)
    server.sendmail(user,user_list,message.as_string())
    server.close()

if name == ‘main’:
send = SendEmail()
user_list = [‘11xx@qq.com’]
sub = “测试邮件”
content = “ceshi看看”
send.send_mail(user_list,sub,content)


![](https://img-blog.csdnimg.cn/img_convert/5e760bf7293e8475055d9571f6a19094.gif)


(1)Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件


(2)构造MIMEText对象时,第一个参数是邮件正文;第二个参数是MIME的subtype,传入'plain'表示纯文本,最终的MIME就是'text/plain';最后一定要用utf-8编码保证多语言兼容性


(3)发送的邮件需要添加头部信息,头部信息中包含发送者、接收者、邮件主题等信息:message['From']、message['To']、message['Subject']


(4)构造完要发送的邮件信息后,通过SMTP发出去:login()方法用来登录SMTP服务器;sendmail()方法就是发邮件,由于可以一次发给多个人,所以传入一个list;邮件正文是一个str,as\_string()把MIMEText对象变成str


(5)SMTP.close() :关闭SMTP服务器连接



5、发送邮件带附件


参考代码:



![](https://img-blog.csdnimg.cn/img_convert/6b62b20fdc4a836bb088ce76e3d58add.gif)



#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

class SendEmail:
global send_user
global email_host
global password
password = “lunkbrgwqxhfjgxx”
email_host = “smtp.qq.com”
send_user = “xx@qq.com”

def send_mail(self,user_list,sub,content):
    user = "shape" + "<" + send_user + ">"

    # 创建一个带附件的实例
    message = MIMEMultipart()
    message['Subject'] = sub
    message['From'] = user
    message['To'] = ";".join(user_list)

    # 邮件正文内容
    message.attach(MIMEText(content, 'plain', 'utf-8'))

    # 构造附件(附件为txt格式的文本)
    att = MIMEText(open('../log/log.txt', 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment; filename="Log.txt"'
    message.attach(att)

    server = smtplib.SMTP_SSL()
    server.connect(email_host,465)# 启用SSL发信, 端口一般是465
    # server.set_debuglevel(1)# 打印出和SMTP服务器交互的所有信息
    server.login(send_user,password)
    server.sendmail(user,user_list,message.as_string())
    server.close()

def send_main(self,pass_list,fail_list,no_run_list):

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

user_list,message.as_string())
server.close()

def send_main(self,pass_list,fail_list,no_run_list):

[外链图片转存中…(img-rnhDwuMo-1719269516513)]
[外链图片转存中…(img-8bUa8Xo6-1719269516513)]
[外链图片转存中…(img-ojkaqg9K-1719269516514)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Pythonunittest库提供了一种基于单元测试的测试框架,是一个方便易用的Python测试框架。使用unittest库进行接口自动化测试可以提高测试效率和质量,本文将分享如何使用Python unittest库搭建接口自动化测试框架。 第一步:安装Python unittest库 首先需要安装Python unittest库,Python unittest库是默认安装在Python中的,无需单独安装。 第二步:安装requests模块 接口自动化测试需要使用requests模块来发送HTTP请求、获取响应等操作,因此需要安装requests模块。使用pip安装requests命令如下: pip install requests 第三步:编写测试用例 使用unittest框架编写测试用例,首先需要导入unittest库并创建测试类,编写测试方法,方法名必须以test开头,并使用assert断言方法进行验证。例如: import unittest import requests class TestApi(unittest.TestCase): def test_get_users(self): url = 'http://localhost:8080/api/users' res = requests.get(url) self.assertEqual(res.status_code, 200) self.assertIsNotNone(res.json()) 第四步:执行测试用例 使用unittest框架执行测试用例,使用unittest.main()方法运行所有测试用例。例如: if __name__ == '__main__': unittest.main() 执行测试用例后,将输出测试结果,包括测试用例总数、成功数、失败数等。 第五步:持续集成 持续集成可以帮助实现自动化测试,可以将上述步骤集成到自动化测试框架中,提高测试效率和质量。使用持续集成工具,例如Jenkins,可以实现自动化测试的调度和执行,定期输出测试报告,是测试自动化化的不二选择。 在以上步骤中,请求地址和验证方法需要根据具体需求进行更改,但是编写测试用例的方法是类似的,熟练掌握unittest库可以快速搭建接口自动化测试框架,提高测试效率和质量。 ### 回答2: Python unittest requests 接口自动化测试框架搭建教程博客是指一篇博客文章,介绍如何使用Python unittestrequests库搭建接口自动化测试框架。该教程博客有如下几个方面: 1. 简单介绍Python unittestrequests库,以及它们在接口自动化测试中的使用; 2. 详细讲解如何安装Python unittestrequests库,并编写测试用例; 3. 讲解如何通过使用Python unittest的setUp()和tearDown()方法,在测试用例执行前后进行一些操作,以便更好地进行测试; 4. 介绍如何运行测试用例,并查看测试结果,以及如何进行测试报告生成; 5. 提供一些实例,展示如何使用Python unittestrequests库搭建接口自动化测试框架。 通过这篇教程博客,读者可以学习如何使用Python unittestrequests库搭建接口自动化测试框架,并且能够快速了解并掌握这种接口自动化测试方法的流程和基本方法。此外,该教程博客也提供一些实例,帮助读者更好地理解和应用这种方法。因此,这篇教程博客对于想要学习接口自动化测试以及深入了解Python unittestrequests库的读者来说,是一篇非常有价值的文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值