1

I ran into a unique issue while writing test cases with pytest If we write a test as:-

a = 4
b = 5
c = 4

def match_test():
    assert a == (b or c) # (fails with `AssertionError`)

Now if we do the same using constants

def match_test():
    assert a == (4 or 5)  (passes)

But passes if we breakdown the assert as:-

def match_test():
    assert a == b or a == c # (passed)

The same happens with strings, curious if anyone can explain why this unique behaviour, PS I am new to Pytest and assert statements.

Hari_pb
  • 7,088
  • 3
  • 45
  • 53
  • 2
    The condition `a == (4 or 5)` probably passed when you tried it because `a` was 4. The code you wrote is equivalent to `a == 4`, and it is not a correct way to use `or`. – kaya3 Feb 10 '20 at 06:01
  • I got it, thank you! – Hari_pb Feb 10 '20 at 06:05
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – AMC Feb 10 '20 at 06:13
  • Yes, I accepted this as duplicate, I thought this is unique to assert statements. – Hari_pb Feb 10 '20 at 06:15

1 Answers1

2

They are not the same. (4 or 5) is evaluated to 4, so

assert a == (4 or 5)

passes since 4 == 4.

(b or c) is evaluated to 5 (b is 5) so

assert a == (b or c)

fails since 4 != 5. assert a == (c or b) will pass.

assert a == b or a == c

pass because the assert evaluates the entire expression, True or False is True.

Guy
  • 46,488
  • 10
  • 44
  • 88