From 1c44ae764bcc0e8b28202786310ca7dba3817585 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 22 Mar 2018 21:28:46 +0800 Subject: [PATCH] refactor: load csv file with csv.DictReader --- httprunner/__about__.py | 2 +- httprunner/testcase.py | 31 +++++-------------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 44b5f9db..e9280236 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '1.3.1' +__version__ = '1.3.2' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/testcase.py b/httprunner/testcase.py index 75df5987..4d673b4b 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -1,4 +1,5 @@ import ast +import csv import io import itertools import json @@ -62,33 +63,11 @@ def _load_csv_file(csv_file): ] """ csv_content_list = [] - parameter_list = None - collums_num = 0 - with io.open(csv_file, encoding='utf-8') as data_file: - for line in data_file: - line_data = line.strip().split(",") - if line_data == [""]: - # ignore empty line - continue - if not parameter_list: - # first line will always be parameter name - parameter_list = line_data - collums_num = len(parameter_list) - continue - - # from the second line - if len(line_data) != collums_num: - err_msg = "CSV file collums does match with headers.\n" - err_msg += "\tcsv file path: {}\n".format(csv_file) - err_msg += "\terror line content: {}".format(line_data) - raise exception.FileFormatError(err_msg) - else: - data = {} - for index, parameter_name in enumerate(parameter_list): - data[parameter_name] = line_data[index] - - csv_content_list.append(data) + with io.open(csv_file, encoding='utf-8') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + csv_content_list.append(row) return csv_content_list