简体   繁体   English

使用所有测试用例运行时,Django测试失败

[英]Django tests failing when run with all test cases

I have a problem with tests. 我的测试有问题。 When I run some tests I launch separately, they pass. 当我运行一些测试时,我会分别启动,它们会通过。 When all together then fail. 当所有的一切都失败了。

@mock.patch(
    'apps.abstract.validators.years_range_is_not_future', new=fake_years_range_is_not_future
)
def test_create_building_with_validation_of_foundation_period(self):

self.c.login(username=self.user.username, password='111')

response = self.c.post(
    '/en/api/buildings/',
    data={
        'name': "New Building",
        'foundation_period': {
            'lower': MIN_INT_VALUE,
            'upper': MAX_INT_VALUE
        },
        'stream': {
            'uuid': s_uuid(self.stream)
        }
    }
)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

I read about this problem here why would a django test fail only when the full test suite is run? 我在这里阅读有关此问题的信息, 为什么只有运行完整的测试套件时django测试才会失败? and tried to patch the validator in the serializer file as shown here 并尝试在序列化程序文件中修补验证程序,如下所示

@mock.patch(
    'apps.buildings.api.serializers.years_range_is_not_future', new=fake_years_range_is_not_future
)
def test_create_building_with_validation_of_foundation_period(self):
..............................................................

but then I get an incomprehensible for me exception 但是我得到了一个我无法理解的例外

Error
Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/mock.py", line 1049, in _dot_lookup
    return getattr(thing, comp)
AttributeError: module 'apps.buildings.api' has no attribute 'serializers'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/mock.py", line 1149, in patched
    arg = patching.__enter__()
  File "/usr/lib/python3.5/unittest/mock.py", line 1205, in __enter__
    self.target = self.getter()
  File "/usr/lib/python3.5/unittest/mock.py", line 1375, in <lambda>
    getter = lambda: _importer(target)
  File "/usr/lib/python3.5/unittest/mock.py", line 1062, in _importer
    thing = _dot_lookup(thing, comp, import_path)
  File "/usr/lib/python3.5/unittest/mock.py", line 1051, in _dot_lookup
    __import__(import_path)
  File "/home/env/project/apps/buildings/api/serializers.py", line 12, in <module>
    from apps.communities.api.serializers import CommunityBriefSerializer
  File "/home/env/project/apps/communities/api/serializers.py", line 297, in <module>
    class CommunityOfficialRequestBuildingSerializer(BaseCommunityOfficialRequestSerializer):
  File "/home/rp/env/project/apps/communities/api/serializers.py", line 299, in CommunityOfficialRequestBuildingSerializer
    from apps.buildings.api.serializers import BuildingBriefSerializer
ImportError: cannot import name 'BuildingBriefSerializer'

help please understand what I'm doing wrong 帮助请理解我在做什么

project structure ( __init__.py files not listed) 项目结构(未列出__init__.py文件)

project
       |__apps
             |__communities
             |             |_api
             |                  |_serializers.py
             |
             |__buildings
             |           |_api
             |           |    |_serializers.py
             |           |
             |           |_tests
             |                  |_test.py
             | 
             |_abstract
                      |_validators.py

Seeing this, 看到这个

Traceback (most recent call last):
  File "/home/rp/env/project/apps/communities/api/serializers.py", line 299, in CommunityOfficialRequestBuildingSerializer
    from apps.buildings.api.serializers import BuildingBriefSerializer

suggests that your import statement is inside a class or a def or some other statement. 建议您的import语句在classdef或其他语句中。

Maybe the import statement is executed after you mocked apps.buildings.api.serializers . 也许在嘲笑apps.buildings.api.serializers之后执行import语句。 If you move this import to the top of the file, then BuildingBriefSerializer will probably become available before apps.buildings.api.serializers is mocked, and your tests will pass. 如果将此导入移动到文件顶部,则在apps.buildings.api.serializers之前, BuildingBriefSerializer可能将变得可用,并且测试将通过。 This would also explain why the tests run, when you run them individually. 当您单独运行测试时,这也可以解释为什么运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM