Best Python code snippet using Testify_python
test_case_test.py
Source:test_case_test.py
...22 assert self.test_2_run23class DeprecatedClassSetupFixturesGetRun(TestCase):24 def classSetUp(self):25 self.test_var = True26 def test_test_var(self):27 assert self.test_var28class DeprecatedSetupFixturesGetRun(TestCase):29 def setUp(self):30 self.test_var = True31 def test_test_var(self):32 assert self.test_var33class DeprecatedTeardownFixturesGetRun(TestCase):34 COUNTER = 035 def tearDown(self):36 self.test_var = True37 def test_test_var_pass_1(self):38 self.COUNTER += 139 if self.COUNTER > 1:40 assert self.test_var41 def test_test_var_pass_2(self):42 self.COUNTER += 143 if self.COUNTER > 1:44 assert self.test_var45class DeprecatedClassTeardownFixturesGetRun(TestCase):46 def test_placeholder(self):47 pass48 def class_teardown(self):49 self.test_var = True50 @class_teardown51 def test_test_var(self):52 assert self.test_var53class ClassSetupFixturesGetRun(TestCase):54 @class_setup55 def set_test_var(self):56 self.test_var = True57 def test_test_var(self):58 assert self.test_var59class SetupFixturesGetRun(TestCase):60 @setup61 def set_test_var(self):62 self.test_var = True63 def test_test_var(self):64 assert self.test_var65class TeardownFixturesGetRun(TestCase):66 COUNTER = 067 @teardown68 def set_test_var(self):69 self.test_var = True70 def test_test_var_first_pass(self):71 self.COUNTER += 172 if self.COUNTER > 1:73 assert self.test_var74 def test_test_var_second_pass(self):75 self.COUNTER += 176 if self.COUNTER > 1:77 assert self.test_var...
test_config.py
Source:test_config.py
1''' Unit tests for boss.config module. '''2import os3from mock import patch4from boss.core.util.string import strip_ansi5from boss.core.constants.config import DEFAULT_CONFIG6from boss.config import (7 load,8 parse_config,9 merge_config,10 is_vault_enabled,11 resolve_dotenv_file,12 use_vault_if_enabled,13 get_deployment_preset14)15SAMPLE_BOSS_YAML = '''16project_name: test-project17user: test-user18deployment:19 preset: web20 base_dir: ~/source/deployment21'''22def test_get_deployment_preset_returns_configured_preset():23 '''24 Check get_deployment_preset() function returns25 the configured deployment preset if it is configured.26 '''27 raw_config = {28 'deployment': {29 'preset': 'test-preset'30 }31 }32 preset = get_deployment_preset(raw_config)33 assert preset == 'test-preset'34def test_get_deployment_preset_returns_default_preset_if_not_set():35 '''36 Check get_deployment_preset() function returns,37 the default deployment preset if it is not configured.38 '''39 raw_config = {40 'project_name': 'test-project'41 }42 preset = get_deployment_preset(raw_config)43 assert preset == DEFAULT_CONFIG['deployment']['preset']44def test_merge_config_that_default_config_values_are_put():45 '''46 Ensure default values are put if config options are not provided.47 '''48 raw_config = {}49 result = merge_config(raw_config)50 assert result['user'] == DEFAULT_CONFIG['user']51 assert result['port'] == DEFAULT_CONFIG['port']52 assert result['deployment'] == DEFAULT_CONFIG['deployment']53def test_merge_config_that_default_config_could_be_overridden():54 '''55 Ensure default config values could be overridden for the56 config options that are set, however the rest of the options57 that aren't set would still take default values.58 '''59 raw_config = {60 'port': '1234',61 'deployment': {62 'base_dir': '~/some/directory'63 }64 }65 result = merge_config(raw_config)66 assert result['port'] == raw_config['port']67 assert result['deployment']['base_dir'] == raw_config[68 'deployment']['base_dir']69 # Not overridden, uses default values.70 assert result['user'] == DEFAULT_CONFIG['user']71 assert result['deployment'][72 'preset'] == DEFAULT_CONFIG['deployment']['preset']73def test_merge_config_base_config_is_merged_to_each_stage_specfic_config():74 '''75 Ensure the base deployment config and basic config are merged to76 each of the stage configurations77 '''78 raw_config = {79 'port': '1234',80 'remote_env_path': 'test',81 'deployment': {82 'base_dir': '~/some/directory'83 },84 'vault': {85 'enabled': True,86 'path': 'root/path'87 },88 'stages': {89 'stage1': {90 'host': 'stage1.example.com',91 'remote_env_path': 'best',92 'vault': {93 'path': 'root/path/stage1'94 }95 },96 'stage2': {97 'host': 'stage2.example.com',98 'port': '4321',99 'vault': {100 'path': 'root/path/stage2'101 }102 },103 'stage3': {104 }105 }106 }107 result = merge_config(raw_config)108 # Stage1109 stage1_config = result['stages']['stage1']110 assert stage1_config['port'] == raw_config['port']111 assert stage1_config['host'] == raw_config['stages']['stage1']['host']112 assert stage1_config['remote_env_path'] == raw_config['stages']['stage1']['remote_env_path']113 assert stage1_config['deployment'][114 'base_dir'] == raw_config['deployment']['base_dir']115 assert stage1_config['deployment'][116 'build_dir'] == DEFAULT_CONFIG['deployment']['build_dir']117 assert stage1_config['vault']['enabled'] is True118 assert stage1_config['vault']['path'] == 'root/path/stage1'119 # Stage 2120 stage2_config = result['stages']['stage2']121 assert stage2_config['port'] == raw_config['stages']['stage2']['port']122 assert stage2_config['host'] == raw_config['stages']['stage2']['host']123 assert stage2_config['remote_env_path'] == raw_config['remote_env_path']124 assert stage2_config['deployment'][125 'base_dir'] == raw_config['deployment']['base_dir']126 assert stage2_config['deployment'][127 'build_dir'] == DEFAULT_CONFIG['deployment']['build_dir']128 assert stage2_config['vault']['enabled'] is True129 assert stage2_config['vault']['path'] == 'root/path/stage2'130 # Stage 3131 stage3_config = result['stages']['stage3']132 assert stage3_config['vault']['enabled'] is True133 assert stage3_config['vault']['path'] == 'root/path'134@patch('boss.core.fs.read')135def test_load(read_mock):136 ''' Test load() function loads yaml file correctly. '''137 read_mock.return_value = SAMPLE_BOSS_YAML138 config_filename = 'test.yml'139 boss_config = load(config_filename)140 read_mock.assert_called_with(config_filename)141 # Configured options142 assert boss_config['user'] == 'test-user'143 assert boss_config['project_name'] == 'test-project'144 assert boss_config['deployment']['preset'] == 'web'145 assert boss_config['deployment']['base_dir'] == '~/source/deployment'146 # Default values resolved147 assert boss_config['port'] == 22148 assert boss_config['port'] == 22149@patch('boss.core.fs.read')150def test_load_with_env_vars(read_mock):151 '''152 Test load() function loads yaml file with env vars interpolation.153 '''154 read_mock.return_value = '''155 user: ${TEST_USER}156 project_name: ${TEST_PROJECT}157 port: 23158 deployment:159 preset: 'web'160 base_dir: ${TEST_BASE_DIR}161 '''162 os.environ['TEST_USER'] = 'test-user'163 os.environ['TEST_PROJECT'] = 'test-project'164 os.environ['TEST_BASE_DIR'] = '~/source/deployment'165 config_filename = 'test.yml'166 boss_config = load(config_filename)167 read_mock.assert_called_with(config_filename)168 # Configured options169 assert boss_config['user'] == 'test-user'170 assert boss_config['port'] == 23171 assert boss_config['project_name'] == 'test-project'172 assert boss_config['deployment']['preset'] == 'web'173 assert boss_config['deployment']['base_dir'] == '~/source/deployment'174 # Teardown175 os.environ['TEST_USER'] = ''176 os.environ['TEST_PROJECT'] = ''177 os.environ['TEST_BASE_DIR'] = ''178@patch('boss.core.fs.read')179@patch('boss.core.vault.read_secrets')180def test_load_with_env_vars_from_vault(read_secrets_mock, read_mock):181 '''182 Test load() function loads yaml file with183 env vars interpolation from vault.184 '''185 read_mock.return_value = '''186 user: ${TEST_USER}187 project_name: ${TEST_PROJECT}188 port: 24189 vault:190 enabled: true191 silent: true192 deployment:193 base_dir: ${TEST_BASE_DIR}194 test_var: $TEST_TEST_VAR195 stages:196 dev:197 test: test198 prod:199 user: $TEST_PROD_USER200 '''201 os.environ['TEST_USER'] = 'test-user-from-host'202 read_secrets_mock.return_value = {203 'TEST_PROJECT': 'test-project-from-vault',204 'TEST_BASE_DIR': 'test-base-dir-from-vault',205 'TEST_TEST_VAR': 'test-test-var-from-vault',206 'TEST_PROD_USER': 'test-prod-user-from-vault'207 }208 config_filename = 'test.yml'209 boss_config = load(config_filename)210 read_mock.assert_called_with(config_filename)211 read_secrets_mock.assert_called_with(DEFAULT_CONFIG['vault']['path'])212 # Configured options213 assert boss_config['user'] == 'test-user-from-host'214 assert boss_config['port'] == 24215 assert boss_config['project_name'] == 'test-project-from-vault'216 assert boss_config['deployment']['preset'] == 'remote-source'217 assert boss_config['deployment']['base_dir'] == 'test-base-dir-from-vault'218 # Stage specific219 assert boss_config['stages']['dev']['test'] == 'test'220 assert boss_config['stages']['dev']['user'] == 'test-user-from-host'221 assert boss_config['stages']['prod']['user'] == 'test-prod-user-from-vault'222 # Teardown223 os.environ['TEST_USER'] = ''224 os.environ['TEST_PROJECT'] = ''225 os.environ['TEST_BASE_DIR'] = ''226 os.environ['TEST_TEST_VAR'] = ''227 os.environ['TEST_PROD_USER'] = ''228@patch('boss.config.info')229@patch('dotenv.load_dotenv')230def test_resolve_dotenv_file_loads_dotenv_file_if_it_exists(load_dotenv_mock, info_m):231 ''' Test .env file is loaded if it exists. '''232 dotenv_path = '.env'233 with patch('boss.core.fs.exists', side_effect=lambda p: p == dotenv_path):234 resolve_dotenv_file('')235 load_dotenv_mock.assert_called_with(dotenv_path)236 msg = strip_ansi(info_m.call_args[0][0])237 assert msg == 'Resolving env file: .env'238@patch('boss.config.info')239@patch('boss.core.fs.exists')240@patch('dotenv.load_dotenv')241def test_resolve_dotenv_file_is_not_loaded_if_not_exists(load_dotenv_m, exists_m, _):242 ''' Test .env file is not loaded if it doesn't exists. '''243 exists_m.return_value = False244 resolve_dotenv_file('')245 load_dotenv_m.assert_not_called()246@patch('boss.config.info')247@patch('dotenv.load_dotenv')248def test_resolve_dotenv_file_loads_stage_specific_env_file(load_dotenv_m, info_m):249 ''' Test .env file is loaded if it exists. '''250 stage = 'production'251 def exists(p):252 return p == '.env.production'253 with patch('boss.core.fs.exists', side_effect=exists):254 resolve_dotenv_file('', stage)255 load_dotenv_m.assert_called_with('.env.production')256 msg = strip_ansi(info_m.call_args[0][0])257 assert msg == 'Resolving env file: .env.production'258@patch('boss.config.info')259@patch('dotenv.load_dotenv')260def test_resolve_dotenv_file_loads_env_file_if_stage_specific_file_doesnt_exist(261 load_dotenv_m, info_m):262 '''263 Test .env file is loaded as a fallback option,264 if stage is provided but stage specific env file doesn't exist.265 '''266 stage = 'production'267 def exists(p):268 return p == '.env'269 with patch('boss.core.fs.exists', side_effect=exists):270 resolve_dotenv_file('', stage)271 load_dotenv_m.assert_called_with('.env')272 msg = strip_ansi(info_m.call_args[0][0])273 assert msg == 'Resolving env file: .env'274def test_is_vault_enabled_returns_true():275 '''276 Test is_vault_enabled() returns True when it should.277 '''278 assert is_vault_enabled({'vault': {'enabled': True}}) is True279def test_is_vault_enabled_returns_false():280 '''281 Test is_vault_enabled() returns False when it should.282 '''283 assert is_vault_enabled({'vault': {'enabled': False}}) is False284def test_parse_config_returns_defaults_if_empty_config():285 ''' Test parse_config() returns defaults for empty config. '''286 result = parse_config('')287 assert result == DEFAULT_CONFIG288@patch('boss.core.vault.read_secrets')289def test_use_vault_if_enabled(read_secrets_mock):290 ''' Test use_vault_if_enabled() when vault enabled. '''291 config_str = '''292 user: ${TEST_USER}293 project_name: ${TEST_PROJECT}294 vault:295 enabled: true296 path: root/path297 silent: true298 '''299 os.environ['TEST_USER'] = 'test-user-from-host'300 read_secrets_mock.return_value = {301 'TEST_PROJECT': 'test-project-from-vault',302 }303 use_vault_if_enabled(config_str)304 read_secrets_mock.assert_called_with('root/path')305 # Configured options306 assert os.environ['TEST_USER'] == 'test-user-from-host'307 assert os.environ['TEST_PROJECT'] == 'test-project-from-vault'308 # Teardown309 os.environ['TEST_USER'] = ''310 os.environ['TEST_PROJECT'] = ''311@patch('boss.core.vault.read_secrets')312def test_use_vault_if_enabled_with_stage(read_secrets_mock):313 '''314 Test use_vault_if_enabled() when vault enabled315 with specific stage given.316 '''317 config_str = '''318 user: ${TEST_USER}319 project_name: ${TEST_PROJECT}320 vault:321 enabled: true322 path: root/path323 silent: true324 stages:325 stage1:326 vault:327 path: root/path/stage1328 stage2:329 test: test330 '''331 os.environ['TEST_USER'] = 'test-user-from-host'332 read_secrets_mock.return_value = {333 'TEST_PROJECT': 'test-project-from-vault',334 }335 # Test when invoked with stage=stage1336 # takes vault path for stage1337 use_vault_if_enabled(config_str, 'stage1')338 read_secrets_mock.assert_called_with('root/path/stage1')339 # Test when invoked with stage=stage2340 # takes the default vault path341 use_vault_if_enabled(config_str, 'stage2')342 read_secrets_mock.assert_called_with('root/path')343 # Env interpolation344 assert os.environ['TEST_USER'] == 'test-user-from-host'345 assert os.environ['TEST_PROJECT'] == 'test-project-from-vault'346 # Teardown347 os.environ['TEST_USER'] = ''348 os.environ['TEST_PROJECT'] = ''349@patch('boss.core.vault.read_secrets')350def test_use_vault_if_enabled_when_not_enabled(read_secrets_mock):351 '''352 Test use_vault_if_enabled() when vault is not enabled.353 It should just use the env vars from the host.354 '''355 config_str = '''356 user: ${TEST_USER}357 project_name: ${TEST_PROJECT}358 '''359 os.environ['TEST_USER'] = 'test-user-from-host'360 os.environ['TEST_PROJECT'] = ''361 read_secrets_mock.return_value = {362 'TEST_PROJECT': 'test-project-from-vault',363 }364 # Invoke with stage=stage1365 use_vault_if_enabled(config_str)366 read_secrets_mock.assert_not_called()367 # Env interpolation368 assert os.environ['TEST_USER'] == 'test-user-from-host'369 assert not os.environ['TEST_PROJECT']370 # Teardown...
Currency.py
Source:Currency.py
1# Currency.py module2test_test_var = 34563class Currency:4 5 def __init__(self, _id, _code, _nominal, _rate ):6 7 self.setId ( _id )8 self.setCode ( _code )9 self.setNominal( _nominal )10 self.setRate ( _rate )11# set get methods12 def setId( self, id ):13 self._id = id14 15 def getId(self, id ):16 return self._id17 18 def setCode( self, code ):19 self._code = code20 21 def getCode( self, code ):22 return self._code23 24 def setNominal( self, nominal ):25 self._nominal = nominal26 27 def getNominal( self, nominal ):28 return self._nominal29 30 def setRate( self, rate ):31 self._rate = rate32 33 def getRate( self, rate ):34 return self._rate35#### 36 37 def __str__( self ):38 return f"\n "\39 f"id: {self._id}\n"\40 f"Code: {self._code}\n"\41 f"Nominal: {self._nominal}\n"\42 f"Rate: {self._rate}"\43 44 def __repr__( self ):45 return self.__str__() ...
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!!