开发者

django model unit test

开发者 https://www.devze.com 2023-02-20 22:05 出处:网络
I\'m making unit tests to a model. This test RUN ok def test_event_creation(self): new_user = User.objects.create_user(**self.user_info)

I'm making unit tests to a model.

This test RUN ok

def test_event_creation(self):
    new_user = User.objects.create_user(**self.user_info)
    new_event = Event.objects.create(name="Event test", user=new_user,
        start_date=datetime.date(2011,07,03), end_date=datetime.date(2011,07,10),
        start_time=datetime.time(8,30), end_time=datetime.time(18,00))

    self.assertEqual(Event.objects.count(), 1)

The problem 开发者_JAVA百科is that I need to test the model errors too, for example a invalid name. In that case the execution of the test show me an error (the error that i'm trying to test)

How i can get that error to compare in a assert and determine if is the error that suppost? to be.

(Maybe i have to use: assertRaisesRegexp)


self.assertRaises(exception, function, parameters)

exception: TypeError, ValueError, http://www.python.org/doc/essays/stdexceptions.html function: Event.objects.create parameters: user=new_user, start_date=datetime.date(2011,07,03), etc...

example:

def test_event_creation(self):
    new_user = User.objects.create_user(**self.user_info)
    self.assertRaises(TypeError, Event.objects.create,name="Event test", user=new_user,
        start_date=datetime.date(2011,07,03), end_date=datetime.date(2011,07,10),
        start_time=datetime.time(8,30), end_time=datetime.time(18,00))
0

精彩评论

暂无评论...
验证码 换一张
取 消