Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define context variables in behave python

Sometimes, you need to define values dynamically, (like datetime now, random strings, random integers, file contents, etc.) and use them across different steps without being explicit or hard-coding the value.

So, my question is how could I define variables inside of steps (the correct way to do it) to use these variables in the following steps.

Some example

Given A random string of length "100" as "my_text"
And I log in to my platform
And I ask to add the following post:
 | title                    | description |
 | Some example of title    | {{my_text}} |
When I submit the post form
Then The posts table shows these posts:
 | title                    | description |
 | Some example of title    | {{my_text}} |
And I delete any post containing in the description "{{my_text}}"

This is a basic example trying to explain why I would like to define variables in steps and save them in the context to use it in the following steps.

My idea was to modify before_step and after_step methods... to set a variable in context to store my custom variables like this:

def before_step(context):
    if not hasattr(context, 'vars'):
       context.vars = {}

    if hasattr(context, table) and context.table:
       parse_table(context)

def parse_table(context):
    # Here use a regex to check each cell and look for `"{{<identifier>}}"` and if match, replace the cell value by context.vars[identifier] so the step "the posts table shows these posts will never know what is `{{my_text}}` it will be abstract seeing the random string.

Scenarios Outline, use something like this defining variables like "<some_identifier>" and then for each example replace the value in the step.

It's basically to reproduce the behaviour but for any kind of step, simple or using tables.

Is it the right way to do something like this?

like image 395
Federico Castro Avatar asked Apr 07 '15 19:04

Federico Castro


People also ask

What is context in behave Python?

Context is a very important feature in Python Behave where the user and Behave can store information to share around. It holds the contextual information during the execution of tests. It is an object that can store user-defined data along with Python Behave-defined data, in context attributes.

What are context variables?

Context variable: A variable which can be set either at compile time or runtime. It can be changed and allows variables which would otherwise be hardcoded to be more dynamic. Context: The environment or category of the value held by the context variable. Most of the time Contexts are DEV, TEST, PROD, UAT, etc.

How do you use tags in behave in Python?

Tags are placed before a Scenario or a Feature that we want to tag. We can also have multiple tags which are separated by spaces within a line. A tag begins with @ and is followed by the tag name. Tags help to manage the test execution by excluding/including the specific scenarios or features depending on the tag.

How do you run a specific scenario in behave?

You can run a feature file by using -i or --include flags and then the name of the feature file. For more information check the documentation for command line arguments. There's a lot of useful information hidden in their appendix section. NOTE: At the time I'm writing this it won't work with Python 3.6 and Behave 1.2.


1 Answers

From Behave docs on the context:

When behave launches into a new feature or scenario it adds a new layer to the context, allowing the new activity level to add new values, or overwrite ones previously defined, for the duration of that activity. These can be thought of as scopes:

@given('I request a new widget for an account via SOAP')
def step_impl(context):
    client = Client("http://127.0.0.1:8000/soap/")
    // method client.Allocate(...) returns a dict
    context.response = client.Allocate(customer_first='Firstname',
        customer_last='Lastname', colour='red')
    // context vars can be set more directly
    context.new_var = "My new variable!"

@then('I should receive an OK SOAP response')
def step_impl(context):
    eq_(context.response['ok'], 1)
    cnv = str(context.new_var)
    print (f"This is my new variable:'{cnv}'"

So, the value can be set using dot notation and retrieved the same.

like image 84
ingyhere Avatar answered Oct 16 '22 16:10

ingyhere