• python接口自动化测试


    写在前面的话:

      这个是我实际工作中写的项目,主要用来备注和后期查看~~大家可以参考学习,但是请不要用于其他不好的途径~~

    准备工作:

             先下载HTMLTestRunner.py

             下载地址:HTMLTestRunner - tungwaiyip's software

             参考:http://www.cnblogs.com/testyao/p/5658200.html

             里面提供写好的适合python3使用的:HTMLTestRunner.py_免费高速下载|百度网盘-分享无限制

             把这个文件放在你安装python的lib目录下(我的在C:\Program Files\Python36\Lib)

    一:少量用例,仅生成测试报告        

    #############run1.py#############

    1. 1 import unittest
    2. 2 import HTMLTestRunner
    3. 3 import os
    4. 4
    5. 5 #从文件test_ljj.py加载AddCompany类
    6. 6 from test_case.company.test_getLog import GetLog
    7. 7
    8. 8 #使用测试套件,添加Case01类里面的test_ case01方法
    9. 9 suite = unittest.TestSuite()
    10. 10 suite.addTest(GetLog('test_getLogN'))
    11. 11
    12. 12 #{这一段是输出到特定目录下
    13. 13 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__)))
    14. 14 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件所在目录
    15. 15 report_path = os.path.join(cur_path, 'report') #拼接成一个新目录
    16. 16 report_abspath = os.path.join(report_path, "result.html")
    17. 17
    18. 18 #以字节的方式写入目录下的report.html文件里
    19. 19 st = open(report_abspath, 'wb')
    20. 20 HTMLTestRunner.HTMLTestRunner(stream = st, title= '公司端接口自动化测试报告').run(suite)
    21. 21 #}
    22. 22
    23. 23 # 这种方法是直接在控制台输出
    24. 24 # runner = unittest.TextTestRunner()
    25. 25 # runner.run(suite)

    二、执行多条用例,仅生成测试报告

     #############run2.py#############

    1. 1 #coding=utf-8
    2. 2 import unittest
    3. 3 import os
    4. 4 import HTMLTestRunner
    5. 5
    6. 6 #仅生成测试报告不发送邮件
    7. 7 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__)))
    8. 8 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径
    9. 9 case_path = os.path.join(cur_path, 'test_case') #测试case目录
    10. 10 report_path = os.path.join(cur_path, 'report') #测试报告存放目录
    11. 11
    12. 12
    13. 13 def all_case():
    14. 14 '''第一步:加载所有的测试用例'''
    15. 15
    16. 16 # 批量加载iscover方法里面有三个参数:-case_dir:这个是待执行用例的目录-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。
    17. 17 # discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里,这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。
    18. 18
    19. 19 discover = unittest.defaultTestLoader.discover(case_path,
    20. 20 pattern="test_ljj*.py",
    21. 21 top_level_dir=None)
    22. 22 # print(discover)
    23. 23 return discover
    24. 24
    25. 25 def run_case(all_case):
    26. 26 '''第二步:执行所有的用例, 并把结果写入HTML测试报告'''
    27. 27
    28. 28 # 测试报告文件路径
    29. 29 report_abspath = os.path.join(report_path, "result.html")
    30. 30 fp = open(report_abspath, "wb")
    31. 31 # 批量执行测试用例三个参数: --stream:测试报告写入文件的存储区域 --title:测试报告的主题 --description:测试报告的描述
    32. 32 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
    33. 33 title=u'日本公司端自动化测试报告,测试结果如下:',
    34. 34 description=u'用例执行情况:')
    35. 35
    36. 36 # 调用add_case函数返回值
    37. 37 runner.run(all_case)
    38. 38 fp.close()
    39. 39
    40. 40 if __name__ == "__main__":
    41. 41 all_case = all_case() # 1加载用例
    42. 42 run_case(all_case) # 2执行用例

    #############run3.py#############

    1. 1 # -* - coding: UTF-8 -* -
    2. 2
    3. 3 [email]
    4. 4 smtp_server = smtp.163.com
    5. 5 port = 587
    6. 6 sender = ljjiaoyou@163.com
    7. 7 psw = Aa!23456
    8. 8 receiver = 1628891265@qq.com
    9. 9
    10. 10 [website]
    11. 11 host = http://10.95.178.216:8001
    12. 12 url = /gulfstream/mis/i18n-apac-mis/company
    13. 13
    14. 14 [payload]
    15. 15 page =1
    16. 16 size = 50
    17. 17 locale = zh-CN
    18. 18 utc_offset = 480
    19. 19 canonical_country_code = JP

    1. 1 # -* - coding: UTF-8 -* -
    2. 2 import os
    3. 3 from backports.configparser import ConfigParser
    4. 4
    5. 5 cur_path = os.path.dirname(os.path.realpath(__file__)) #获取当前文件所在路径
    6. 6 configPath = os.path.join(cur_path, "cfg.ini") #拼接出文件路径
    7. 7 conf = ConfigParser()
    8. 8 conf.read(configPath) #读取配置文件cfg.ini
    9. 9
    10. 10 # 获取指定的section, 指定的option的值
    11. 11 smtp_server = conf.get("email", "smtp_server")
    12. 12 sender = conf.get("email", "sender")
    13. 13 psw = conf.get("email", "psw")
    14. 14 receiver = conf.get("email", "receiver")
    15. 15 port = conf.get("email", "port")
    16. 16
    17. 17 host = conf.get("website", "host")
    18. 18 url = conf.get("website", "url")
    19. 19
    20. 20 # 读取该section下的所有值,并以键值对形式输出。格式为list
    21. 21 payload1 = conf.items("payload")
    22. 22 payload = dict(payload1)

    1. 1 #coding=utf-8
    2. 2 import unittest
    3. 3 import os
    4. 4 import HTMLTestRunner
    5. 5 import smtplib
    6. 6 from email.mime.text import MIMEText
    7. 7 from email.mime.multipart import MIMEMultipart
    8. 8
    9. 9 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__)))
    10. 10 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径
    11. 11 case_path = os.path.join(cur_path, 'test_case') #测试case目录
    12. 12 report_path = os.path.join(cur_path, 'report') #测试报告存放目录
    13. 13
    14. 14 def send_mail(sender, psw, receiver, smtpserver, port):
    15. 15 '''第三步:发送最新的测试报告内容'''
    16. 16 file_path = report_path + r"\result.html"
    17. 17 with open(file_path, "rb") as f:
    18. 18 mail_body = f.read()
    19. 19
    20. 20 # 定义邮件内容
    21. 21 msg = MIMEMultipart()
    22. 22 msg['Subject'] = u"就是想测试下看看能不能收到邮件" #主题
    23. 23 msg["from"] = sender #发件人
    24. 24 msg["to"] = receiver #收件人
    25. 25
    26. 26 #正文
    27. 27 body = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    28. 28 msg.attach(body)
    29. 29
    30. 30 # 添加附件
    31. 31 att = MIMEText(mail_body, "base64", "utf-8")
    32. 32 att["Content-Type"] = "application/octet-stream"
    33. 33 att["Content-Disposition"] = 'attachment; filename= "report.html"'
    34. 34 msg.attach(att)
    35. 35
    36. 36 try:
    37. 37 smtp = smtplib.SMTP_SSL(smtpserver, port) #QQ邮箱
    38. 38 except:
    39. 39 smtp = smtplib.SMTP() #163邮箱
    40. 40 smtp.connect(smtpserver, port) #链接服务器
    41. 41 # 用户名密码
    42. 42 smtp.login(sender, psw) #登录
    43. 43 smtp.sendmail(sender, receiver, msg.as_string())#发送
    44. 44 smtp.quit() #关闭
    45. 45 print('test report email has send out !')
    46. 46
    47. 47
    48. 48 if __name__ == "__main__":
    49. 49 # 读取邮箱配置
    50. 50 from config import readConfig
    51. 51 sender = readConfig.sender
    52. 52 psw = readConfig.psw
    53. 53 smtp_server = readConfig.smtp_server
    54. 54 port = readConfig.port
    55. 55 receiver = readConfig.receiver
    56. 56 send_mail(sender, psw, receiver, smtp_server, port) # 发送报告

    四、想把执行case和发邮件放在一起的话,把run2.py和run3.py合并即可

             参考:https://www.cnblogs.com/yoyoketang/p/7259993.html

             #############run4.py#############

    1. 1 #coding=utf-8
    2. 2 import unittest
    3. 3 import os
    4. 4 import HTMLTestRunner
    5. 5 import smtplib
    6. 6 from email.mime.text import MIMEText
    7. 7 from email.mime.multipart import MIMEMultipart
    8. 8
    9. 9 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__)))
    10. 10 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径
    11. 11 case_path = os.path.join(cur_path, 'test_case') #测试case目录
    12. 12 report_path = os.path.join(cur_path, 'report') #测试报告存放目录
    13. 13
    14. 14 def all_case():
    15. 15 '''第一步:加载所有的测试用例'''
    16. 16
    17. 17 # 批量加载iscover方法里面有三个参数:-case_dir:这个是待执行用例的目录-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。
    18. 18 # discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里,这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。
    19. 19
    20. 20 discover = unittest.defaultTestLoader.discover(case_path,
    21. 21 pattern="test_lj*.py",
    22. 22 top_level_dir=None)
    23. 23 # print(discover)
    24. 24 return discover
    25. 25
    26. 26 # def run_case():
    27. 27 def run_case(all_case):
    28. 28 '''第二步:执行所有的用例, 并把结果写入HTML测试报告'''
    29. 29 # 测试报告文件路径
    30. 30 report_abspath = os.path.join(report_path, "result.html")
    31. 31 fp = open(report_abspath, "wb")
    32. 32 # 批量执行测试用例三个参数: --stream:测试报告写入文件的存储区域 --title:测试报告的主题 --description:测试报告的描述
    33. 33 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
    34. 34 title=u'日本公司端自动化测试报告,测试结果如下:',
    35. 35 description=u'用例执行情况:')
    36. 36
    37. 37 # 调用add_case函数返回值
    38. 38 runner.run(all_case)
    39. 39 fp.close()
    40. 40
    41. 41 def send_mail(sender, psw, receiver, smtpserver, port):
    42. 42 '''第三步:发送最新的测试报告内容'''
    43. 43 file_path = report_path + r"\result.html"
    44. 44 with open(file_path, "rb") as f:
    45. 45 mail_body = f.read()
    46. 46
    47. 47 # 定义邮件内容
    48. 48 msg = MIMEMultipart()
    49. 49 msg['Subject'] = u"就是想测试下看看能不能收到邮件" #主题
    50. 50 msg["from"] = sender #发件人
    51. 51 msg["to"] = receiver #收件人
    52. 52
    53. 53 #正文
    54. 54 body = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    55. 55 msg.attach(body)
    56. 56
    57. 57 # 添加附件
    58. 58 att = MIMEText(mail_body, "base64", "utf-8")
    59. 59 att["Content-Type"] = "application/octet-stream"
    60. 60 att["Content-Disposition"] = 'attachment; filename= "report.html"'
    61. 61 msg.attach(att)
    62. 62
    63. 63 try:
    64. 64 smtp = smtplib.SMTP_SSL(smtpserver, port) #QQ邮箱
    65. 65 except:
    66. 66 smtp = smtplib.SMTP() #163邮箱
    67. 67 smtp.connect(smtpserver, port) #链接服务器
    68. 68 # 用户名密码
    69. 69 smtp.login(sender, psw) #登录
    70. 70 smtp.sendmail(sender, receiver, msg.as_string())#发送
    71. 71 smtp.quit() #关闭
    72. 72 print('test report email has send out !')
    73. 73
    74. 74 if __name__ == "__main__":
    75. 75 all_case = all_case() # 1加载用例
    76. 76 # 生成测试报告的路径
    77. 77 run_case(all_case) # 2执行用例
    78. 78 # 邮箱配置
    79. 79 from config import readConfig
    80. 80 sender = readConfig.sender
    81. 81 psw = readConfig.psw
    82. 82 smtp_server = readConfig.smtp_server
    83. 83 port = readConfig.port
    84. 84 receiver = readConfig.receiver
    85. 85 send_mail(sender, psw, receiver, smtp_server, port) # 发送报告

    六、项目文件分布图
     

    Python接口自动化测试零基础入门到精通(2023最新版)

  • 相关阅读:
    凡治众如治寡,分数是也——分治算法
    线程池的使用及ThreadPoolExecutor源码分析
    高精度数字压力表丨铭控传感多款数字压力表在多场景中的应用
    结构体超详解(小白一看就懂,多维度分析!!!!)
    在内网部署docker工程总结
    双位置继电器DLS-42/6-4/DC110V
    “论单元测试方法及应用”写作框架,软考高级论文,系统架构设计师论文
    Spring事务回滚核心源码解读
    vue2 项目中引入iconfont
    Linux 性能分析命令详解
  • 原文地址:https://blog.csdn.net/dad22211/article/details/134077604