Defining context variables in python management

Sometimes you need to dynamically determine values ​​(for example, date-time, random strings, random integers, file contents, etc.) and use them at different stages without explicit or hard coding of the value.

So my question is how can I define the variables inside the steps (the right way to do this) in order to use these variables in the next steps.

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 that tries to explain why I would like to define variables in steps and store them in context, so I can use it in the next steps.

My idea was to modify the before_step and after_step ... methods to set the variable in context to store my custom variables as follows:

 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. 

Outline scripts, use something like these defining variables, such as "<some_identifier>" , and then for each example, replace the value in step.

This is mainly for reproducing behavior, but for any step, simple or using tables.

Is this right to do?

+7
source share
3 answers

To answer this question, it should be noted:

  • Is it necessary to control test data from the outside? For example, test data can be entered from the command line so that a value can be selected explicitly.

If the answer is no, then probably we should not worry about hard coding anything in the properties file. And we can leave the data generated in one step, save it in context and again access the next step.

An example that I can present is exactly the same as the question described. We don’t care about what random text content we created, published and checked? Probably no. Then we should not expose such details to the user (i.e. the Function File), since this is not important for the behavior that we are testing.

If so, then we need a little hack for this to happen. I experience such a case. I want to change the test data when I run the test, so I don’t need to hardcode them in function files, like in a table or script scheme. How can i do this?

I can use the -D option on the command line to transfer as much user data as possible, which can then be accessed in the context.config.userdata dictionary at any stage. If the amount of test data is very limited. This approach is an easy way. But if the test data set contains a lot of data that no one wants to enter one after another on the command line, it can be saved externally, for example, ini file with section names such as testdata_1 ... testdata_n, and thus the line can be passed from the command line that will be used to address the section name in this configuration file. And the test data can be read either before_all or before_scenario, etc., and use them at all stages.

0
source

In my experience, you cannot create a dynamic value in a properties file.
for example this step:
Given a random string of length "100" as "my_text"
I see no way to change {my_text} every time you run the script. (don't consider using behave -D to parse the value of context.config.userdata, I think this is also the wrong approach)
Even a script is a script; it actually breaks down into many scripts. each script will have a different value, but the value {my_text} is already defined in the example table for each script.

The method makes the step dynamic using the definition of the step (code layer).
You can create a random number in the @given step definition ("A random string of length" 100 "as" {my_text} "')
And use context.my_text to save the generated number and use its arround.

I also agree with Murphy Man that you do not need to explicitly expose an arbitrary number in the function file. You know which step this number will use, just use context.my_text in this step to get the value. It.

0
source

I think something like this:

@step (I start using the values ​​queue and save them in the context store with the key "{rabbit_mq_response}")

In this step, you can create a rabbit_mq connection, start using the queue, and then save the received JSON response in context.rabbit_mq_response

. What is the best way to save the received JSON from the rabbit and use it as a context variable?

0
source

All Articles