How to use unittest test interface in python_python

不言
Release: 2018-04-04 16:31:13
Original
1929 people have browsed it

This article mainly introduces in detail how Python uses the unittest test interface. It has certain reference value. Interested friends can refer to it.

The example of this article shares with you how to use python to test using unittest. The specific code of the interface is for your reference. The specific content is as follows

1. First use python requests to test the interface

# TestInface.py 
import requests,json 
url = visit.get_test_url() 
news_url = url+'news.info' 
headers = baseToken.basetoken_datas()['headers'] 
def new_data(data): 
   
  r = requests.post(news_url,data=data,headers=headers) 
  cnn = json.loads(r.text) 
  return cnn
Copy after login

2. Use unittest to call the interface and collect statistics on the results of the interface test

# TestCase.py 
# -*- coding:utf-8 -*- 
import unittest 
import TestInface 
 
 
# 对执行的case结果进行统计 
# --------------------------------------------------------------------------------------------------------------------- 
text = "" 
num_success = 0 
num_fail = 0 
 
 
# 测试通过 
def decide_success(joggle): 
  global num_success 
  num_success += 1 
  print_out(joggle + ":接口测试通过\n") 
  return num_success 
 
 
# 测试不通过 
def decide_fail(txt, joggle): 
  global num_fail 
  num_fail += 1 
  print_out(joggle + ":接口测试未通过 \n错误信息: " + txt + "\n") 
  return num_fail 
 
 
# 邮件内容写入 & 客户端输出 
def print_out(message): 
  global text 
  text += "\n" + message 
  return text 
  
# 返回值判断 
def decide_result(result, code, joggle): 
  if result['code'] == code: 
    decide_success(joggle) 
    return "pass" 
  else: 
    txt = u"期望返回值:" + str(code) + u" 实际返回值:" + str(result) + '\n' + result['message'] 
    decide_fail(txt, joggle) 
    return "fail" 
 
 
def decide_Count(): 
  data = { 
    'num_success': num_success, 
    'num_fail': num_fail, 
    'text': text 
  } 
  return data 
# -------------------------------------------------------------------------------------------------------------------- 
# 定义 unittest 
class MyTestCase(unittest.TestCase):  
 
  # 初始化工作 
  def setUp(self): 
    pass  
 
  # 退出清理工作 
  def tearDown(self): 
    pass 
  def test_Case1(self): 
  id = 16 
    data = {'id':id} 
    a = TestInface.new_data(data) 
    decide_result(a,0,'test_Case1')
Copy after login

3. Use suite to manage the case

# TestSuite.py 
# -*- coding:utf-8 -*- 
import unittest 
import TestCase 
  
def test_InterFace(): 
  
  # 构造测试集 
  suite = unittest.TestSuite() 
  suite.addTest(TestCase("test_Case1")) # unittest中的测试用例 
 
 
  runner = unittest.TextTestRunner() 
  runner.run(suite) 
  # 对测试集进行测试需要返回值 
  # return suite 
 
if __name__ == '__main__': 
  
  # unittest.main(defaultTest='test_InterFace') 
  # 执行测试 
  runner = unittest.TextTestRunner() 
  runner.run(test_InterFace())
Copy after login

4. Statistics of interface data

# TestCensus.py 
# -*- coding:utf-8 -*- 
import time 
import TestSuite 
import send_email 
import TestCase 
 
class Test_Calss(): 
 
  def census(self): 
    text = '' 
    # 初始化测试起始时间 
    start_time = time.time() 
    # 调用suite测试集 
    TestSuite.test_InterFace() 
    # 结束执行时间计算 
    end_time = time.time() 
 
    result = TestCase.decide_Count() 
    # 接口测试统计说明 
    total_use_case = u"执行用例总数:" + str(result['num_success'] + result['num_fail']) + \ 
             u"\t通过数:" + str(result['num_success']) + \ 
             u"\t不通过数:" + str(result['num_fail']) 
    total_time = u"\t总共耗时:" + str(round((end_time - start_time), 3)) + u'秒' 
    text = result['text'] + total_use_case + total_time 
    print (text) 
 
    # 发生测试报告邮件 
    send_email.email_file(text) 
 
if __name__ == '__main__': 
  Test_Calss().census()
Copy after login

Related recommendations:

Unitest usage examples in Python

Concise usage examples of unittest in Python unit testing framework

Usage examples of unittest module in Python for UT (unit testing)

The above is the detailed content of How to use unittest test interface in python_python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!