【问题标题】:Django tests pass locally but not on Github Actions pushDjango 测试在本地通过,但在 Github Actions 推送上没有通过
【发布时间】:2022-01-11 02:31:39
【问题描述】:

我的测试在本地通过,实际上在 Github Actions 上它也显示“运行 8 个测试”,然后是“OK”(我有 8 个)。但是,由于回溯中出现奇怪的错误,测试阶段失败了。

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 421, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "SCHEMA": syntax error

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

Traceback (most recent call last):
  File "/home/runner/work/store/store/manage.py", line 22, in <module>
    main()
  File "/home/runner/work/store/store/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/core/management/commands/test.py", line 55, in handle
    failures = test_runner.run_tests(test_labels)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/test/runner.py", line 736, in run_tests
    self.teardown_databases(old_config)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django_heroku/core.py", line 41, in teardown_databases
    self._wipe_tables(connection)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django_heroku/core.py", line 26, in _wipe_tables
    cursor.execute(
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 421, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "SCHEMA": syntax error
Error: Process completed with exit code 1.

这些都是默认的 Django 文件,我没有弄乱它们。我真的不知道该怎么办,互联网搜索没有任何帮助。

【问题讨论】:

  • 您的网站是否托管在 heroku 上?因为在heroku中你不能使用sqlite。
  • 是的,但它工作正常。数据库和图像都正常工作;唯一的问题是 Github Actions 中的测试。

标签: sql django github testing github-actions


【解决方案1】:

刚刚遇到同样的问题。对我来说,我的项目 settings.py 中有 django_heroku。看起来,基于您的应用程序正在使用的 cmets。 我在 settings.py 中有这样的东西:

import django_heroku
django_heroku.settings(locals())

更改为以下内容,使其不在 github 操作上使用 django_heroku:

if os.environ.get('ENVIRONMENT') != 'github':
    import django_heroku
    django_heroku.settings(locals())

然后在工作流文件中声明一个环境变量,我有这个:

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.7, 3.8, 3.9, '3.10']
    env: 
      DJANGO_SECRET_KEY: "someKey"
      ENVIRONMENT: github

注意:这是假设您已设置环境变量。我知道有不同的方法,因此根据它们的设置方式,您可能必须更改它们的访问方式

更多信息here

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2018-12-18
    • 2020-10-28
    • 2022-07-07
    • 1970-01-01
    相关资源
    最近更新 更多