Best Python code snippet using autotest_python
test_hook.py
Source:test_hook.py
...18from test import _common19from test.helper import TestHelper, capture_log20from beets import config21from beets import plugins22def get_temporary_path():23 temporary_directory = tempfile._get_default_tempdir()24 temporary_name = next(tempfile._get_candidate_names())25 return os.path.join(temporary_directory, temporary_name)26class HookTest(_common.TestCase, TestHelper):27 TEST_HOOK_COUNT = 528 def setUp(self):29 self.setup_beets()30 def tearDown(self):31 self.unload_plugins()32 self.teardown_beets()33 def _add_hook(self, event, command):34 hook = {35 'event': event,36 'command': command37 }38 hooks = config['hook']['hooks'].get(list) if 'hook' in config else []39 hooks.append(hook)40 config['hook']['hooks'] = hooks41 def test_hook_empty_command(self):42 self._add_hook('test_event', '')43 self.load_plugins('hook')44 with capture_log('beets.hook') as logs:45 plugins.send('test_event')46 self.assertIn('hook: invalid command ""', logs)47 @unittest.skipIf(sys.platform, 'win32') # FIXME: fails on windows48 def test_hook_non_zero_exit(self):49 self._add_hook('test_event', 'sh -c "exit 1"')50 self.load_plugins('hook')51 with capture_log('beets.hook') as logs:52 plugins.send('test_event')53 self.assertIn('hook: hook for test_event exited with status 1', logs)54 def test_hook_non_existent_command(self):55 self._add_hook('test_event', 'non-existent-command')56 self.load_plugins('hook')57 with capture_log('beets.hook') as logs:58 plugins.send('test_event')59 self.assertTrue(any(60 message.startswith("hook: hook for test_event failed: ")61 for message in logs))62 @unittest.skipIf(sys.platform, 'win32') # FIXME: fails on windows63 def test_hook_no_arguments(self):64 temporary_paths = [65 get_temporary_path() for i in range(self.TEST_HOOK_COUNT)66 ]67 for index, path in enumerate(temporary_paths):68 self._add_hook(f'test_no_argument_event_{index}',69 f'touch "{path}"')70 self.load_plugins('hook')71 for index in range(len(temporary_paths)):72 plugins.send(f'test_no_argument_event_{index}')73 for path in temporary_paths:74 self.assertTrue(os.path.isfile(path))75 os.remove(path)76 @unittest.skipIf(sys.platform, 'win32') # FIXME: fails on windows77 def test_hook_event_substitution(self):78 temporary_directory = tempfile._get_default_tempdir()79 event_names = [f'test_event_event_{i}' for i in80 range(self.TEST_HOOK_COUNT)]81 for event in event_names:82 self._add_hook(event,83 f'touch "{temporary_directory}/{{event}}"')84 self.load_plugins('hook')85 for event in event_names:86 plugins.send(event)87 for event in event_names:88 path = os.path.join(temporary_directory, event)89 self.assertTrue(os.path.isfile(path))90 os.remove(path)91 @unittest.skipIf(sys.platform, 'win32') # FIXME: fails on windows92 def test_hook_argument_substitution(self):93 temporary_paths = [94 get_temporary_path() for i in range(self.TEST_HOOK_COUNT)95 ]96 for index, path in enumerate(temporary_paths):97 self._add_hook(f'test_argument_event_{index}',98 'touch "{path}"')99 self.load_plugins('hook')100 for index, path in enumerate(temporary_paths):101 plugins.send(f'test_argument_event_{index}', path=path)102 for path in temporary_paths:103 self.assertTrue(os.path.isfile(path))104 os.remove(path)105 @unittest.skipIf(sys.platform, 'win32') # FIXME: fails on windows106 def test_hook_bytes_interpolation(self):107 temporary_paths = [108 get_temporary_path().encode('utf-8')109 for i in range(self.TEST_HOOK_COUNT)110 ]111 for index, path in enumerate(temporary_paths):112 self._add_hook(f'test_bytes_event_{index}',113 'touch "{path}"')114 self.load_plugins('hook')115 for index, path in enumerate(temporary_paths):116 plugins.send(f'test_bytes_event_{index}', path=path)117 for path in temporary_paths:118 self.assertTrue(os.path.isfile(path))119 os.remove(path)120def suite():121 return unittest.TestLoader().loadTestsFromName(__name__)122if __name__ == '__main__':...
image_utils.py
Source:image_utils.py
...57 temp_directory = tempfile.TemporaryDirectory()58 if isinstance(reference, str):59 reference_path = reference60 else:61 reference_path = utils.get_temporary_path(directory=temp_directory.name,62 extension='.nii.gz')63 nibabel.save(reference, reference_path)64 if isinstance(volume, str):65 volume_path = volume66 else:67 volume_path = utils.get_temporary_path(directory=temp_directory.name, extension='.nii.gz')68 nibabel.save(volume, volume_path)69 registered_volume_path = utils.get_temporary_path(directory=temp_directory.name,70 extension='.nii')71 cmd = shlex.split('3dvolreg -base {} -prefix {}'.format(reference_path,72 registered_volume_path))73 if output_transform:74 transform_path = utils.get_temporary_path(temp_directory.name, extension='.aff12.1D')75 cmd.extend(['-1Dmatrix_save', transform_path])76 if twopass:77 cmd.append('-twopass')78 cmd.append(volume_path)79 env = os.environ.copy()80 env['AFNI_NIFTI_TYPE_WARN'] = 'NO'81 error_message = utils.run_command(cmd, raise_errors=False, env=env)82 if error_message is not None:83 logger.debug(error_message)84 registered_volume = nibabel.load(registered_volume_path)85 registered_volume.get_data()86 if output_transform:87 xfm = load_afni_xfm(transform_path)88 return registered_volume, xfm...
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!!