How to use test_constant method in locust

Best Python code snippet using locust

app_constants_test.py

Source:app_constants_test.py Github

copy

Full Screen

1# Copyright 2018 Google Inc. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS-IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tests for deployments.lib.config."""15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18from absl import flags19from absl.testing import flagsaver20from absl.testing import parameterized21import mock22from absl.testing import absltest23from loaner.deployments.lib import app_constants24from loaner.deployments.lib import utils25FLAGS = flags.FLAGS26flags.DEFINE_string('app_constants_test_flag', '', 'Give me a value!')27FLAGS.register_key_flag_for_module(__name__, FLAGS['app_constants_test_flag'])28class AppConstantsTest(parameterized.TestCase, absltest.TestCase):29 def test_get_default_constants(self):30 constants = app_constants.get_default_constants()31 self.assertLen(constants, 7)32 @flagsaver.flagsaver(app_constants_test_flag='valid')33 def test_get_constants_from_flags(self):34 constants = app_constants.get_constants_from_flags(module=__name__)35 self.assertLen(constants, 1)36 self.assertEqual('valid', constants['app_constants_test_flag'].value)37 def test_get_constants_from_flags__not_parsed(self):38 FLAGS.__dict__['__flags_parsed'] = False39 constants = app_constants.get_constants_from_flags(module=__name__)40 FLAGS.__dict__['__flags_parsed'] = True41 self.assertLen(constants, 1)42 self.assertEqual('', constants['app_constants_test_flag'].value)43 @parameterized.parameters(44 ('DEFAULT', None, 'new', 'new'),45 ('', utils.StringParser(True), '', ''),46 ('yes', utils.YesNoParser(), 'no', False),47 ('', utils.ListParser(True), 'this,one', ['this', 'one']),48 ('', utils.StringParser(False), 'asdf', 'asdf'),49 ('1', flags.IntegerParser(), '10', 10),50 ('user@example.com', utils.EmailParser(), 'other@e.co', 'other@e.co'),51 )52 def test_constant_constructor(self, default, parser, new_value, expected):53 test_constant = app_constants.Constant('name', 'message', default, parser)54 self.assertEqual('name: {}'.format(default), str(test_constant))55 self.assertEqual(56 "<Constant('name', 'message', {!r}, {!r})>".format(default, parser),57 repr(test_constant))58 self.assertEqual(59 app_constants.Constant('name', 'message', default, parser),60 test_constant)61 self.assertNotEqual(62 app_constants.Constant('other', 'message', default, parser),63 test_constant)64 test_constant.value = new_value65 self.assertTrue(test_constant.valid)66 self.assertEqual(test_constant.value, expected)67 def test_constant_prompt(self):68 test_constant = app_constants.Constant(69 'name', 'messsage', '', utils.StringParser(False))70 self.assertFalse(test_constant.valid)71 with mock.patch.object(utils, 'prompt', return_value='VALID!'):72 test_constant.prompt()73 self.assertTrue(test_constant.valid)74if __name__ == '__main__':...

Full Screen

Full Screen

weak_constant_test.py

Source:weak_constant_test.py Github

copy

Full Screen

1#!/usr/bin/env python2# Copyright 2018 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5# pylint: disable=protected-access6import time7import unittest8from devil import devil_env9from devil.utils import lazy10from devil.utils import timeout_retry11with devil_env.SysPath(devil_env.PYMOCK_PATH):12 import mock13class DynamicSideEffect(object):14 """A helper object for handling a sequence of single-use side effects."""15 def __init__(self, side_effects):16 self._side_effects = iter(side_effects or [])17 def __call__(self):18 val = next(self._side_effects)()19 if isinstance(val, Exception):20 raise val21 return val22class WeakConstantTest(unittest.TestCase):23 def testUninitialized(self):24 """Ensure that the first read calls the initializer."""25 initializer = mock.Mock(return_value='initializer called')26 test_constant = lazy.WeakConstant(initializer)27 self.assertEquals('initializer called', test_constant.read())28 initializer.assert_called_once_with()29 def testInitialized(self):30 """Ensure that reading doesn't reinitialize the value."""31 initializer = mock.Mock(return_value='initializer called')32 test_constant = lazy.WeakConstant(initializer)33 test_constant._initialized.set()34 test_constant._val = 'initializer not called'35 self.assertEquals('initializer not called', test_constant.read())36 self.assertFalse(initializer.mock_calls) # assert not called37 def testFirstCallHangs(self):38 """Ensure that reading works even if the first initializer call hangs."""39 dyn = DynamicSideEffect(40 [lambda: time.sleep(10), lambda: 'second try worked!'])41 initializer = mock.Mock(side_effect=dyn)42 test_constant = lazy.WeakConstant(initializer)43 self.assertEquals('second try worked!',44 timeout_retry.Run(test_constant.read, 1, 1))45 initializer.assert_has_calls([mock.call(), mock.call()])46if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful