pip install unittest
pip install ddt
# coding: utf-8
import unittest
import ddt
from xmlrunner import XMLTestRunner
from Predict import Predict
from char_cnn_api import get_csv_data, write_csv_data
@ddt.ddt
class CharCnnTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.test_res = {'matched': [], 'unmatched': [], 'results': []}
#数据源获取
@ddt.data(*get_csv_data('./test_data/CurrentPolitical.csv'))
@ddt.unpack
def test_char_cnn_predict(self, data, result):
predict_test = Predict()
ret = predict_test.http_predict(data)
self.test_res['results'].append(str(ret) + data)
print(ret)
if ret['output'] == result:
self.test_res['matched'].append(data)
elif ret['output'] != result:
self.test_res['unmatched'].append(data)
@classmethod
def tearDownClass(cls):
#过滤存储
for key, val in cls.test_res.items():
write_csv_data('./report/{}.csv'.format(key), val)
if __name__ == '__main__':
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(CharCnnTest))
with open('report/test_result.xml', 'w') as output:
runner = XMLTestRunner(output=output, verbosity=2)
report = runner.run(suite)
if not report.wasSuccessful():
exit(1)
# coding: utf-8
import csv
import requests
import json
import numpy as np
import pandas
def get_csv_data(csv_path):
"""
read test data from csv and return as list
@type csv_path: string
@param csv_path: some csv path string
@return list
"""
rows = []
with open(str(csv_path), "r") as fp:
content = fp.readlines()
for line in content:
line = str(line).replace(',', '。').replace(',', '。')
rows.append([line, 1])
return rows
def write_csv_data(csv_path, data):
with open(str(csv_path), 'w') as fp:
for line in data:
fp.write(line)
以上就是简单实现ddt过程。不过这里数据量很多,还需要使用多线程,多进程加快执行用例。