Best Python code snippet using slash
mockfile.py
Source:mockfile.py
...44 ... print(f.read())45 ...46 3L47 barfoo48 >>> def raise_io_error():49 ... raise IOError('message')50 ...51 >>> with MockFile(filename='filename',52 ... side_effect=lambda *args: raise_io_error()):53 ... open('filename')54 ...55 Traceback (most recent call last):56 File "<stdin>", line 2, in <module>57 File "mockfile.py", line 123, in __enter__58 return self.mock_open_file(self.filename, self.args)59 File "mockfile.py", line 102, in mock_open_file60 self.side_effect(*args)61 File "<stdin>", line 2, in <lambda>62 File "<stdin>", line 2, in raise_io_error63 IOError: message64 >>>65 .. note::66 Tested only for one file at time and for text files....
error_handling.py
Source:error_handling.py
...13 pass14def raise_exit(message):15 """Raise a ProgramExit with the specified message."""16 raise ProgramExit(message)17def raise_io_error(err_no: int, path: Optional[str]=None, message: Optional[str]=None):18 """Raise an IOError with an auto-generated message based on err_no."""19 if not message:20 message = os.strerror(err_no) + '.'21 if path:22 message += ' Path: ' + path23 e = IOError(message)24 e.errno = err_no25 raise e26def raise_not_found(path: Optional[str]=None, message: Optional[str]=None):27 """Raise a 'file not found' exception."""28 raise_io_error(errno.ENOENT, path, message)29def raise_if_none(**kwargs: Any) -> None:30 """Raise exception if any of the args is None."""31 for key in kwargs:32 if kwargs[key] is None:33 raise ValueError('Illegal "None" value for: ' + key)34def raise_if_falsy(**kwargs: Any):35 """Raise exception if any of the args is falsy."""36 for key in kwargs:37 value = kwargs[key]38 if not value:39 if value is None:40 adj = '"None"'41 elif value == 0:42 adj = 'zero'43 else:44 adj = 'empty'45 raise ValueError('Illegal {} value for: {}'.format(adj, key))46def raise_if_not_found(path, file_type):47 """Raise exception if the specified file does not exist."""48 raise_if_falsy(path)49 if file_type == FileType.FILE:50 test_func = os.path.isfile51 elif file_type == FileType.DIR:52 test_func = os.path.isdir53 else:54 test_func = os.path.exists55 if not test_func(path):56 raise_not_found(path)57def raise_if_exists(path):58 """Raise exception if a file already exists at a given path."""59 raise_if_falsy(path)60 if os.path.exists(path):61 raise_io_error(errno.EEXIST, path)62def raise_if_not_root():63 """Raise exception if the effective user is not root."""64 if os.geteuid() != 0:65 raise_io_error(errno.EPERM, message='This operation requires root privileges.')66def re_raise_new_message(exception: Exception, message: str):67 """Re-raise exception with a new message."""...
test_mockfile.py
Source:test_mockfile.py
...9 with open('filename') as f:10 assert f.read() == 'contentmore_content'11 assert not os.path.isfile('filename')12def test_mockfile_sideeffect():13 def raise_io_error():14 raise IOError('message')15 with pytest.raises(IOError) as excinfo:16 with MockFile(filename='filename',17 side_effect=lambda *args: raise_io_error()):18 open('filename')19 assert excinfo.value.args[0] == 'message'20def test_mockfile_saver():21 mfile = MockFile()22 assert mfile.saver == mfile.set_content23def test_set_filename():24 mfile = MockFile()25 mfile.set_filename('filename')26 assert mfile.filename == 'filename'27def test_set_content():28 mfile = MockFile()29 mfile.set_content('content')30 assert mfile.content == 'content'31def test_set_sideeffect():...
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!!