1
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. 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?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
A. S
  • 41
  • 3

2 Answers2

1

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. This will give you in great detail which test cases failed

deborah-digges
  • 1,165
  • 11
  • 19
0
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. Making two copies of it may not be practical or possible.

kylieCatt
  • 10,672
  • 5
  • 43
  • 51