【问题标题】:Django unitest - test image downloadDjango unitest - 测试图片下载
【发布时间】:2016-07-31 02:27:06
【问题描述】:

您好,我的 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)

【问题讨论】:

标签: django beautifulsoup django-testing django-tests


【解决方案1】:

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

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

【讨论】:

  • 试过了,还是一样的问题。
  • 检查您是否没有从我的原始帖子中复制和粘贴,我使用的是 localhost 而不是 localhost。也许尝试使用 127.0.0.1 而不是 localhost?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 2014-02-23
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多