How to use test_case_start method in Testify

Best Python code snippet using Testify_python

parser.py

Source:parser.py Github

copy

Full Screen

1#!/usr/bin/env python32import os3import base644import copy5import random6import string7import hashlib8import json9import xml.etree.ElementTree as ET10class FPSParser(object):11 def __init__(self, fps_path=None, string_data=None):12 if fps_path:13 self._etree = ET.parse(fps_path).getroot()14 elif string_data:15 self._ertree = ET.fromstring(string_data).getroot()16 else:17 raise ValueError("You must tell me the file path or directly give me the data for the file")18 version = self._etree.attrib.get("version", "No Version")19 if version not in ["1.1", "1.2"]:20 raise ValueError("Unsupported version '" + version + "'")21 @property22 def etree(self):23 return self._etree24 def parse(self):25 ret = []26 for node in self._etree:27 if node.tag == "item":28 ret.append(self._parse_one_problem(node))29 return ret30 def _parse_one_problem(self, node):31 sample_start = True32 test_case_start = True33 problem = {"title": "No Title", "description": "No Description",34 "input": "No Input Description",35 "output": "No Output Description",36 "memory_limit": {"unit": None, "value": None},37 "time_limit": {"unit": None, "value": None},38 "samples": [], "images": [], "append": [],39 "template": [], "prepend": [], "test_cases": [],40 "hint": None, "source": None, "spj": None, "solution": []}41 for item in node:42 tag = item.tag43 if tag in ["title", "description", "input", "output", "hint", "source"]:44 problem[item.tag] = item.text45 elif tag == "time_limit":46 unit = item.attrib.get("unit", "s")47 if unit not in ["s", "ms"]:48 raise ValueError("Invalid time limit unit")49 problem["time_limit"]["unit"] = item.attrib.get("unit", "s")50 value = int(item.text)51 if value <= 0:52 raise ValueError("Invalid time limit value")53 problem["time_limit"]["value"] = value54 elif tag == "memory_limit":55 unit = item.attrib.get("unit", "MB")56 if unit not in ["MB", "KB", "mb", "kb"]:57 raise ValueError("Invalid memory limit unit")58 problem["memory_limit"]["unit"] = unit.upper()59 value = int(item.text)60 if value <= 0:61 raise ValueError("Invalid memory limit value")62 problem["memory_limit"]["value"] = value63 elif tag in ["template", "append", "prepend", "solution"]:64 lang = item.attrib.get("language")65 if not lang:66 raise ValueError("Invalid " + tag + ", language name is missed")67 problem[tag].append({"language": lang, "code": item.text})68 elif tag == "spj":69 lang = item.attrib.get("language")70 if not lang:71 raise ValueError("Invalid spj, language name if missed")72 problem["spj"] = {"language": lang, "code": item.text}73 elif tag == "img":74 problem["images"].append({"src": None, "blob": None})75 for child in item:76 if child.tag == "src":77 problem["images"][-1]["src"] = child.text78 elif child.tag == "base64":79 problem["images"][-1]["blob"] = base64.b64decode(child.text)80 elif tag == "sample_input":81 if not sample_start:82 raise ValueError("Invalid xml, error 'sample_input' tag order")83 problem["samples"].append({"input": item.text, "output": None})84 sample_start = False85 elif tag == "sample_output":86 if sample_start:87 raise ValueError("Invalid xml, error 'sample_output' tag order")88 problem["samples"][-1]["output"] = item.text89 sample_start = True90 elif tag == "test_input":91 if not test_case_start:92 raise ValueError("Invalid xml, error 'test_input' tag order")93 problem["test_cases"].append({"input": item.text, "output": None})94 test_case_start = False95 elif tag == "test_output":96 if test_case_start:97 raise ValueError("Invalid xml, error 'test_output' tag order")98 problem["test_cases"][-1]["output"] = item.text99 test_case_start = True100 return problem101class FPSHelper(object):102 def save_image(self, problem, base_dir, base_url):103 _problem = copy.deepcopy(problem)104 for img in _problem["images"]:105 name = "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(12))106 ext = os.path.splitext(img["src"])[1]107 file_name = name + ext108 with open(os.path.join(base_dir, file_name), "wb") as f:109 f.write(img["blob"])110 for item in ["description", "input", "output"]:111 _problem[item] = _problem[item].replace(img["src"], os.path.join(base_url, file_name))112 return _problem113 # {114 # "spj": false,115 # "test_cases": {116 # "1": {117 # "stripped_output_md5": "84f244e41d3c8fd4bdb43ed0e1f7a067",118 # "input_size": 12,119 # "output_size": 7,120 # "input_name": "1.in",121 # "output_name": "1.out"122 # }123 # }124 # }125 def save_test_case(self, problem, base_dir):126 spj = problem.get("spj", {})127 test_cases = {}128 for index, item in enumerate(problem["test_cases"]):129 input_content = item.get("input")130 output_content = item.get("output")131 if input_content:132 with open(os.path.join(base_dir, str(index + 1) + ".in"), "w", encoding="utf-8") as f:133 f.write(input_content)134 if output_content:135 with open(os.path.join(base_dir, str(index + 1) + ".out"), "w", encoding="utf-8") as f:136 f.write(output_content)137 if spj:138 one_info = {139 "input_size": len(input_content),140 "input_name": f"{index}.in"141 }142 else:143 one_info = {144 "input_size": len(input_content),145 "input_name": f"{index}.in",146 "output_size": len(output_content),147 "output_name": f"{index}.out",148 "stripped_output_md5": hashlib.md5(output_content.rstrip().encode("utf-8")).hexdigest()149 }150 test_cases[index] = one_info151 info = {152 "spj": True if spj else False,153 "test_cases": test_cases154 }155 with open(os.path.join(base_dir, "info"), "w", encoding="utf-8") as f:156 f.write(json.dumps(info, indent=4))157if __name__ == "__main__":158 import pprint159 import os160 parser = FPSParser("fps.xml")161 helper = FPSHelper()162 problems = parser.parse()163 for index, problem in enumerate(problems):164 path = os.path.join("/tmp/", str(index + 1))165 os.mkdir(path)166 helper.save_test_case(problem, path)...

Full Screen

Full Screen

test_model.py

Source:test_model.py Github

copy

Full Screen

1import model2import random3import arrow4import nose5import logging6logging.basicConfig(format='%(levelname)s:%(message)s',7 level=logging.WARNING)8log = logging.getLogger(__name__)9range_day = 710range_hour = 811now = arrow.now()12now_min = now.format("mm")13now_hour = int(now.format('HH'))14then = now.shift(days=range_day, hours=range_hour)15then_min = then.format("mm")16RANGE = model.calendar_event(now.isoformat(), then.isoformat())17def test_without():18 test_case_start = now.shift(days=-1)19 test_case_end = test_case_start.shift(seconds=1)20 test_case = model.calendar_event(test_case_start, test_case_end)21 assert test_case.compare_to(RANGE) == model.event_compare_result.without22 test_case_start = now.shift(hours=-3)23 test_case_end = test_case_start.shift(hours=2)24 test_case = model.calendar_event(test_case_start, test_case_end)25 assert test_case.compare_to(RANGE) == model.event_compare_result.without26 test_case_start = then.shift(days=1)27 test_case_end = test_case_start.shift(hours=3)28 test_case = model.calendar_event(test_case_start, test_case_end)29 assert test_case.compare_to(RANGE) == model.event_compare_result.without30 for i in range(10000):31 test_case_start = now.shift(days=random.randrange(range_day))32 test_case_start = test_case_start.shift(hours=random.randrange(-(24-range_hour), -1))33 test_case_end = test_case_start.shift(hours=1)34 test_case = model.calendar_event(test_case_start, test_case_end)35 logging.info(str(test_case))36 assert test_case.compare_to(RANGE) == model.event_compare_result.without37 logging.info("Passed.")38 test_case_start = then.shift(days=random.randrange(-range_day, 0))39 test_case_start = test_case_end.shift(hours=random.randrange(1, (24-range_hour)))40 test_case_end = test_case_start.shift(hours=1)41 logging.info(str(test_case))42 assert test_case.compare_to(RANGE) == model.event_compare_result.without43 logging.info("Passed.")44def test_within():45 test_case_start = now.shift(days=1)46 test_case_end = test_case_start.shift(seconds=1)47 test_case = model.calendar_event(test_case_start, test_case_end)48 assert test_case.compare_to(RANGE) == model.event_compare_result.within49 test_case_start = now.shift(hours=-3)50 test_case_end = test_case_start.shift(hours=4)51 test_case = model.calendar_event(test_case_start, test_case_end)52 assert test_case.compare_to(RANGE) == model.event_compare_result.within53 test_case_start = then.shift(days=-1)54 test_case_end = test_case_start.shift(days=2)55 test_case = model.calendar_event(test_case_start, test_case_end)56 assert test_case.compare_to(RANGE) == model.event_compare_result.within57 for i in range(10000):58 test_case_start = now.shift(days=random.randrange(range_day))59 test_case_start = test_case_start.shift(hours=random.randrange(1, range_hour))60 test_case_end = test_case_start.shift(hours=1)61 test_case = model.calendar_event(test_case_start, test_case_end)62 logging.info(str(test_case))63 logging.info("Range: " + str(RANGE))64 assert test_case.compare_to(RANGE) == model.event_compare_result.within65 logging.info("Passed.")66 test_case_start = then.shift(days=random.randrange(-range_day, 0))67 test_case_start = test_case_end.shift(hours=random.randrange(1, range_hour))68 test_case_end = test_case_start.shift(hours=1)69 logging.info(str(test_case))70 assert test_case.compare_to(RANGE) == model.event_compare_result.within...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful