-
Hi, Is there a way to raise a warning when trying to overwrite a certain attribute of context? For example: # environment.py
-----------------
@fixture
def fixture_test(ctx):
ctx.locked_attribute = SomeImportantClass()
yield ctx.locked_attribute
ctx.locked_attribute.clear()
# steps.py
-----------------
@step("This step shall do something")
def do_something(ctx):
# do something
ctx.locked_attribute = "foo" # I would like to raise here a warning
ctx.locked_attribute.method_of_SomeImportantClass() # error is raised by python
Is this possible? For example, is there a way to raise a [1] "If an attempt made by user code to overwrite one of these variables, or indeed by behave to overwite a user-set variable, then a behave.runner.ContextMaskWarning warning will be raised." |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Currently, you will need to provide such a special NOTES: EXAMPLE: from behave import given, when, then, step
from behave4me.step_util import use_or_create_context_param
@step(„This step shall do something“)
def do_something(ctx):
# — NOTE: ctx.locked_attribute is only created/assigned if no such named attribute exists.
locked_attribute = use_or_create_context_param(ctx, „locked_attribute“, SomeImportantClass)
locked_attribute.method_of_SomeImportantClass() SEE ALSO:
|
Beta Was this translation helpful? Give feedback.
-
SEE ALSO: |
Beta Was this translation helpful? Give feedback.
behave
does not protectuser
context variables. As you have stated, it raises only such a warning if you try to override/hide abehave
context variable.Currently, you will need to provide such a special
ValueObject
class, like aConstant
object that protects itself against assignment (and raises aContextMaskWarning
if an assignment occurs).NOTES:
Normally, you need a
use_or_create_context_param(ctx, name, some_factory_func, *args, **kwargs)
helper function that provides/stores a context variable is none exists.EXAMPLE: