Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest conditional assert

I need to test if either assertEqual(var, 'a') or assertEqual(var2, 'a') is true.

I can't just write them like:

assertEqual(var, 'a')
assertEqual(var2, 'a')

because that's not the scope of the test. The test should succeed whether var = 'a' or var2 = 'a', but in this case if for instance var2 = 'b' it will fail.

So how could I write this test? Because if I use if assertEqual(var, 'a') or assertEqual(var2, 'a'):, what should I type inside?


2 Answers

You could use assertTrue:

assertTrue(var == 'a' or var2 == 'a')
like image 50
Leistungsabfall Avatar answered Oct 20 '25 04:10

Leistungsabfall


I had a slightly different problem: if var was either 'a' or 'b'

I used assertIn(var, {'a', 'b'})

like image 30
calvh Avatar answered Oct 20 '25 04:10

calvh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!