How to use lock_further_definitions method in hypothesis

Best Python code snippet using hypothesis

_settings.py

Source:_settings.py Github

copy

Full Screen

...153 all_settings[name] = Setting(154 name, description.strip(), default, options, deprecation)155 setattr(settings, name, SettingsProperty(name))156 @classmethod157 def lock_further_definitions(cls):158 settings.__definitions_are_locked = True159 def __setattr__(self, name, value):160 if name in settings._WHITELISTED_REAL_PROPERTIES:161 return object.__setattr__(self, name, value)162 elif name == 'database':163 if self._construction_complete:164 raise AttributeError(165 'Settings objects are immutable and may not be assigned to'166 ' after construction.'167 )168 else:169 return object.__setattr__(self, '_database', value)170 elif name in all_settings:171 if self._construction_complete:172 raise AttributeError(173 'Settings objects are immutable and may not be assigned to'174 ' after construction.'175 )176 else:177 setting = all_settings[name]178 if (179 setting.options is not None and180 value not in setting.options181 ):182 raise InvalidArgument(183 'Invalid %s, %r. Valid options: %r' % (184 name, value, setting.options185 )186 )187 return object.__setattr__(self, name, value)188 else:189 raise AttributeError('No such setting %s' % (name,))190 def __repr__(self):191 bits = []192 for name in all_settings:193 value = getattr(self, name)194 bits.append('%s=%r' % (name, value))195 bits.sort()196 return 'settings(%s)' % ', '.join(bits)197 @property198 def database(self):199 """An ExampleDatabase instance to use for storage of examples. May be200 None.201 If this was explicitly set at settings instantiation then that202 value will be used (even if it was None). If not and the203 database_file setting is not None this will be lazily loaded as204 an SQLite backed ExampleDatabase using that file the first time205 this property is accessed on a particular thread.206 """207 try:208 if self._database is not_set and self.database_file is not None:209 from hypothesis.database import ExampleDatabase210 from hypothesis.database.backend import SQLiteBackend211 if self.database_file not in _db_cache:212 _db_cache[self.database_file] = (213 ExampleDatabase(214 backend=SQLiteBackend(self.database_file)))215 return _db_cache[self.database_file]216 if self._database is not_set:217 self._database = None218 return self._database219 except AttributeError:220 import traceback221 traceback.print_exc()222 assert False223 def __enter__(self):224 default_context_manager = default_variable.with_value(self)225 self.defaults_stack().append(default_context_manager)226 default_context_manager.__enter__()227 return self228 def __exit__(self, *args, **kwargs):229 default_context_manager = self.defaults_stack().pop()230 return default_context_manager.__exit__(*args, **kwargs)231 @staticmethod232 def register_profile(name, settings):233 """registers a collection of values to be used as a settings profile.234 These settings can be loaded in by name. Enable different defaults for235 different settings.236 - settings is a settings object237 """238 settings._profiles[name] = settings239 @staticmethod240 def get_profile(name):241 """Return the profile with the given name.242 - name is a string representing the name of the profile243 to load244 A InvalidArgument exception will be thrown if the245 profile does not exist246 """247 try:248 return settings._profiles[name]249 except KeyError:250 raise InvalidArgument(251 "Profile '{0}' has not been registered".format(252 name253 )254 )255 @staticmethod256 def load_profile(name):257 """Loads in the settings defined in the profile provided If the profile258 does not exist an InvalidArgument will be thrown.259 Any setting not defined in the profile will be the library260 defined default for that setting261 """262 settings._assign_default_internal(settings.get_profile(name))263Setting = namedtuple(264 'Setting', (265 'name', 'description', 'default', 'options', 'deprecation'))266settings.define_setting(267 'min_satisfying_examples',268 default=5,269 description="""270Raise Unsatisfiable for any tests which do not produce at least this many271values that pass all assume() calls and which have not exhaustively covered the272search space.273"""274)275settings.define_setting(276 'max_examples',277 default=200,278 description="""279Once this many satisfying examples have been considered without finding any280counter-example, falsification will terminate.281"""282)283settings.define_setting(284 'max_iterations',285 default=1000,286 description="""287Once this many iterations of the example loop have run, including ones which288failed to satisfy assumptions and ones which produced duplicates, falsification289will terminate.290"""291)292settings.define_setting(293 'max_shrinks',294 default=500,295 description="""296Once this many successful shrinks have been performed, Hypothesis will assume297something has gone a bit wrong and give up rather than continuing to try to298shrink the example.299"""300)301settings.define_setting(302 'timeout',303 default=60,304 description="""305Once this many seconds have passed, falsify will terminate even306if it has not found many examples. This is a soft rather than a hard307limit - Hypothesis won't e.g. interrupt execution of the called308function to stop it. If this value is <= 0 then no timeout will be309applied.310"""311)312settings.define_setting(313 'derandomize',314 default=False,315 description="""316If this is True then hypothesis will run in deterministic mode317where each falsification uses a random number generator that is seeded318based on the hypothesis to falsify, which will be consistent across319multiple runs. This has the advantage that it will eliminate any320randomness from your tests, which may be preferable for some situations321. It does have the disadvantage of making your tests less likely to322find novel breakages.323"""324)325settings.define_setting(326 'strict',327 default=os.getenv('HYPOTHESIS_STRICT_MODE') == 'true',328 description="""329If set to True, anything that would cause Hypothesis to issue a warning will330instead raise an error. Note that new warnings may be added at any time, so331running with strict set to True means that new Hypothesis releases may validly332break your code.333You can enable this setting temporarily by setting the HYPOTHESIS_STRICT_MODE334environment variable to the string 'true'.335"""336)337settings.define_setting(338 'database_file',339 default=lambda: (340 os.getenv('HYPOTHESIS_DATABASE_FILE') or341 os.path.join(hypothesis_home_dir(), 'examples.db')342 ),343 description="""344 database: An instance of hypothesis.database.ExampleDatabase that will be345used to save examples to and load previous examples from. May be None346in which case no storage will be used.347"""348)349class Verbosity(object):350 def __repr__(self):351 return 'Verbosity.%s' % (self.name,)352 def __init__(self, name, level):353 self.name = name354 self.level = level355 def __eq__(self, other):356 return isinstance(other, Verbosity) and (357 self.level == other.level358 )359 def __ne__(self, other):360 return not self.__eq__(other)361 def __hash__(self):362 return self.level363 def __lt__(self, other):364 return self.level < other.level365 def __le__(self, other):366 return self.level <= other.level367 def __gt__(self, other):368 return self.level > other.level369 def __ge__(self, other):370 return self.level >= other.level371 @classmethod372 def by_name(cls, key):373 result = getattr(cls, key, None)374 if isinstance(result, Verbosity):375 return result376 raise InvalidArgument('No such verbosity level %r' % (key,))377Verbosity.quiet = Verbosity('quiet', 0)378Verbosity.normal = Verbosity('normal', 1)379Verbosity.verbose = Verbosity('verbose', 2)380Verbosity.debug = Verbosity('debug', 3)381Verbosity.all = [382 Verbosity.quiet, Verbosity.normal, Verbosity.verbose, Verbosity.debug383]384ENVIRONMENT_VERBOSITY_OVERRIDE = os.getenv('HYPOTHESIS_VERBOSITY_LEVEL')385if ENVIRONMENT_VERBOSITY_OVERRIDE:386 DEFAULT_VERBOSITY = Verbosity.by_name(ENVIRONMENT_VERBOSITY_OVERRIDE)387else:388 DEFAULT_VERBOSITY = Verbosity.normal389settings.define_setting(390 'verbosity',391 options=Verbosity.all,392 default=DEFAULT_VERBOSITY,393 description='Control the verbosity level of Hypothesis messages',394)395settings.define_setting(396 name='stateful_step_count',397 default=50,398 description="""399Number of steps to run a stateful program for before giving up on it breaking.400"""401)402settings.define_setting(403 'perform_health_check',404 default=True,405 description=u"""406If set to True, Hypothesis will run a preliminary health check before407attempting to actually execute your test.408"""409)410settings.lock_further_definitions()411settings.register_profile('default', settings())412settings.load_profile('default')413assert settings.default is not None414def note_deprecation(message, s=None):415 # If *either* self or the current default are non-strict416 # then this should be an error. This is to handle e.g. the case417 # where defining a new setting while non-strict updates a418 # profile which is strict. This should not be an error, but419 # using the profile here would cause it to be one.420 if s is None:421 s = settings.default422 assert s is not None423 strict = settings.default.strict and s.strict424 verbosity = s.verbosity...

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 hypothesis 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