Best Python code snippet using tempest_python
test_base.py
Source:test_base.py
...14from ospurge import exceptions15from ospurge.resources import base16from ospurge.tests import mock17from ospurge.tests import unittest18def generate_timeout_series(timeout):19 """Generate a series of times that exceeds the given timeout.20 Yields a series of fake time.time() floating point numbers21 such that the difference between each pair in the series just22 exceeds the timeout value that is passed in. Useful for23 mocking time.time() in methods that otherwise wait for timeout24 seconds.25 """26 iteration = 027 while True:28 iteration += 129 yield (iteration * timeout) + iteration30class SignatureMismatch(Exception):31 pass32class WrongMethodDefOrder(Exception):33 pass34@mock.patch('logging.warning', mock.Mock(side_effect=SignatureMismatch))35class TestMatchSignaturesMeta(unittest.TestCase):36 class Test(six.with_metaclass(base.MatchSignaturesMeta)):37 def a(self, arg1):38 pass39 def b(self, arg1=True):40 pass41 def c(self, arg1, arg2):42 pass43 def _private(self):44 pass45 def test_nominal(self):46 class Foo1(self.Test):47 def a(self, arg1):48 pass49 class Foo2(self.Test):50 def b(self, arg1=True):51 pass52 class Foo3(self.Test):53 def c(self, arg1, arg2):54 pass55 class Foo4(self.Test):56 def _startswith_underscore(self, arg1, arg2):57 pass58 class Foo5(self.Test):59 def new_method(self):60 pass61 def test_method_arg1_has_different_name(self):62 with self.assertRaises(SignatureMismatch):63 class Foo(self.Test):64 def a(self, other_name):65 pass66 def test_method_arg1_has_different_value(self):67 with self.assertRaises(SignatureMismatch):68 class Foo(self.Test):69 def b(self, arg1=False):70 pass71 def test_method_has_different_number_of_args(self):72 with self.assertRaises(SignatureMismatch):73 class Foo(self.Test):74 def c(self, arg1, arg2, arg3):75 pass76# OrderedMeta requires Python 377if six.PY3:78 @mock.patch('logging.warning', mock.Mock(side_effect=WrongMethodDefOrder))79 class TestOrderedMeta(unittest.TestCase):80 class Test(base.OrderedMeta):81 ordered_methods = ['a', 'b']82 def test_nominal(self):83 class Foo1(six.with_metaclass(self.Test)):84 def a(self):85 pass86 class Foo2(six.with_metaclass(self.Test)):87 def b(self):88 pass89 class Foo3(six.with_metaclass(self.Test)):90 def a(self):91 pass92 def b(self):93 pass94 class Foo4(six.with_metaclass(self.Test)):95 def a(self):96 pass97 def other(self):98 pass99 def b(self):100 pass101 def test_wrong_order(self):102 with self.assertRaises(WrongMethodDefOrder):103 class Foo(six.with_metaclass(self.Test)):104 def b(self):105 pass106 def a(self):107 pass108class TestServiceResource(unittest.TestCase):109 def test_init_without_order_attr(self):110 class Foo5(base.ServiceResource):111 def list(self):112 pass113 def delete(self, resource):114 pass115 @staticmethod116 def to_str(resource):117 pass118 self.assertRaisesRegex(ValueError, 'Class .*ORDER.*',119 Foo5, mock.Mock())120 def test_instantiate_without_concrete_methods(self):121 class Foo6(base.ServiceResource):122 ORDER = 1123 self.assertRaises(TypeError, Foo6)124 @mock.patch.multiple(base.ServiceResource, ORDER=12,125 __abstractmethods__=set())126 def test_instantiate_nominal(self):127 creds_manager = mock.Mock()128 resource_manager = base.ServiceResource(creds_manager)129 self.assertEqual(resource_manager.cloud, creds_manager.cloud)130 self.assertEqual(resource_manager.options, creds_manager.options)131 self.assertEqual(resource_manager.cleanup_project_id,132 creds_manager.project_id)133 self.assertEqual(12, resource_manager.order())134 self.assertEqual(True, resource_manager.check_prerequisite())135 self.assertRaises(NotImplementedError, resource_manager.delete, '')136 self.assertRaises(NotImplementedError, resource_manager.to_str, '')137 self.assertRaises(NotImplementedError, resource_manager.list)138 @mock.patch.multiple(base.ServiceResource, ORDER=12,139 __abstractmethods__=set())140 def test_should_delete(self):141 creds_manager = mock.Mock()142 resource_manager = base.ServiceResource(creds_manager)143 resource = mock.Mock(get=mock.Mock(side_effect=[None, None]))144 self.assertEqual(True, resource_manager.should_delete(resource))145 resource.get.call_args = [mock.call('project_id'),146 mock.call('tenant_id')]147 resource.get.side_effect = ["Foo", "Bar"]148 self.assertEqual(False, resource_manager.should_delete(resource))149 resource.get.side_effect = [42, resource_manager.cleanup_project_id]150 self.assertEqual(True, resource_manager.should_delete(resource))151 @mock.patch('time.sleep', autospec=True)152 @mock.patch.multiple(base.ServiceResource, ORDER=12,153 __abstractmethods__=set())154 @mock.patch.object(base.ServiceResource, 'check_prerequisite',155 return_value=False)156 def test_wait_for_check_prerequisite_runtimeerror(157 self, mock_check_prerequisite, mock_sleep):158 resource_manager = base.ServiceResource(mock.Mock())159 mock_exit = mock.Mock(is_set=mock.Mock(return_value=False))160 with mock.patch('time.time') as mock_time:161 mock_time.side_effect = generate_timeout_series(30)162 self.assertRaisesRegex(163 exceptions.TimeoutError, "^Timeout exceeded .*",164 resource_manager.wait_for_check_prerequisite, mock_exit165 )166 self.assertEqual(mock_check_prerequisite.call_args_list,167 [mock.call()] * (120 // 30 - 1))168 self.assertEqual(mock_sleep.call_args_list,169 [mock.call(i) for i in (2, 4, 8)])170 mock_sleep.reset_mock()171 mock_check_prerequisite.reset_mock()172 mock_exit.is_set.return_value = True173 self.assertRaisesRegex(174 RuntimeError, ".* exited because it was interrupted .*",175 resource_manager.wait_for_check_prerequisite, mock_exit...
utils.py
Source:utils.py
...11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14#15def generate_timeout_series(timeout):16 """Generate a series of times that exceeds the given timeout.17 Yields a series of fake time.time() floating point numbers18 such that the difference between each pair in the series just19 exceeds the timeout value that is passed in. Useful for20 mocking time.time() in methods that otherwise wait for timeout21 seconds.22 """23 iteration = 024 while True:25 iteration += 1...
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!!