2

I have a response from a WS which returns a dictionary of the list. In general, I need to check if none of the lists is empty from the dictionary. However now I need to check some of them, but not all of them, based on the condition.

For example: The result is:

{
    'firstList': [{...}], #some data in the list
    'secondList': [], #empty list
    'thirdList': [{...}], #some data in the list
    ...
}

So for example, if I have a condition, that the secondList shouldn't be checked with

assert len(response['secondList']) > 0

Then it shouldn't raise an exception, but if I don't have it in the condition, then it should raise an assertion exception.

Any idea what is the best solution for this problem? Maybe another assertion library?

The best would've been to collect all of the exceptions in the end then I could validate them if they are ok/nok.

2 Answers 2

5

There are several ways but one that is quite clean would be:

lists_to_check = ('firstList', 'thirdList')
for list_name in lists_to_check:
    assert len(response[list_name]) > 0

If you want to check them one by one you can also use something like:

assert condition1 or len(response['firstList']) > 0
assert condition3 or len(response['thirdList']) > 0

but I would use the first one anyway. The above snippets only check the length of the 'firstList' and 'thirdList'.

Sign up to request clarification or add additional context in comments.

1 Comment

Why iterate through the whole dict if you know which elements need to be checked? Could you not say: for list_name in lists_to_check: assert len(response[list_name]) > 0
2

You can add the condition to the assert

assert condition or len(response['secondList']) > 0

The assertion will pass if atleast one of the conditions is True.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.