Search code examples
pythontestingassertnose

Python test framework with support of non-fatal failures


I'm evaluating "test frameworks" for automated system tests; so far I'm looking for a python framework. In py.test or nose I can't see something like the EXPECT macros I know from google testing framework. I'd like to make several assertions in one test while not aborting the test at the first failure. Am I missing something in these frameworks or does this not work? Does anybody have suggestions for python test framworks usable for automated system tests?


Solution

  • I was wanting something similar for functional testing that I'm doing using nose. I eventually came up with this:

    def raw_print(str, *args):
        out_str = str % args
        sys.stdout.write(out_str)
    
    class DeferredAsserter(object):
        def __init__(self):
            self.broken = False
        def assert_equal(self, expected, actual):
            outstr = '%s == %s...' % (expected, actual)
            raw_print(outstr)
            try:
                assert expected == actual
            except AssertionError:
                raw_print('FAILED\n\n')
                self.broken = True
            except Exception, e:
                raw_print('ERROR\n')
                traceback.print_exc()
                self.broken = True
            else:
                raw_print('PASSED\n\n')
    
        def invoke(self):
            assert not self.broken
    

    In other words, it's printing out strings indicating if a test passed or failed. At the end of the test, you call the invoke method which actually does the real assertion. It's definitely not preferable, but I haven't seen a Python testing framework that can handle this kind of testing. Nor have I gotten around to figuring out how to write a nose plugin to do this kind of thing. :-/