Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm relatively new to unittesting in python and find that some tasks are quite arduous and I've seen nicer packages in other languages.

I've been wondering if there are packages that help me for example with shortening validation of complex properties (of which I tend to have a lot) like below, or if there are none, is there any reason I should not consider writing them?

def test_lengthProperty(self):
    tester = new PropertyTester(instance.property)

    # assert raises TypeError
    tester = tester.withInstanceValidation('pass': (int, long, float))
    tester = telter.withInstanceValidation('fail': (str, complex, unicode, ...))

    # assert raises ValueError
    tester = tester.withMinValue({'pass': (0), 'fail':(-1)}) 
    tester = tester.withMaxValue({'pass': (100), 'fail': (101)})

    # assert the values are just stored not adjusted.
    tester = tester.setterEqualsGetter({'pass' : range(101)})
    tester.assertAll()


def test_dateProperty(self):
    # assert raises TypeError
    tester = tester.withSubclassValidation('pass': (datetime.date))
    tester = tester.withInstanceValidation('fail': (str, complex, unicode, ...))

    # Assert adjusts invalid dates
    tester = tester.assertAdjust(
        {'input' : date(2010, 31, 12), 
         'output': date(2011, 1,1)})
    tester.assertAll()
share|improve this question
    
It's not entirely clear to me what you're looking for here, but if you want to check that a call to a callable raises a TypeError, then use unittest.TestCase.assertRaises() with TypeError as the first argument. The TestCase class provides a whole host of methods to check for and report failures. –  Johnsyweb Apr 2 '14 at 22:30
1  
Have you looked at nose? There are plug-ins for it as well. Or, if you do get into writing your own, nose might be the place to start. –  loste Apr 3 '14 at 3:33
    
I understand that I can do individual tests, but I'm looking after a shorthand, that allows me to use one line to make several assertions. –  dashservice Apr 3 '14 at 12:18
    
Looking at nose at the moment and it appears to cover many of my use cases. Thank you. –  dashservice Apr 3 '14 at 12:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.