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
@hafsanaeem-aai
Copy link

hafsanaeem-aai commented May 6, 2020

Do you have a example of passing optional variable parameter to step for example in this case 'model_name' ?

@huyenuet
Copy link

I tried with an optional param that has 3 words, and it didn't work.
My scenario is

    When I should see something on domain2 browser
    When I should see something

step:

def parse_optional(text):
    return text.strip()

parse_optional.pattern = r'\son domain2 browser'
register_type(optional=parse_optional)

@when('I {state} see something{domain2_browser:optional}')
def impl(ctx, state, domain2_browser):
    print(state)
    print(domain2_browser)

The 2nd step is undefined when I run test

@guewen
Copy link
Author

guewen commented May 12, 2020

This is from 7 years ago, I don't use this anymore 😉

@hafsanaeem-aai here the optional parameters are the n (nitpicking for English grammar...) and the active parameters.
As noted by @huyenuet, I doubt having 3 optional parameters following each other would work as the parser cannot know which parameter of the 3 you provide. So changing model_name to optional would not work, it can't know if you used active or model_name.

I have the feeling it could work with a more precise regular expression though.

Edit: sorry @huyenuet, I misread your example which is not about 3 optional parameters following each others, but a single parameter with 3 words.
But... I don't have the answer, your code seems alright (although I didn't touch behave since years).

@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