Sunday, 27 December 2020

Python behave optional argument

I am trying to write a test using cucumber, the 'given' statement needs to be able to take an optional parameter, it is for verifying the contents a string.

An example feature syntax for both scenarios would be:

Given Verify text "this is a test string"
When String validation is "success"
Then Store form contents.

Given Verify text "this is a test string" exclude "test,trial,hello"
When String validation is "success"
Then Store form contents.

The step I tried to implement was:

Verify text "this is a test string" exclude "test,trial,hello"

@given('Verify text "{text_to_clean}" {?: exclude "{excluded}')
def step_verify_text(context, text_to_clean, excluded):
    context.verified = verify_string(text_to_clean, excluded)

How do I do this please, I tried using the optional symbol of ?, but I can't figure it out. What do I need to do to use for optional parameters, please?

I am using a Mac using Catalina.

Using Chandan's solution, I still get an error:

Under features: testfeature.feature

Feature: Positive test of API.

    Scenario: Successful API call with no exclusion list
        Given Verify text "this is a test string"

    Scenario: Successful API call with an exclusion list
        Given Verify text "this is a test string" exclude "test,trial,hello"

Under features/steps: testcode.py

from behave import use_step_matcher, given, when, then
use_step_matcher("re")

@given('Verify text "(?P<text_to_clean>.*?)"(?: exclude )(?:"(?P<excluded>.*?)")')
def step_verify_text(context, text_to_clean, excluded):
    context.myvar = excluded


from Python behave optional argument

No comments:

Post a Comment