Skip to content

New Should Contain Any and Should Not Contain Any keywords #2120

Closed

Description

@jongu82

I am searching through a list and would like to pass if any one of my values are found. It appears that the List Should Contain Value keyword only accepts one value. I have tried using Value1 or Value2 but it does not appear to be searching for either one. I would suggest to modify this keyword to accept multiple values separated by 'or'.

Furthermore, I would like to have the output display the matches, and fail if no matches are found

Activity

  1. pekkaklarck commented on Aug 28, 2015

    @pekkaklarck
    Member

    Yes, there doesn't seem to be any keyword that supports this in Collections. We cannot change List Should Contain Value because basically any change would be backwards incompatible. Adding separate List Should Contain Any Value is possible, but we could also consider adding more generic Should Contain Any to BuiltIn. Are you interested to provide a pull request?

  2. jongu82 commented on Aug 28, 2015

    @jongu82
    Author

    Yes I am interested. I'm fairly new to github and the use of pull requests but I'd be willing to give it a shot.

  3. pekkaklarck commented on Aug 28, 2015

    @pekkaklarck
    Member

    GitHub has pretty good documentation about pull requests, and our contributing guidelines have links to them along with some other valuable information. Let us know if it is hard to get started or you get stuck somewhere.

    Would you be interested to implement List Should Contain Any Value or Should Contain Any? I think the latter would be more valuable because it would be more generic. If the former is implemented, then we should add related dict keywords too. Adding all of them is possible too but perhaps too much work compared to benefits. Regardless what is implemented, Not version (e.g. Should Not Contain Any) should be added too.

  4. pekkaklarck commented on Oct 9, 2015

    @pekkaklarck
    Member

    After thinking this a bit more, I believe it's best to implement Should Contain Any and Should Not Contain Any. They are generic and can be used with strings or anything that is list-like. This functionality isn't needed too often, so adding separate List Should Contain Any Value, Dictionary Should Contain Any Key, etc. wouldn't be worth the effort in my opinion.

    Anyone interested to implement Should (Not) Contain Any? There's plenty of time until RF 3.0 which is the next planned release and I can help if needed. If you are new to providing pull requests and contributing in general, you can start by reading these guidelines:
    https://github.com/robotframework/robotframework/blob/master/CONTRIBUTING.rst

  5. changed the title [-]Allow List Should Contain Value to accept multiple values[/-] [+]New `Should Contain Any` and `Should Not Contain Any` keywords[/+] on Jun 1, 2016
  6. added this to the 3.0.1 milestone on Jun 1, 2016
  7. pekkaklarck commented on Jun 1, 2016

    @pekkaklarck
    Member

    Nobody has shown any interest to implement the new keywords, but I consider them so useful that I plan to include them in RF 3.0.1 anyway. If someone is interested in contributing to RF, this would be a pretty easy issue to get started.

  8. jimni commented on Jun 2, 2016

    @jimni
    Contributor

    I would like to have a go at this issue.
    I will either come up with the solution before 7th of June, or consider me forfeit :)

  9. pekkaklarck commented on Jun 2, 2016

    @pekkaklarck
    Member

    Great! Please provide a pull request once you got something ready and we can review it. If you do it in a separate branch, you can easily commit new changes and the PR automatically gets updated.

    The signature of these keywords should probably be something like container, *items. That allows usage like this:

    Should Contain Any    ${text}    foo    bar    zap
    

    What do you think, should same msg=None and values=True arguments be accepted as Should (Not) Contain accepts? If yes, the signature should probably be container, *items, **config and code contain something like:

        msg = config.pop('msg', None)
        values = config.pop('values', True)
        if config:
            raise RuntimeError('Message explaining that unsupported config values were used')

    Alternatively we could look does the last and second last item start with msg= and values= but that is more complicated. This kind of **config approach is already used by the Process library and it has worked well there. If we didn't need to care about Python 2, we could just use keyword-only arguments.

  10. jimni commented on Jun 2, 2016

    @jimni
    Contributor

    What do you think, should same msg=None and values=True arguments be accepted as Should (Not) Contain accepts?

    I believe they should. I will aim for this behaviour.

  11. jimni commented on Jun 2, 2016

    @jimni
    Contributor

    Should we also redefine Should (Not) Contain as wrappers for these new keywords? As in:

    def should_not_contain(self, container, item, msg=None, values=True):
        self.should_not_contain_any(container, [item], msg, values)
    

    I'm not sure if this interpretation of SRP is considered appropriate in RF, please let me know.

  12. pekkaklarck commented on Jun 2, 2016

    @pekkaklarck
    Member

    Depends how the code would look line in the end. These kws may also have slightly different failure messages making direct reuse harder.

  13. jimni commented on Jun 2, 2016

    @jimni
    Contributor

    I see your point. Let's consider that afterwards

  14. jimni commented on Jun 5, 2016

    @jimni
    Contributor

    While writing tests I came across the fact that lists and tuples are stringified with u' ' (unlike strings):

    '(u'yet', u'yes', u'no')'
    

    I'm not sure if this is desired behaviour or not, or maybe I'm just doing something wrong.
    For now, I left this hanging in the tests' fail messages, because it looks harmless. Let me know if this needs a follow-up resolution.

    Looking forward to your feedback.

  15. pekkaklarck commented on Jun 5, 2016

    @pekkaklarck
    Member

    Is this in the error message? Probably should use utils.seq2str there.

    u prefix is added because strings passed to Robot are Unicode and string representation of tuples and lists contains the repr() of items. It's not added in Python 3 which is problematic in tests. Using the aforementioned util resolves that too.

  16. pekkaklarck commented on Jun 5, 2016

    @pekkaklarck
    Member

    On a very quick look the PR looks very good. Hopefully I have time to look it tomorrow in more detail. Trying to get our house sold means I'm somewhat busy.

  17. jimni commented on Jun 5, 2016

    @jimni
    Contributor

    Take your time, I'm not going anywhere.

  18. jimni commented on Jun 6, 2016

    @jimni
    Contributor

    utils.seq2str worked very well. Thank you.

  19. added a commit that references this issue on Oct 5, 2016
  20. self-assigned this
    on Oct 5, 2016
  21. pekkaklarck commented on Oct 5, 2016

    @pekkaklarck
    Member

    This has been implemented by @jimni in PR #2393 that was just merged. I'll do a little additional testing possibly little fine tuning but otherwise this issue can be considered done. Thanks @jimni for a great PR!

  22. pekkaklarck commented on Oct 7, 2016

    @pekkaklarck
    Member

    Final fine-tuning in done in 5bde125. Main change was making it an explicit error to use these keywords without any items to test against. Should Contain Any already failed with a bad error and Should Not Contain Any passed in this case. I think it is safer to fail in both cases and to have a better message, but it is still easy to change this before the 3.0.1 release.

    I consider this issue done. Already started thinking should we also add Should (Not) Match Any keywords... =)

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions