Django unitest - 测试图像下载

时间:2016-07-31 02:27:06

标签: django beautifulsoup django-testing django-tests

你有我的Django测试用例的跟随代码,并且测试用例失败,因为c.get()正在命中404.所以我得到了这个错误。如果我在 python manage.py runserver 80 时转到http://localost//static/recaptcha/47.jpg,我可以看到我的图像很好。

Creating test database for alias 'default'...
..>>>img_url: /static/recaptcha/47.jpg
F...
======================================================================
FAIL: test_signup_get (fastmojo.tests.test_account.FastMojoSignUpTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\dj_site_test\fastmojo\tests\test_account.py", line 46, in test_signup_get
    self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 6 tests in 0.085s

我的测试用例

class SignUpTest(TestCase):

    def test_signup_get(self):
        rechapcha_image = soup.find('img', {'class':'recap'})
        assert rechapcha_image != None
        assert rechapcha_image['src'] != None

        img_url = rechapcha_image['src']
        print ">>>img_url: %s" % img_url
        assert img_url != ""
        assert img_url != None

        response = c.get(img_url)
        self.assertEqual(response.status_code, 200)

1 个答案:

答案 0 :(得分:0)

您是否尝试在get请求中传递完整的URL而不是相对路径?

看起来c.get(img_url)应该通过http://localhost/static/recaptcha/47.jpg而不是/static/recaptcha/47.jpg