1、对小极客首页跳转接口测试
2、写接口用例
以TestXjkLinks.py命名
1 # --*-- coding:utf-8 --*--
2
3 import requests
4 import unittest
5 import time
6
7
8 class TestXjkLinks(unittest.TestCase):
9
10 def test_learn(self):
11 u"""首页学习接口"""
12 url = 'http://www.xiaojike.cn/learn/'
13 result = requests.get(url)
14 time.sleep(1)
15 # 响应状态码断言
16 real_code = result.status_code
17 self.assertEqual(200, real_code)
18 # 未登陆时文案断言
19 real_text = result.text
20 expect_text = '使用用户名密码登录'
21 self.assertIn(expect_text, real_text)
22
23 def test_explore(self):
24 u"""首页发现接口"""
25 url = 'http://www.xiaojike.cn/explore/'
26 result = requests.get(url)
27 time.sleep(1)
28 # 响应状态码断言
29 real_code = result.status_code
30 self.assertEqual(200, real_code)
31 # 文案断言
32 real_text = result.text
33 expect_text = '有趣的作品'
34 self.assertIn(expect_text, real_text)
35
36 def test_login(self):
37 u"""首页登陆接口"""
38 url = 'http://www.xiaojike.cn/sign_in/'
39 result = requests.get(url)
40 time.sleep(1)
41 # 响应状态码断言
42 real_code = result.status_code
43 self.assertEqual(200, real_code)
44 # 文案断言
45 real_text = result.text
46 expect_text = '注册不到1秒钟'
47 self.assertIn(expect_text, real_text)
48
49 def test_err(self):
50 u"""判断错误的例子"""
51 self.assertNotEqual(3, 3)
52
53
54 if __name__ == '__main__':
55 unittest.main()
56 print('小极客首页接口测试成功')
3、写执行测试用例脚本 有兴趣的可以保存执行用例的脚本,基本上时通用的
1 # ! /usr/bin/env python3
2 # coding:utf-8
3
4 """
5 Created on 2018-05-30
6 Project: learning
7 @Author:wuya
8 """
9
10 import os, time, unittest
11 import HTMLTestRunner
12
13
14 report_path = os.getcwd() # 设置保存报告的路径,这儿设置的是与执行文件在同一个目录下
15 now = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time())) # 获取当前时间
16 title = u"小极客首页接口测试" # 标题
17 report_abspath = os.path.join(report_path, title + now + ".html") # 设置报告存放和命名
18
19 # 导入用例
20 def all_case():
21 case_path = os.getcwd() # 用例路径,这儿的用例和执行文件在同一目录下
22 discover = unittest.defaultTestLoader.discover(case_path,
23 pattern="Test*.py") # 添加用例,在case_path的路径下,所有以Test开头的文件都当做用例文件执行
24 print(discover)
25 return discover
26
27
28 if __name__ == "__main__":
29 fp = open(report_abspath, "wb") # 保存报告文件
30 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
31 title=title + ':',)
32 runner.run(all_case()) # 执行用例
33 fp.close()
4、执行查看结果