【问题标题】:Django Test : __init__() got an unexpected keyword argument 'failfast'Django 测试:__init__() 得到了一个意外的关键字参数“failfast”
【发布时间】:2019-11-26 00:12:30
【问题描述】:

我的 Django 应用有问题,这周我添加了一些模型和测试,但是当我要使用 "python manage.py test" 对其进行测试时,我遇到了这个错误:

TypeError: __init__() got an unexpected keyword argument 'failfast'

我尝试查看我的模型/序列化程序/测试中是否有一些错误(语法错误),甚至尝试返回到一个古老的工作提交,但它在测试时并没有解决我的问题...... 另外,当我使用 "python manage.py runserver".

时,我的服务器工作正常
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\django\test\runner.py", line 633, in run_tests
    result = self.run_suite(suite)
  File "C:\Users\Arh\.virtualenvs\mybacksentinhealth-gHbH4SRy\lib\site-packages\xmlrunner\extra\djangotestrunner.py", line 57, in run_suite
    runner = self.test_runner(**runner_kwargs)
TypeError: __init__() got an unexpected keyword argument 'failfast'

我不在命令行中使用 failfast 参数,所以这个错误对我来说毫无意义。 我使用 Django 2.2.2 !

这里是我的测试的样子:

import pytz
from logging import info
from django.test import TestCase
from django.utils import timezone
from django.test import TransactionTestCase
from apps.myheart.api.api_acquisitions_markers import acquisitions_markers_get
from apps.myheart.models import Implants, Markers, AcquisitionsMarkers, Acquisitions
from datetime import timedelta, datetime
from .test_authentication import authenticate

class ImplantsAPIViewTestCase(TransactionTestCase):


    def setUp(self):
            self.implant1 = Implants.objects.create(
                num=1, implantation_date=datetime(2012, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital1"
            )
            self.implant2 = Implants.objects.create(
                num=2, implantation_date=datetime(2014, 11, 18, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital2"
            )
            self.implant3 = Implants.objects.create(
                num=34, implantation_date=datetime(2016, 10, 18, 20, 8, 7, 127325, tzinfo=pytz.UTC), implantation_hopital="hopital1"
            )
            self.token = authenticate()
            self.nb_imp = len(Implants.objects.all())


    def test_implants_get(self):
        info("Test /api/implants/\n")
        response = self.client.get("/api/implants/", HTTP_AUTHORIZATION=self.token)
        info(response.data)
        # We need to check that we have all the files presents in DB
        self.assertEqual(len(response.data), self.nb_imp)
        info("\n")

【问题讨论】:

  • 您是否更改了 testrunner 上的某些内容或与 TestCase 类 SetUp 相关的任何内容?我的意思是,我只是在这里在黑暗中拍摄。
  • 我没有更改包中的任何代码,甚至尝试将所有这些(我正在使用 pipenv)重新安装到工作版本。我正在向我的问题添加一个关于我的测试是什么样子的 sn-p。

标签: django django-tests


【解决方案1】:

我在一个 django 项目上遇到了同样的问题,该项目曾在 python 2 上运行,但在我切换到 python 3 时却神秘地停止了工作。

对我来说,解决方案是从 xmlrunner 包切换到 unittest-xml-reporting

即试试这个:-

pip3 uninstall xmlrunner
pip3 install unittest-xml-reporting

不再支持 xmlrunner 包,取而代之的是 unittest-xml-reporting。

【讨论】:

    【解决方案2】:

    我刚刚发现了我的错误,似乎是 XMLRunner 和 unittest-xml-reporting 向我报告了这个错误。由于我使用了它们一段时间并且效果很好,我不知道为什么不再工作但删除它们修复了它。

    【讨论】:

      【解决方案3】:

      Django 测试:init() 得到了一个意外的关键字参数 'failfast' 在这个网站的帮助下,我花了一段时间才弄清楚这一点。为 pipenv 添加答案。 pipenv 卸载 xmlrunner pipenv 安装 unittest-xml-reporting

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-08
        • 2017-03-01
        • 2020-06-15
        • 2021-11-17
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多