Best Python code snippet using molotov_python
__init__.py
Source:__init__.py
1import check502from re import escape, search, sub3"""4Setup5"""6@check50.check()7def exists():8 """working.py and test_working.py exist"""9 check50.exists("working.py")10 check50.exists("test_working.py")11@check50.check(exists)12def libraries():13 """working.py does not import libraries other than sys and re"""14 with open("working.py", "r") as file:15 contents = file.read()16 if search(r'(?<!#)(?<! )((import(?![ \t]*(re|sys)\b))|(\bfrom\b(?![ \t]*(re|sys)\b)))', contents):17 raise check50.Failure("working.py imports libraries other than sys and re", help="Be sure only to use \"import re\" and \"import sys\", or \"from re import ...\" and \"from sys import ...\"")18"""19working.py checks20"""21@check50.check(libraries)22def convert_9_to_5_short():23 """working.py converts \"9 AM to 5 PM\" to \"09:00 to 17:00\""""24 test_valid_time(input="9 AM to 5 PM", output="09:00 to 17:00")25@check50.check(libraries)26def convert_9_to_5_long():27 """working.py converts \"9:00 AM to 5:00 PM\" to \"09:00 to 17:00\""""28 test_valid_time(input="9:00 AM to 5:00 PM", output="09:00 to 17:00")29@check50.check(libraries)30def convert_8_to_8_short():31 """working.py converts \"8 PM to 8 AM\" to \"20:00 to 08:00\""""32 test_valid_time(input="8 PM to 8 AM", output="20:00 to 08:00")33@check50.check(libraries)34def convert_8_to_8_long():35 """working.py converts \"8:00 PM to 8:00 AM\" to \"20:00 to 08:00\""""36 test_valid_time(input="8:00 PM to 8:00 AM", output="20:00 to 08:00")37@check50.check(libraries)38def convert_12_to_12_short():39 """working.py converts \"12 AM to 12 PM\" to \"00:00 to 12:00\""""40 test_valid_time(input="12 AM to 12 PM", output="00:00 to 12:00")41@check50.check(libraries)42def convert_12_to_12_long():43 """working.py converts \"12:00 AM to 12:00 PM\" to \"00:00 to 12:00\""""44 test_valid_time(input="12:00 AM to 12:00 PM", output="00:00 to 12:00")45@check50.check(libraries)46def raise_for_invalid_time():47 """working.py raises ValueError when given \"8:60 AM to 4:60 PM\""""48 test_invalid_time(input="8:60 AM to 4:60 PM", error="ValueError")49@check50.check(libraries)50def raise_for_invalid_spaces():51 """working.py raises ValueError when given \"9AM to 5PM\""""52 test_invalid_time(input="9AM to 5PM", error="ValueError")53@check50.check(libraries)54def raise_for_invalid_format_24_hour():55 """working.py raises ValueError when given \"09:00 to 17:00\""""56 test_invalid_time(input="09:00 to 17:00", error="ValueError")57@check50.check(libraries)58def raise_for_invalid_format_dash():59 """working.py raises ValueError when given \"9 AM - 5 PM\""""60 test_invalid_time(input="9 AM - 5 PM", error="ValueError")61"""62test_working.py checks63"""64@check50.check(libraries)65def test_correct():66 """correct working.py passes all test_working checks"""67 test_implementation("working.py", "correct_test.pyc", "test_working.py", code=0)68@check50.check(test_correct)69def test_incorrect_hours():70 """test_working.py catches working.py printing incorrect hours"""71 test_implementation("working.py", "off_by_one_test.pyc", "test_working.py", code=1)72@check50.check(test_correct)73def test_incorrect_minutes():74 """test_working.py catches working.py printing incorrect minutes"""75 test_implementation("working.py", "incorrect_minutes_test.pyc", "test_working.py", code=1)76@check50.check(test_correct)77def test_raise_for_format():78 """test_working.py catches working.py not raising ValueError when user omits \" to \""""79 test_implementation("working.py", "raise_for_format_test.pyc", "test_working.py", code=1)80@check50.check(test_correct)81def test_raise_for_out_of_range_time():82 """test_working.py catches working.py not raising ValueError for out-of-range times"""83 test_implementation("working.py", "raise_for_format_test.pyc", "test_working.py", code=1)84@check50.check(test_correct)85def test_raise_for_invalid_time():86 """test_working.py catches working.py not raising ValueError for invalid time format"""87 test_implementation("working.py", "raise_for_format_test.pyc", "test_working.py", code=1)88"""89Helpers90"""91def test_valid_time(input, output):92 check50.run("python3 working.py").stdin(input, prompt=True).stdout(stdout_regex(output), output, regex=True).exit(0)93def test_invalid_time(input, error):94 check50.run("python3 working.py").stdin(input, prompt=True).stdout(traceback_regex(error), error, regex=True).exit(1)95def stdout_regex(text):96 """Match case-sensitively, allowing for only whitespace on either side"""97 return fr'^\s*{escape(text)}\s*$'98def traceback_regex(text):99 """Match case-sensitively, allowing for characters on either side"""100 return fr'^.*{escape(text)}.*$'101def test_implementation(base_filename, implementation_filename, test_filename, code=0):102 """Test implementation_file, an implementation of base_file, against student's checks in test_file. Expect a given exit status"""103 check50.include("pytest_helper.py")104 check50.include(implementation_filename)105 # Overwrite base_file with code to run implementation_file106 with open(base_filename, "w") as base_file, open("pytest_helper.py", "r") as pytest_helper:107 # Read text from pytest_helper108 pytest_helper_text = pytest_helper.read()109 # Replace open statement with implementation_file110 pytest_helper_text = sub("with open\(\".*\", \"rb\"\) as test_file:", f"with open(\"{implementation_filename}\", \"rb\") as test_file:", pytest_helper_text)111 # Write helper file text to base_file112 base_file.writelines(pytest_helper_text)113 # Expect that pytest will exit with given status code...
test_my_module.py
Source:test_my_module.py
...4 b = mm.B(member_a=a)5 print(f'b: {b}', type(b))6 print(f'b.member_a: {b.member_a}', type(b.member_a))7 b.bar(bar_arg='some argument')8def test_working() -> None:9 a = mm.A()10 b2 = mm.B2(member_a=a)11 print(f'b2: {b2}', type(b2))12 print(f'b2.member_a: {b2.member_a}', type(b2.member_a))13 b2.bar(bar_arg='some argument')14if __name__ == '__main__':15 test_working()...
test_main.py
Source:test_main.py
1# Smoke Test2def test_working():...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!