使用Ghost.py测试失败(“无事务处于活动状态”)

时间:2014-05-22 11:13:14

标签: python django django-testing ghost.py

我有一个Django项目,可以在Javascript中进行一些计算。

我正在使用Ghost.py尝试将有效的Javascript计算测试合并到Django测试套件中:

from ghost.ext.django.test import GhostTestCase

class CalculationsTest(GhostTestCase):
    def setUp(self):
        page, resources = self.ghost.open('http://localhost:8081/_test/')
        self.assertEqual(page.http_status, 200)

    def test_frobnicate(self):
        result, e_resources = self.ghost.evaluate('''
            frobnicate(test_data, "argument");
        ''')
        self.assertEqual(result, 1.204)

(其中frobnicate()是测试页面上的Javascript函数。

如果我一次运行一个测试,这非常有效。

但是,如果我运行django-admin.py test,我会

Traceback (most recent call last):
  ...
    result = self.run_suite(suite)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/runner.py", line 113, in run_suite
    ).run(suite)
  File "/usr/lib64/python2.7/unittest/runner.py", line 151, in run
    test(result)
  File "/usr/lib64/python2.7/unittest/suite.py", line 70, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python2.7/unittest/suite.py", line 108, in run
    test(result)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 184, in __call__
    super(SimpleTestCase, self).__call__(result)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/ghost/test.py", line 53, in __call__
    self._post_teardown()
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 796, in _post_teardown
    self._fixture_teardown()
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 817, in _fixture_teardown
    inhibit_post_syncdb=self.available_apps is not None)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 81, in handle_noargs
    self.emit_post_syncdb(verbosity, interactive, db)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 101, in emit_post_syncdb
    emit_post_sync_signal(set(all_models), verbosity, interactive, database)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(db).get_for_model(klass)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 47, in get_for_model
    defaults = {'name': smart_text(opts.verbose_name_raw)},
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/query.py", line 388, in get_or_create
    six.reraise(*exc_info)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/query.py", line 380, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/transaction.py", line 305, in __exit__
    connection.commit()
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 168, in commit
    self._commit()
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 136, in _commit
    return self.connection.commit()
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 136, in _commit
    return self.connection.commit()
django.db.utils.OperationalError: cannot commit - no transaction is active

(与django-nose一起运行会产生更奇怪,不一致的结果)

有关如何防止此问题的任何线索,目前阻碍了CI?

1 个答案:

答案 0 :(得分:0)

我自己没有使用Ghost,但是对于类似的测试,我必须使用TransactionTestCase来进行类似的测试。您可以尝试更改GhostTestCase并查看它是否正常工作?