简体   繁体   English

Python unittest 条件断言

[英]Python unittest conditional assert

I need to test if either assertEqual(var, 'a') or assertEqual(var2, 'a') is true.我需要测试assertEqual(var, 'a')assertEqual(var2, 'a')是否为真。

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.无论var = 'a'还是var2 = 'a'测试都应该成功,但在这种情况下,如果var2 = 'b'它将失败。

So how could I write this test?那么我怎么能写这个测试呢? Because if I use if assertEqual(var, 'a') or assertEqual(var2, 'a'): , what should I type inside?因为如果我使用if assertEqual(var, 'a') or assertEqual(var2, 'a'): ,我应该在里面输入什么?

您可以使用assertTrue

assertTrue(var == 'a' or var2 == 'a')

I had a slightly different problem: if var was either 'a' or 'b'我有一个稍微不同的问题:如果var'a''b'

I used assertIn(var, {'a', 'b'})我使用了assertIn(var, {'a', 'b'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM