Skip to main content
 首页 » 编程设计

python之Django 1.11 : What fails `pickle` in my parallel test run

2024年12月31日35开发

我在 dockerized Django 1.11 上,现在想并行运行我长期使用的测试套件:

manage.py test mainfolder --parallel -v2

虽然它在我的测试套件中失败了 2 分钟(在所有到目前为止运行的测试都成功之后)并出现 pickle 错误。然而,堆栈跟踪并没有告诉我它发生的位置。我怎样才能更接近失败的原因? tblib 已安装。

test_foo (myapp.some.testmodule.SomeTestCase) ... ok 
Traceback (most recent call last): 
  File "./myapp/manage.py", line 34, in <module> 
    execute_from_command_line() 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line 
    utility.execute() 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv 
    super(Command, self).run_from_argv(argv) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute 
    output = self.handle(*args, **options) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/commands/test.py", line 62, in handle 
    failures = test_runner.run_tests(test_labels) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/test/runner.py", line 603, in run_tests 
    result = self.run_suite(suite) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/test/runner.py", line 567, in run_suite 
    return runner.run(suite) 
  File "/usr/lib/python2.7/unittest/runner.py", line 151, in run 
    test(result) 
  File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__ 
    return self.run(*args, **kwds) 
  File "/var/venv-stable/lib/python2.7/site-packages/django/test/runner.py", line 370, in run 
    subsuite_index, events = test_results.next(timeout=0.1) 
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 659, in next 
    raise value 
cPickle.PicklingError: Can't pickle <type 'module'>: attribute lookup __builtin__.module failed 

请您参考如下方法:

在内心深处,我有一个 __init__ 方法混合到一个测试用例中,不是我写的。它将对导入模块的引用存储到类中,我不知道这对谁有意义。

from django.core import mail 
 
def __init__(self, *args, **kwargs): 
    super(EmailTestHelperMixin, self).__init__(*args, **kwargs) 
    self.mail = mail 

删除它解决了我的问题。