接口测试报告的输出(三)

1、对小极客首页跳转接口测试

2、写接口用例

以TestXjkLinks.py命名

# --*-- coding:utf-8 --*--
 
 import requests
 import unittest
 import time
 
 
 class TestXjkLinks(unittest.TestCase):
 
     def test_learn(self):
         u"""首页学习接口"""
         url = 'http://www.xiaojike.cn/learn/'
         result = requests.get(url)
         time.sleep(1)
         # 响应状态码断言
         real_code = result.status_code
         self.assertEqual(200, real_code)
         # 未登陆时文案断言
         real_text = result.text
         expect_text = '使用用户名密码登录'
         self.assertIn(expect_text, real_text)
 
     def test_explore(self):
         u"""首页发现接口"""
         url = 'http://www.xiaojike.cn/explore/'
         result = requests.get(url)
         time.sleep(1)
         # 响应状态码断言
         real_code = result.status_code
         self.assertEqual(200, real_code)
         # 文案断言
         real_text = result.text
         expect_text = '有趣的作品'
         self.assertIn(expect_text, real_text)
 
     def test_login(self):
         u"""首页登陆接口"""
         url = 'http://www.xiaojike.cn/sign_in/'
         result = requests.get(url)
         time.sleep(1)
         # 响应状态码断言
         real_code = result.status_code
         self.assertEqual(200, real_code)
         # 文案断言
         real_text = result.text
         expect_text = '注册不到1秒钟'
         self.assertIn(expect_text, real_text)
 
     def test_err(self):
         u"""判断错误的例子"""
         self.assertNotEqual(3, 3)
 
 
 if __name__ == '__main__':
     unittest.main()
     print('小极客首页接口测试成功')

3、写执行测试用例脚本

有兴趣的可以保存执行用例的脚本,基本上时通用的

# ! /usr/bin/env python3
 # coding:utf-8
 
 """
 Created on 2018-05-30
 Project: learning
 @Author:wuya
 """
 
 import os, time, unittest
 import HTMLTestRunner
 
 
 report_path = os.getcwd()  # 设置保存报告的路径,这儿设置的是与执行文件在同一个目录下
 now = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())) # 获取当前时间
 title = u"小极客首页接口测试" # 标题
 report_abspath = os.path.join(report_path, title + now + ".html") # 设置报告存放和命名
 
 # 导入用例
 def all_case():
     case_path = os.getcwd() # 用例路径,这儿的用例和执行文件在同一目录下
     discover = unittest.defaultTestLoader.discover(case_path,
                                                    pattern="Test*.py") # 添加用例,在case_path的路径下,所有以Test开头的文件都当做用例文件执行
     print(discover)
     return discover
 
 
 if __name__ == "__main__":
     fp = open(report_abspath, "wb") # 保存报告文件
     runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                            title=title + ':',)
     runner.run(all_case()) # 执行用例
     fp.close()

4、执行查看结果

结果原文档下载

接口测试报告的输出(三)

相关推荐