-3

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
Faith Berroya
  • 177
  • 1
  • 9
  • Thank you so much @Guy . But quick question, when to know whether we add `is True` to an assertion? :o – Faith Berroya Sep 13 '22 at 04:42
  • 1
    You never add `is True`, it's pointless. `assert` will pass or fail when given bool. There is no point in checking it's value again. – Guy Sep 13 '22 at 04:45
  • Hi @Guy thank you for that. Is it the same for adding `is False` ? How do we check something if it's disabled? Like, `assert elementLocator.is_enabled is False` or do we have a better way of writing it? – Faith Berroya Sep 13 '22 at 05:41
  • What about `assert not elementLocator.is_enabled`? – Matthias Sep 13 '22 at 07:13

1 Answers1

2

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.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15