Skip to content

Instantly share code, notes, and snippets.

@guewen
Last active May 21, 2020 13:16
Show Gist options
  • Save guewen/7579757 to your computer and use it in GitHub Desktop.
Save guewen/7579757 to your computer and use it in GitHub Desktop.
Use an optional argument in a behave phrase.
from behave.matchers import register_type
def parse_optional(text):
return text.strip()
# https://pypi.python.org/pypi/parse#custom-type-conversions
parse_optional.pattern = r'\s?\w*\s?'
register_type(optional=parse_optional)
@step(u'I find a{n:optional}{active:optional} "{model_name}" with {domain}')
def impl(ctx, n, active, model_name, domain):
assert active in ('', 'inactive', 'active', 'possibly inactive')
# phrases
# Given I find a "res.users" with login: test
# Given I find an inactive "res.users" with login: test
# Given I find an active "res.users" with login: test
# Given I find a possibly inactive "res.users" with login: test
@guewen
Copy link
Author

guewen commented May 12, 2020

Also note that behave's may have changed since I wrote this gist.

@huyenuet
Copy link

I switch domain2_browser to 1st position, and it works!
e.g.
def impl(ctx, domain2_browser, state):

@saikiranmankena
Copy link

@huyenuet I guess you are using pycharm. One issue I am facing when using optional part is steps are passing but I am not able to navigate to the steps as they are showing as undefined. Are you facing this issue ?

@hafsanaeem-aai
Copy link

@guewen yeah i was referring to variable parameter and Thanks @huyenuet for the answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment