-
-
Save guewen/7579757 to your computer and use it in GitHub Desktop.
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 |
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
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).
Also note that behave's may have changed since I wrote this gist.
I switch domain2_browser to 1st position, and it works!
e.g.
def impl(ctx, domain2_browser, state):
@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 ?
Do you have a example of passing optional variable parameter to step for example in this case 'model_name' ?