Python: Can we assert "in" and "not in" in a string?

Question:

I have this code that I need to check whether partialExpectedTitle is included in documentTitle . But I get an error afterwards that we have an assertion error. Does in and not in only work in a list? Or do I make it work in a string?

documentTitle = context.mailTrapPage.getExcelTitle().text.strip()
excelTitle = download_path + "/" + documentTitle
excelWorkbook = o.load_workbook(excelTitle)
print("partialExpectedTitle is " + partialExpectedTitle)
print("documentTitle is " + documentTitle)
assert partialExpectedTitle in documentTitle is True

Error:

      assert partialExpectedTitle in documentTitle is True
  AssertionError
  
  Captured stdout:
  ['1 - AAAAAA qlgmdfol']
  partialExpectedTitle is courses20220913_1110
  documentTitle is courses20220913_111050.xlsx
Asked By: Faith Berroya

||

Answers:

Because of operator chaining, the expression x in y is True is equivalent to (x in y) and (y is True), which is probably not what you had in mind.

So when do you need is True? Hardly ever. It only makes sense when you need to distinguish the actual value True from a "truthy" value such as 17 or "hello".

For your example in a comment, use the not operator: assert not elementLocator.is_enabled.

Answered By: Ture Pålsson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.