Bird
0
0

You want to write a pytest test to confirm that none of the forbidden words ['spam', 'scam', 'fraud'] appear in the user input string text. Which assertion correctly checks this?

hard🚀 Application Q15 of 15
PyTest - Writing Assertions
You want to write a pytest test to confirm that none of the forbidden words ['spam', 'scam', 'fraud'] appear in the user input string text. Which assertion correctly checks this?
Aassert all(word not in text for word in ['spam', 'scam', 'fraud'])
Bassert any(word in text for word in ['spam', 'scam', 'fraud'])
Cassert 'spam' not in text and 'scam' not in text or 'fraud' not in text
Dassert 'spam' in text or 'scam' in text or 'fraud' in text
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want to ensure none of the forbidden words appear in the text, so all must be absent.
  2. Step 2: Use a generator with 'all' and 'not in'

    The expression all(word not in text for word in forbidden_words) returns True only if every word is not in text.
  3. Step 3: Evaluate other options

    assert any(word in text for word in ['spam', 'scam', 'fraud']) checks if any forbidden word is present (opposite). assert 'spam' not in text and 'scam' not in text or 'fraud' not in text has incorrect logic mixing 'and' and 'or'. assert 'spam' in text or 'scam' in text or 'fraud' in text checks presence, not absence.
  4. Final Answer:

    assert all(word not in text for word in ['spam', 'scam', 'fraud']) -> Option A
  5. Quick Check:

    Use all() with 'not in' for absence of all items [OK]
Quick Trick: Use all() with 'not in' to confirm all items absent [OK]
Common Mistakes:
MISTAKES
  • Using any() instead of all() for absence check
  • Mixing 'and' and 'or' incorrectly in conditions
  • Checking only one forbidden word instead of all

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes