0

Hi so Im new to behave + python. I need some simple explanation of what context is and how it's working for my project. Where and when need to by passing. Like sometime Im passing only context and in some cases self.context :(

I read the documentation of behave which is not incomprehensible for me. I know that context is a "bag" where we putting every thing but where I should use this? And how to accecc this data 'bag'. I need some live examples. thanks

    def __init__(self, context):
        self.context = context
        self.driver = self.context.driver

or

    def step_impl(context):
        context.announcement_page = context.personal_page.offer_type()

Cubimon
  • 150
  • 3
  • 11
ranger
  • 637
  • 2
  • 9
  • 18
  • 1
    I think it could be helpful to find a resource for learning Python fundamentals like attributes and scope, since I think there may be a key misunderstanding going on here. The best I can explain it is that `self` and `context` are separate variables, and `self.context` is simply an "attribute" of `self`. The `self` variable is special because it refers to the object itself (so it survives after the function call), while `context` is created at the start of the function call and lost after it exits. – StardustGogeta Jul 23 '19 at 13:16
  • Behave has some [tutorials](https://behave.readthedocs.io/en/latest/tutorial.html) that show how to use the context. It is just like in your example a container where you can put your stuff in and use it later. Like @StardustGogeta said, it is as simple as a basic python member variable – Cubimon Jul 23 '19 at 13:38
  • yes I saw that doc. but there is not much examples on how to use context – ranger Jul 25 '19 at 08:43

2 Answers2

1

In Python Behave, the context object is just like any other object in Python. You can modify it on the fly by adding whatever attribute you want to it. For example, in the context of Python Behave,

def step_impl(context):
    print(context.name) # Throws error since the 'name' attribute is undefined

    context.name = 'ranger'
    print(context.name) # Prints "ranger"

The importance of the context object for Python Behave is how the Python Behave framework sets and cleans it up between each operation (e.g. before_all, before_scenario, etc.). In Python Behave, you have the power of doing something like this,

def before_scenario(context):
    context.myValueExists = true

def step_impl(context):
    print(context.myValueExists) # Prints "true"

The power here is that you can configure all the needed data for your test step before the test step runs, and also ensure that any artifacts or side-effects of your test run can be addressed, cleaned up, or analyzed.

For a quick explanation of Python objects, see here.

For more information on Python Behave, see the documentation here.

natn2323
  • 1,983
  • 1
  • 13
  • 30
  • Ok its clear now But I have another question. When I have 30 different buttons/sliders/inputs. How to test this I mean how to handle many steps in behave? Should I pass e.g. Given, When and And 1cond. And 2cond. And 3con. And 4cond. And 5cond ... and assertions for that steps? 1 Then 2Then 3 Then 4Then 5Then? – ranger Aug 30 '19 at 09:47
  • @ranger That's an entirely different question that has less to do with Behave and more to do with the architecture of your testing framework. Try asking it as a separate SO question and I'll answer it there. And if I've answered your current question, please feel free to mark it as the accepted answer – natn2323 Aug 30 '19 at 18:04
0

natn2323 already provided a good explanation.

SEE ALSO: behave docs: Appendix -- Context Attributes (for details and lifecycle, cleanup semantics)

jenisys
  • 704
  • 5
  • 6