Last active
May 21, 2020 13:16
-
-
Save guewen/7579757 to your computer and use it in GitHub Desktop.
Use an optional argument in a behave phrase.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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 ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 theactive
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 usedactive
ormodel_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).