Django测试数据库连接未关闭

时间:2019-04-10 08:19:35

标签: python django selenium testing

我使用Selenium来测试Django站点上的语言切换器是否正常运行。为了进行国际化,我使用Django的内置功能。<​​/ p>

测试可以顺利通过,但是在测试完成后,我立即收到psycopg2.OperationalError的提示,说有另一个使用该数据库的会话。测试实际上并没有使用数据库,从中我只能看到djangos会话管理。

即使我在测试的SESSION_EXPIRE_AT_BROWSER_CLOSE方法中调用True,也无法将driver.quit()设置为tearDownCLass。通过connections.close_all()关闭所有连接也没有任何改变。 select * from pg_stat_activity在运行测试时显示,COMMIT阶段中确实有一个空闲连接在断开测试数据库时打开。

我可以通过定义跳过DiscoverRunner的{​​{1}}来解决该问题,但这充其量是一个丑陋的解决方案。

编辑:确切的错误消息和堆栈跟踪。

teardown_databases

EDIT2 :示例测试用例

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: database "test_postgres" is being accessed by other users
DETAIL:  There is 1 other session using the database.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/test.py", line 56, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python3.7/site-packages/django/test/runner.py", line 607, in run_tests
    self.teardown_databases(old_config)
  File "/usr/local/lib/python3.7/site-packages/django/test/runner.py", line 580, in teardown_databases
    keepdb=self.keepdb,
  File "/usr/local/lib/python3.7/site-packages/django/test/utils.py", line 297, in teardown_databases
    connection.creation.destroy_test_db(old_name, verbosity, keepdb)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 257, in destroy_test_db
    self._destroy_test_db(test_database_name, verbosity)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 274, in _destroy_test_db
    % self.connection.ops.quote_name(test_database_name))
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
django.db.utils.OperationalError: database "test_postgres" is being accessed by other users
DETAIL:  There is 1 other session using the database.

0 个答案:

没有答案