5

This is my function:

def get_value(request, param): s = get_string(request, param) value = re.search('(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s) if not value: print 'match not found!' raise Exception('incorrect format: %s' % param) 

test function:

def test_get_value(self): m = test_mocks.HttpRequestMock(REQUEST = {'start_date': '2011.07.31'}) print '*************************' print 'date format changed' self.assertRaises(Exception, get_value, (m, 'start_date')) print '********************* 

get_value doesn't print: match not found!

2
  • 1
    Your helpers.get_date_param calls get_value? Commented Oct 15, 2012 at 4:41
  • changed the call... i had modified the function name for posting the question here! Commented Oct 15, 2012 at 4:49

2 Answers 2

5

It seems there is some issue with your python version. I guess you are using python below version 2.6. Try passing function parameters as other arguments to function i.e. do not put them inside tuple. Try this.

self.assertRaises(Exception, helpers.get_value, m, 'start_date') 
Sign up to request clarification or add additional context in comments.

Comments

5

You're passing the arguments to assertRaises() incorrectly, you should pass them like this:

self.assertRaises(Exception, helpers.get_value, m, 'start_date') 

Here's a full test case that works for me. The first test passes, and the second one fails.

import re from unittest import TestCase def get_value(s): value = re.search('(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s) if not value: raise ValueError('incorrect format: %s' % s) class TesterScratch(TestCase): # this one passes def test_get_value(self): s = '2011.07.31' self.assertRaises(ValueError, get_value, s) # this one fails, because the format is actually correct def test_get_value2(self): s = '2011-07-31' self.assertRaises(ValueError, get_value, s) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.