case校验工具

校验类:compare_json.py
 

# encoding:utf-8
import json
import sys
import types

TYPE = 'TYPE'
PATH = 'PATH'
VALUE = 'VALUE'

# Borrowed from http://djangosnippets.org/snippets/2247/
# with some modifications.
class Diff(object):
  def __init__(self, first, second, with_values=False):
    self.difference = []
    self.seen = []
    not_with_values = not with_values
    self.check(first, second, with_values=with_values)

  def check(self, first, second, path='', with_values=False):
    if with_values and second != None:
      if not isinstance(first, type(second)):
        message = '%s - %s, %s' % (path, type(first).__name__, type(second).__name__)
        self.save_diff(message, TYPE)

    if isinstance(first, dict):
      for key in first:
        # the first part of path must not have trailing dot.
        if len(path) == 0:
          new_path = key
        else:
          new_path = "%s.%s" % (path, key)

        if isinstance(second, dict):
          if second.has_key(key):
            sec = second[key]
          else:
            #  there are key in the first, that is not presented in the second
            self.save_diff(new_path, PATH)

            # prevent further values checking.
            sec = None

          # recursive call
          if sec != None:
            self.check(first[key], sec, path=new_path, with_values=with_values)
        else:
          # second is not dict. every key from first goes to the difference
          self.save_diff(new_path, PATH)
          self.check(first[key], second, path=new_path, with_values=with_values)

    # if object is list, loop over it and check.
    elif isinstance(first, list):
      for (index, item) in enumerate(first):
        new_path = "%s[%s]" % (path, index)
        # try to get the same index from second
        sec = None
        if second != None:
          try:
            sec = second[index]
          except (IndexError, KeyError):
            # goes to difference
            self.save_diff('%s - %s' % (new_path, type(item).__name__), TYPE)

        # recursive call
        self.check(first[index], sec, path=new_path, with_values=with_values)

    # not list, not dict. check for equality (only if with_values is True) and return.
    else:
      if with_values and second != None:
        if first != second:
          self.save_diff('%s - %s | %s' % (path, first, second), VALUE)
      return

  def save_diff(self, diff_message, type_):
    if diff_message not in self.difference:
      self.seen.append(diff_message)
      self.difference.append((type_, diff_message))

def getContentFromFile(filePath):
  return open(filePath, 'r').read()

def getContent(location):
  content = None
  if type(location) is types.DictType:
    return location
  content = getContentFromFile(location)
  if content is None:
    raise Error("Could not load content for " + location)
  return json.loads(content)

def compare(location1, location2):
  json1 = getContent(location1)
  json2 = getContent(location2)
  diff1 = Diff(json1, json2, True).difference
  diff2 = Diff(json2, json1, False).difference
  diffs = []
  for type, message in diff1:
    newType = 'CHANGED'
    if type == PATH:
      newType = 'REMOVED'
    diffs.append({'type': newType, 'message': message})
  for type, message in diff2:
    diffs.append({'type': 'ADDED', 'message': message})
  return diffs

# 判断sample json是否是target json的子集, sample json和target json都是文件名
def isSubsetJsonFile(sample_json, target_json):
    json1 = getContent(sample_json)
    json2 = getContent(target_json)
    diff1 = Diff(json1, json2, True).difference
    diffs = []
    for type, message in diff1:
        newType = 'CHANGED'
        if type == PATH:
            newType = 'REMOVED'
        diffs.append({'type': newType, 'message': message})
    if len(diffs) > 0:
        return False
    else:
        return True

def isSubsetJson(sample_json, target_json):
    json1 = json.loads(sample_json)
    json2 = json.loads(target_json)

    diff1 = Diff(json1, json2, True).difference
    diffs = []
    for type, message in diff1:
        newType = 'CHANGED'
        if type == PATH:
            newType = 'REMOVED'
        diffs.append({'type': newType, 'message': message})
    if len(diffs) > 0:
        return False
    else:
        return True

if __name__ == '__main__':
  # 仅为测试入口,正式使用为函数级别从外部调用
  '''
  if len(sys.argv) != 3:
    sys.exit('Error, Usage: python compareJson.py sample.json full.json')
  location1 = sys.argv[1]
  location2 = sys.argv[2]
  result = isSubsetJsonFile(location1, location2)
  if result == True:
    print str(location1) + " in " + str(location2)
  else:
    print str(location1) + " not in " + str(location2)
  '''
  str1 = '{"code":0,"msg":"","data":{"auctions":[{"result":{"device_id":"32011276001320200162","non_motor_shot_time":"2020-11-10 00:00:08"}}]}}'
  str2 = '{"code":0,"msg":"","data":{"auctions":null,"schema":{"scoreDimension":0}}}'
  if isSubsetJson(str1, str2):
      print 'str1 in str2'
  else:
      print 'not in'

校验case方法:run_test_cases.py
 


#coding:utf-8
import os
import sys
import urllib2
import json
import Queue
import threading
import datetime
import compare_json
import requests
import time

THREAD_NUM = 10 # concurrenty threads num for send searcher request

lock=threading.Lock()
lock1=threading.Lock()
lock2=threading.Lock()
task_queue = Queue.Queue()

def runTestCase(failed_cast_list):
    while True:
        # 先要获取锁
        lock.acquire()
        if task_queue.empty():
            # 释放锁
            lock.release()
            return
        line = task_queue.get(block=False)
        lock.release()
        items = line.split('')
        if (len(items) != 2) and (len(items) != 3):
            print 'format error, please check line ' + line
            lock1.acquire()
            failed_cast_list.append(line)
            lock1.release()
            continue
        
        result = ""
        expect_result = ""
        error_request = ""
        try:
            # get request
            '''
                通过case请求长度判断使用哪种请求放上,长度为2使用get请求,长度为3使用post请求
            '''
            if len(items) == 2:
                expect_result = items[1]
                error_request = items[0]
                req = urllib2.urlopen(items[0])
                result = req.read()
            else:
                expect_result = items[2]
                error_request = items[0] +"==data==" +items[1]
                res = requests.post(url=items[0], data=items[1])
                result = res.text
        except:
            lock1.acquire()
            print('url open failed')
            failed_cast_list.append(line)
            lock1.release()
            continue

        if compare_json.isSubsetJson(expect_result , result) == False:
            lock1.acquire()
            failed_cast_list.append(line)
            print "test case expect result:" + expect_result
            print "actual result:" + result
            print "\033[1;31;40mcase failed\033[0m"
            print "run time: "+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) +", error equest:" + error_request
            lock1.release()

def runAllTestCases(test_case_file, host):
    file1 = open(test_case_file, 'r')
    start = datetime.datetime.now()
    # quese size unlimited
    for line in file1.readlines():
        line = line.strip()
        if line.startswith('/'):
            line = host + line
        else:
            line = host + "/" + line
        task_queue.put(line)
    
    threads = []
    failed_cast_list = []
    count = task_queue.qsize()

    for i in range(THREAD_NUM):
        t = threading.Thread(target=runTestCase, args=(failed_cast_list,))
        threads.append(t)
    for i in range(THREAD_NUM):
        threads[i].start()
    for i in range(THREAD_NUM):
        threads[i].join()

    end = datetime.datetime.now()
    cost = (end -start).seconds
    
    all_passed = True
    if len(failed_cast_list) == 0:
        print "time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print('\033[1;32;40mRun all %d case passed, cost %d seconds!\033[0m' % (count, cost))
    else:
        all_passed = False
        succeed_count = count - len(failed_cast_list)
        print "time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print('\033[1;31;40mRun %d case passed, %d case failed, cost %d seconds!\033[0m' \
            % (succeed_count, len(failed_cast_list) ,cost))
    return all_passed

if __name__ == '__main__':
    # main函数仅为测试用,正常入口为函数级别从外部调用
    if len(sys.argv) != 2:
        sys.exit('Error, Usage: python run_test_cases.py test_case_file')
    test_case_file = sys.argv[1]
    host = "http://33.7.245.208:15001"
    runAllTestCases(test_case_file, host)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值