django测试用例

时间:2015-06-05 09:01:03

标签: django unit-testing testing bdd django-1.6

我为图像api编写了一个测试用例,但是我收到了这个错误

AttributeError:'ProgrammingError'对象没有属性'__traceback __'

我创建了一个图像,我去了api url,然后我测试图像是否存在,一切都很好 但是当我尝试从db中删除此图像时,我收到错误

我现在已经尝试了几天找到一个洗脱但我仍然不知道

这是我的代码

def create_image(name=''):
    ext = os.path.splitext(name)[1].lower()
    picture = Image(type=1, extension=ext)
    picture.save()

   output = StringIO.StringIO()
   im = PIL.Image.open(name)
   im.save(output, format=format)
   output.seek(0)
   default_storage.save(picture.get_filename(), output)
   """
   with open(name) as f:
       default_storage.save(picture.get_filename(), f)
   """
   return picture

def visit_url(picture):
    client = Client()
    response = client.get('/image/' + picture.uuid + picture.extension)
    return response

class ImagesTests(TestCase):

    def setUp(self):
        self.pictures_to_delete = []

    def test_api(self):
            picture = create_image('media/test_image.png')
            self.assertTrue(isinstance(picture, Image))
            self.pictures_to_delete.append(picture)

            resp = visit_url(picture)
            self.assertEqual(resp.status_code, 200)

            self.assertContains(resp, 'PNG')

def tearDown(self):
    for picture in self.pictures_to_delete:
        default_storage.delete(picture.get_filename())
        # Image.objects.get(uuid=picture.uuid).delete()
        picture.delete() #this is what gives the error

是导致问题的picture.delete

这是我的追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/south/management/commands/test.py", line 8, in handle
    super(Command, self).handle(*args, **kwargs)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    result = self.run_suite(suite)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/test/runner.py", line 113, in run_suite
    ).run(suite)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/runner.py", line 172, in run
    test(result)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/suite.py", line 87, in __call__
    return self.run(*args, **kwds)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/suite.py", line 126, in run
    test(result)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/django/test/testcases.py", line 184, in __call__
    super(SimpleTestCase, self).__call__(result)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/case.py", line 649, in __call__
    return self.run(*args, **kwds)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/case.py", line 609, in run
    self._feedErrorsToResult(result, outcome.errors)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/case.py", line 542, in _feedErrorsToResult
    result.addError(test, exc_info)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/runner.py", line 74, in addError
    super(TextTestResult, self).addError(test, err)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/result.py", line 19, in inner
    return method(self, *args, **kw)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/result.py", line 109, in addError
    self.errors.append((test, self._exc_info_to_string(err, test)))
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/unittest2/result.py", line 179, in _exc_info_to_string
    exctype, value, tb, limit=length, capture_locals=self.tb_locals)
  File "/home/astro/.virtualenvs/myrepo/local/lib/python2.7/site-packages/traceback2/__init__.py", line 449, in __init__
    exc_value.__cause__.__traceback__,
AttributeError: 'ProgrammingError' object has no attribute '__traceback__'

2 个答案:

答案 0 :(得分:0)

不是在测试中编写然后删除图像,而是使用它:

https://github.com/codysoyland/django-inmemorystorage

它更快,您不必删除任何图像。

答案 1 :(得分:0)

问题是测试运行器没有创建数据库,因此picture.save()方法不会改变数据库。

所以我刚用过

os.remove(f)