开发者

How can I interactively explore why a test is failing?

I have a test that is failing with:

======================================================================  
    FAIL: test_register_should_create_UserProfile (APP.forum.tests.test_views.UserTestCAse)  
    ----------------------------------------------------------------------  
    Traceback (most recent call last):  
      File "/Users/Bryan/work/app/../app/forum/tests/test_views.py", line 25, in test_register_should_create_UserProfile  
        self.assertEqual(response.status_code, 200)  
    AssertionError: 404 != 200

Here is the test:

class UserTestCAse(TestCase):  
  def test_register_should_create_UserProfile(self):  
    from django.test.client import Client  
    c = Client()  
    # I'm skipping account/signin/ because that requires me to visit Google.  
    response = c.post('account/signin/complete/', {'username': 'john', "email":'john@beatles.com', u'bnewaccount': 'Signup'}) 
    # request.POST from pdb() session with breakpoint in register()
    # <QueryDict: {u'username': [u'john'], u'email': [u'john@beatles.com'], u'bnewaccount': [u'Signup']}>

    #How do I inspect what is breaking in the test case?  
    #How do I run the test client in the shell?  
    self.assertEqual(response.status_code, 200)  

    user = User.objects.get(username ='john')  
    self.assertTrue(user.get_profile())  

Is there anyway that I can see why this response is not returning 200?

I tried to use the TestClient() in the shell, but that didn't work:

In开发者_如何学JAVA [1]: from django.test.client import Client  

In [2]: c = Client()  

In [3]: response = c.post('account/signin/complete/', {'username': 'john', "email":'john@beatles.com', u'bnewaccount': 'Signup'})  
---------------------------------------------------------------------------  

KeyError: 'tried'  


This doesn't look right.

user = User.objects.get('username'=='john')  

If you want to query, you have to write queries in the style shown in the tutorial

http://docs.djangoproject.com/en/1.1/topics/db/queries/#topics-db-queries

user = User.objects.get( username = 'john' ) 

for example.

To debug, you can run things at the command line. That's what we do. The Django tutorial shows all examples as if they're typed interactively at the command line.

>>> from myapp.models import Client
>>> Client.objects.get( name = 'value' )

etc.

You don't generally try to run the unit tests from the command line. That's hard to do because of all the things the unit test framework does for you.

You generally just step through the application view function statements one at a time to be sure your view functions will actually work.


If you're getting an unexpected 404, the Django error page would show you the error (assuming DEBUG is True). In which case, you could just print response.content to show that traceback, and cut and paste that into a browser.

Also, don't forget you can break into a running test with the interactive debugger, pdb, just like any other form of running code, so you could inspect the response dynamically. Or even, put the breakpoint before the post, so you can step through the view.

However, on a second look, I suspect your url is not matching because you are missing an initial \.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜