python unittest单元测试小实例

import unittest
import time
from lib.HTMLTestRunner import HTMLTestRunner
import json
import requests

if __name__ == '__main__':
    discover = unittest.defaultTestLoader.discover('./case', pattern='test*.py')
    # 存放报告的文件夹
    report_dir = './report'
    # 报告命名时间格式化
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    # 报告文件完整路径
    report_name = report_dir + '/' + now + 'result.html'

    with open(report_name, 'wb') as f:
        runner = HTMLTestRunner(stream=f, title="接口测试报告", description="", verbosity=2)
        runner.run(discover)
    f.close()

import unittest
import json
import requests


class Logout(unittest.TestCase):
    def setUp(self):
        print('start')

    def tearDown(self):
        print('end')

class Run(Logout):
    # 科室判断节口
    def test_keshi_api(self):
        param = {"s": "白癜风怎么治疗", "a": 2}
        url = 'http://baike.net/api/auto'
        r = requests.get(url, params=param)
        r = r.json()
        self.assertEqual(r["status"], 200)

    # 关键词相关接口
    def test_relate(self):
        param = {"word": "白癜风"}
        url = 'https://baikd.com/api/GetListByWord'
        r = requests.get(url, params=param)
        r = r.json()
        self.assertIsNotNone(r)


if __name__ == '__main__':
    unittest.main()

发表评论