2

I have a bunch of methods that need to be tested. I'd like to implement a check to ensure that I've written a test for each method. I'm able to check this in my tearDownClass() if I keep a list of tests run, but I can't figure out a way to fail from there.

if I try to do this:

class TestSystem(unittest.TestCase):

    class TestAB(TestSystem):
        @classmethod
        def setUpClass(self):
            print "ABSetup"

        @classmethod
        def tearDownClass(self):
            if (testsNotImplemented()):
                self.fail() #I'd like to fail TestAB here
            print "ABTeardown"  

I get a TypeError: unbound method fail() must be called with TestAB instance as first argument (got type instance instead)

What's the proper way to do this? Even just printing a message would be ok (if not ideal), but as far as I can tell, wing doesn't output "print" messages from withing the setup and teardown methods.

This question is similar, but I don't want the tests to crash, just fail.

Community
  • 1
  • 1
ForeverWintr
  • 5,492
  • 2
  • 36
  • 65
  • 1
    You probably want to raise an cls.failureException() exception because that's what fail would do. The fail() method can't be invoked from a classmethod because it's an instance method. – John Ehresman Jan 03 '13 at 21:51
  • 1
    @JohnEhresman, can you give an example? Adding `raise self.failureException()` gives an assertion error. Is that what you meant? – ForeverWintr Jan 03 '13 at 22:53
  • 1
    Yes, cls.failureException is set to AssertionError by default. This means that calling fail() is equivalent to raising an assertion error in the default case. Note that the convention for classmethod's is to name the 1st argument cls and not self because it's a reference to the class object and not an instance. – John Ehresman Jan 03 '13 at 23:29
  • An alternate way of doing this is to add a separate test that checked to see if all methods are covered. It would be an ordinary method called something like testAllMethodsTested(self) and would fail if not all the tests were written. – John Ehresman Jan 03 '13 at 23:39
  • Thanks @JohnEhresman, I wasn't aware of that convention. However, just calling `cls.failureException()` doesn't seem to have any effect (and I'm not sure why), and calling 'raise cls.failureException()' causes the tests to crash, neither of which are what I want. – ForeverWintr Jan 05 '13 at 00:12
  • I did consider adding an ordinary test, as you suggest, but the problem with that solution is that I'd need to schedule the ` testAllMethodsTested(self)' method to run after of my other tests, in order to be able to check that they'd all run. I'm not aware of a "correct" way to do that. – ForeverWintr Jan 05 '13 at 00:14
  • 1
    I guess I'm a bit confused here -- I thought you'd be introspecting the code to determine if all methods had tests written for them. I don't think the tearDownClass method has access to the results object that records which tests were run. There is also the question of what to do when not all of the tests are run in a test run; do you want all tests to fail in this case? As I think more about this, I think a separate method that can be run by itself or at any time during a test run is probably the way to go. – John Ehresman Jan 06 '13 at 17:56
  • The way I had planned to check that the tests were being run was to generate a list of methods tested as the tests run, and then compare that list to a list of methods that need to be tested, if that makes sense. Such an algorithm would require that the test that checks the generated list be run after all other tests, though. Sounds like you may be right that a self contained method that checks for test implementation rather than execution may be the way to go. If you'd care to write that as an answer, I'll accept it. – ForeverWintr Jan 07 '13 at 19:35

0 Answers0