Best Python code snippet using tavern
test_stacks.py
Source:test_stacks.py
...14from testscenarios import scenarios as scnrs15import testtools16from heatclient.v1 import stacks17load_tests = testscenarios.load_tests_apply_scenarios18def mock_stack(manager, stack_name, stack_id):19 return stacks.Stack(manager, {20 "id": stack_id,21 "stack_name": stack_name,22 "links": [{23 "href": "http://192.0.2.1:8004/v1/1234/stacks/%s/%s" % (24 stack_name, stack_id),25 "rel": "self"}],26 "description": "No description",27 "stack_status_reason": "Stack create completed successfully",28 "creation_time": "2013-08-04T20:57:55Z",29 "updated_time": "2013-08-04T20:57:55Z",30 "stack_status": "CREATE_COMPLETE"31 })32class StackStatusActionTest(testtools.TestCase):33 scenarios = scnrs.multiply_scenarios([34 ('CREATE', dict(action='CREATE')),35 ('DELETE', dict(action='DELETE')),36 ('UPDATE', dict(action='UPDATE')),37 ('ROLLBACK', dict(action='ROLLBACK')),38 ('SUSPEND', dict(action='SUSPEND')),39 ('RESUME', dict(action='RESUME')),40 ('CHECK', dict(action='CHECK'))41 ], [42 ('IN_PROGRESS', dict(status='IN_PROGRESS')),43 ('FAILED', dict(status='FAILED')),44 ('COMPLETE', dict(status='COMPLETE'))45 ])46 def test_status_action(self):47 stack_status = '%s_%s' % (self.action, self.status)48 stack = mock_stack(None, 'stack_1', 'abcd1234')49 stack.stack_status = stack_status50 self.assertEqual(self.action, stack.action)51 self.assertEqual(self.status, stack.status)52class StackIdentifierTest(testtools.TestCase):53 def test_stack_identifier(self):54 stack = mock_stack(None, 'the_stack', 'abcd1234')55 self.assertEqual('the_stack/abcd1234', stack.identifier)56class StackOperationsTest(testtools.TestCase):57 def test_delete_stack(self):58 manager = mock.MagicMock()59 stack = mock_stack(manager, 'the_stack', 'abcd1234')60 stack.delete()61 manager.delete.assert_called_once_with('the_stack/abcd1234')62 def test_abandon_stack(self):63 manager = mock.MagicMock()64 stack = mock_stack(manager, 'the_stack', 'abcd1234')65 stack.abandon()66 manager.abandon.assert_called_once_with('the_stack/abcd1234')67 def test_get_stack(self):68 manager = mock.MagicMock()69 stack = mock_stack(manager, 'the_stack', 'abcd1234')70 stack.get()71 manager.get.assert_called_once_with('the_stack/abcd1234')72 def test_update_stack(self):73 manager = mock.MagicMock()74 stack = mock_stack(manager, 'the_stack', 'abcd1234')75 stack.update()76 manager.update.assert_called_once_with('the_stack/abcd1234')77 def test_create_stack(self):78 manager = mock.MagicMock()79 stack = mock_stack(manager, 'the_stack', 'abcd1234')80 stack = stack.create()81 manager.create.assert_called_once_with('the_stack/abcd1234')82 def test_preview_stack(self):83 manager = mock.MagicMock()84 stack = mock_stack(manager, 'the_stack', 'abcd1234')85 stack = stack.preview()86 manager.preview.assert_called_once_with()87 def test_snapshot(self):88 manager = mock.MagicMock()89 stack = mock_stack(manager, 'the_stack', 'abcd1234')90 stack.snapshot('foo')91 manager.snapshot.assert_called_once_with('the_stack/abcd1234', 'foo')92 def test_snapshot_show(self):93 manager = mock.MagicMock()94 stack = mock_stack(manager, 'the_stack', 'abcd1234')95 stack.snapshot_show('snap1234')96 manager.snapshot_show.assert_called_once_with(97 'the_stack/abcd1234', 'snap1234')98 def test_snapshot_delete(self):99 manager = mock.MagicMock()100 stack = mock_stack(manager, 'the_stack', 'abcd1234')101 stack.snapshot_delete('snap1234')102 manager.snapshot_delete.assert_called_once_with(103 'the_stack/abcd1234', 'snap1234')104 def test_restore(self):105 manager = mock.MagicMock()106 stack = mock_stack(manager, 'the_stack', 'abcd1234')107 stack.restore('snap1234')108 manager.restore.assert_called_once_with(109 'the_stack/abcd1234', 'snap1234')110 def test_snapshot_list(self):111 manager = mock.MagicMock()112 stack = mock_stack(manager, 'the_stack', 'abcd1234')113 stack.snapshot_list()114 manager.snapshot_list.assert_called_once_with('the_stack/abcd1234')115 def test_output_list(self):116 manager = mock.MagicMock()117 stack = mock_stack(manager, 'the_stack', 'abcd1234')118 stack.output_list()119 manager.output_list.assert_called_once_with('the_stack/abcd1234')120 def test_output_show(self):121 manager = mock.MagicMock()122 stack = mock_stack(manager, 'the_stack', 'abcd1234')123 stack.output_show('out123')124 manager.output_show.assert_called_once_with('the_stack/abcd1234',125 'out123')126 def test_environment_show(self):127 manager = mock.MagicMock()128 stack = mock_stack(manager, 'env_stack', 'env1')129 stack.environment()130 manager.environment.assert_called_once_with('env_stack/env1')131 def test_files_show(self):132 manager = mock.MagicMock()133 stack = mock_stack(manager, 'files_stack', 'files1')134 stack.files()135 manager.files.assert_called_once_with('files_stack/files1')136class StackManagerNoPaginationTest(testtools.TestCase):137 scenarios = [138 ('total_0', dict(total=0)),139 ('total_1', dict(total=1)),140 ('total_9', dict(total=9)),141 ('total_10', dict(total=10)),142 ('total_11', dict(total=11)),143 ('total_19', dict(total=19)),144 ('total_20', dict(total=20)),145 ('total_21', dict(total=21)),146 ('total_49', dict(total=49)),147 ('total_50', dict(total=50)),148 ('total_51', dict(total=51)),149 ('total_95', dict(total=95)),150 ]151 # absolute limit for results returned152 limit = 50153 def mock_manager(self):154 manager = stacks.StackManager(None)155 manager._list = mock.MagicMock()156 def mock_list(*args, **kwargs):157 def results():158 for i in range(0, self.total):159 stack_name = 'stack_%s' % (i + 1)160 stack_id = 'abcd1234-%s' % (i + 1)161 yield mock_stack(manager, stack_name, stack_id)162 return list(results())163 manager._list.side_effect = mock_list164 return manager165 def test_stack_list_no_pagination(self):166 manager = self.mock_manager()167 results = list(manager.list())168 manager._list.assert_called_once_with(169 '/stacks?', 'stacks')170 # paginate is not specified, so the total171 # results is always returned172 self.assertEqual(self.total, len(results))173 if self.total > 0:174 self.assertEqual('stack_1', results[0].stack_name)175 self.assertEqual('stack_%s' % self.total, results[-1].stack_name)176class StackManagerPaginationTest(testtools.TestCase):177 scenarios = [178 ('0_offset_0', dict(179 offset=0,180 total=0,181 results=((0, 0),)182 )),183 ('1_offset_0', dict(184 offset=0,185 total=1,186 results=((0, 1),)187 )),188 ('9_offset_0', dict(189 offset=0,190 total=9,191 results=((0, 9),)192 )),193 ('10_offset_0', dict(194 offset=0,195 total=10,196 results=((0, 10), (10, 10))197 )),198 ('11_offset_0', dict(199 offset=0,200 total=11,201 results=((0, 10), (10, 11))202 )),203 ('11_offset_10', dict(204 offset=10,205 total=11,206 results=((10, 11),)207 )),208 ('19_offset_10', dict(209 offset=10,210 total=19,211 results=((10, 19),)212 )),213 ('20_offset_10', dict(214 offset=10,215 total=20,216 results=((10, 20), (20, 20))217 )),218 ('21_offset_10', dict(219 offset=10,220 total=21,221 results=((10, 20), (20, 21))222 )),223 ('21_offset_0', dict(224 offset=0,225 total=21,226 results=((0, 10), (10, 20), (20, 21))227 )),228 ('21_offset_20', dict(229 offset=20,230 total=21,231 results=((20, 21),)232 )),233 ('95_offset_90', dict(234 offset=90,235 total=95,236 results=((90, 95),)237 )),238 ]239 # absolute limit for results returned240 limit = 50241 def mock_manager(self):242 manager = stacks.StackManager(None)243 manager._list = mock.MagicMock()244 def mock_list(arg_url, arg_response_key):245 try:246 result = self.results[self.result_index]247 except IndexError:248 return []249 self.result_index = self.result_index + 1250 limit_string = 'limit=%s' % self.limit251 self.assertIn(limit_string, arg_url)252 offset = result[0]253 if offset > 0:254 offset_string = 'marker=abcd1234-%s' % offset255 self.assertIn(offset_string, arg_url)256 def results():257 for i in range(*result):258 self.limit -= 1259 stack_name = 'stack_%s' % (i + 1)260 stack_id = 'abcd1234-%s' % (i + 1)261 yield mock_stack(manager, stack_name, stack_id)262 return list(results())263 manager._list.side_effect = mock_list264 return manager265 def test_stack_list_pagination(self):266 manager = self.mock_manager()267 list_params = {'limit': self.limit}268 if self.offset > 0:269 marker = 'abcd1234-%s' % self.offset270 list_params['marker'] = marker271 self.result_index = 0272 results = list(manager.list(**list_params))273 # assert that the list method has been called enough times274 self.assertEqual(len(self.results), self.result_index)275 last_result = min(self.limit, self.total - self.offset)...
test_stack.py
Source:test_stack.py
...3class Mock_Stack(Stack_Mixin):4 def __init__(self):5 self.stack = []6@pytest.fixture7def mock_stack():8 return Mock_Stack()9def test_OP_IFDUP(mock_stack):10 mock_stack.stack.append(1)11 mock_stack.OP_IFDUP()12 assert len(mock_stack.stack) is 213 assert mock_stack.stack[-1] is 114def test_OP_DEPTH(mock_stack):15 mock_stack.OP_DEPTH()16 assert len(mock_stack.stack) is 117 assert mock_stack.stack[0] is 018def test_OP_DROP(mock_stack):19 mock_stack.stack.append(1)20 mock_stack.OP_DROP()21 assert len(mock_stack.stack) is 0...
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!!