This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""get input test case lists for parametrized tests""" | |
import os | |
from os import path | |
# root_path is parent folder of Scripts folder | |
root_path = path.dirname(path.dirname(path.realpath(__file__))) | |
test_case_list = [] | |
input_root = path.join(root_path, "inputs") | |
for tc in os.listdir(input_root): | |
if tc.startswith("test_case"): | |
test_case_list.append(tc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ini_to_dict(input): | |
"""Covert a ini file to a simple dict | |
""" | |
with open(input) as f: | |
content = f.read() | |
ret_dict = {} | |
for line in content.split("\n"): | |
if " = " in line: | |
key, value = line.split(" = ", maxsplit=1) | |
ret_dict[key] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_test_input(filename): | |
import re | |
with open(filename, "r") as f: | |
content = f.read() | |
# 3 parts split by empty line | |
parts = re.split("\n\n", content) | |
parts_len = len(parts) | |
# part 1: Method and url | |
assert len(parts[0].split()) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dict_to_ini(dict_var, file=None): | |
ini_content_list = [] | |
def iterate_dict(var, prefix=None): | |
"""iterate dict and convert to a list of 'key1.key2[i] = value' string""" | |
if isinstance(var, dict): | |
for k, v in var.items(): | |
if prefix is None: | |
new_prefix = k # e.g. age | |
else: | |
new_prefix = prefix + "." + k # e.g. name.firstname |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_ignore_file(file): | |
"""Parse ignore file and return a list of ignored keys""" | |
from os import path | |
ignore_keys = [] | |
if path.isfile(file): | |
with open(file) as f: | |
for line in f: | |
if line.strip != "": | |
ignore_keys.append(line.strip()) | |
return ignore_keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
@pytest.mark.parametrize("testcase_folder", test_case_list) | |
def test_by_input_output_text(testcase_folder): | |
"""test by input and expected output text files""" | |
import os | |
from os import path | |
import requests | |
input_root = path.join(root_path, "inputs") | |
output_root = path.join(root_path, "outputs") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""get input test case lists for parametrized tests""" | |
import os | |
from os import path | |
# root_path is parent folder of Scripts folder | |
root_path = path.dirname(path.dirname(path.realpath(__file__))) | |
test_case_list = [] | |
input_root = path.join(root_path, "inputs") | |
for tc in os.listdir(input_root): | |
if tc.startswith("test_case"): | |
test_case_list.append(tc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def diff_simple_dict(expected, actual, ignore=[], output_file=None): | |
"""Compare simple dict generated by ini_to_dict | |
""" | |
diff_list = [] | |
for key in expected: | |
if key not in ignore: | |
# missing in actual | |
if key not in actual: | |
diff_list.append("- %s = %s" % (key, expected[key])) | |
# diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""get input test case lists for parametrized tests""" | |
import os | |
from os import path | |
# root_path is parent folder of Scripts folder | |
# root_path = path.dirname(path.dirname(path.realpath(__file__))) | |
root_path = path.dirname(path.dirname(path.dirname(path.realpath(__file__)))) # if inside code_snippets subfolder | |
test_case_list = [] | |
input_root = path.join(root_path, "inputs") | |
for tc in os.listdir(input_root): | |
if tc.startswith("test_case"): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dotmap import DotMap | |
person = { | |
"name": { | |
"firstname": "Peter", | |
"secondname": "Xie" | |
}, | |
"age": 22 | |
}; |
NewerOlder