简体   繁体   English

python单元测试以测试循环而不中断

[英]python unit testing to test over the loop and not to break

def test2_1_wiered_chars(self):
    for char in l_wchar:
      self.assertFalse(is_number(char), msg='failed for char {}'.format(char) )

I was passing a list to the function is_number to check if it is not a number. 我正在将列表传递给函数is_number以检查它是否不是数字。 The list has the combination of letters and numbers. 该列表包含字母和数字的组合。 The test fails when it hits the first number as expected. 当测试达到预期的第一个数字时,测试将失败。 But it breaks out of the loop and does not show how does it behave with the rest of the indexes of the list. 但是它突围而出,并且没有显示它如何与列表的其余索引一起工作。 what is correct way to test all the values of the list in one test case? 在一个测试案例中测试列表的所有值的正确方法是什么?

You should not have all test data within a single test case. 您不应在一个测试用例中拥有所有测试数据。 The test data should be external to the test case. 测试数据应在测试用例外部。 This should work for your purpose: 这应该适合您的目的:

class CharTests(unittest.TestCase):
    pass

def test_gen(char):
    def test_method(self):
        return self.assertTrue(char.isdigit())
    return test_method

if __name__ == '__main__':

    for index, char in enumerate(l_wchar):
        test_name = 'test_{0}'.format(index)
        test = test_gen(char)
        setattr(CharTests, test_name, test)

    suite = unittest.TestLoader().loadTestsFromTestCase(CharTests)
    result = unittest.TextTestRunner(verbosity=2).run(suite)

For each character in the string, a test case method is appended to the class CharTests , after which the suite is executed. 对于字符串中的每个字符,将一个测试用例方法添加到CharTests类中,然后执行该套件。 This will give you in great detail which test cases failed 这将为您提供详细信息,哪些测试用例失败

def test2_1_wiered_chars(self)
  numbers = [char for char in l_wchar if is_number(char)]
  self.assertFalse(numbers, msg='failed for char(s) {}'.format(numbers))

Create a new list of the objects that fail and assert that your new list is empty once it's done. 为失败的对象创建一个新列表,并在完成后断言新列表为空。 Empty lists are falsey so this will fail if there are any integers in the list. 空列表是错误的,因此如果列表中有任何整数,这将失败。

Note: This could potentially be extremely memory inefficient if l_wchar is very large and contains a lot of numbers. 注意:如果l_wchar很大并且包含很多数字,则可能会导致内存效率l_wchar Making two copies of it may not be practical or possible. 制作两个副本可能不可行或不可能。

The aim of assert is the opposite of what you are trying to do. assert的目的与您尝试做的相反。 When you are testing something - in your case, if there is a number among a list of chars - and it fails once somewhere, it makes no sense for the test to keep running - it has already failed. 在测试某项内容时(在您的情况下,如果字符列表中有一个数字)并且某处失败一次 ,则继续运行该测试没有任何意义-该测试已经失败。

If you want to know all cases where the char is a number, you should change your code to something like 如果您想知道char是数字的所有情况,则应将代码更改为

results = []
for char in l_wchar:
    if is_number(char):
        msg='failed for char {}'.format(char)
        print msg
        results.append(True)

Then you'll have a list where all indexes with True as value correspond to numbers in the other list. 然后,您将得到一个列表,其中所有值为True索引对应于另一个列表中的数字。

To continue asserting ie stating whether the test fails or not, just do it at the end 要继续asserting即说明测试是否失败),只需在最后完成

assertFalse(any(results))

However, it is important to notice that this is not an efficient and correct test. 但是,请务必注意,这不是有效且正确的测试。 You do not have to test everything to know if a test fails or not. 您不必测试所有内容即可知道测试是否失败。 The test should fail (and stop) the first time something goes unexpected. 第一次出现意外情况时,测试应失败 (并停止)。

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

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