【问题标题】:Execute Unittest in Django在 Django 中执行单元测试
【发布时间】:2011-06-08 11:05:27
【问题描述】:

我为我的 django 视图写了一个小单元测试。我的项目结构是这样的

项目名称/

         apps/

              module1/
                      tests.py
              module2/
                      tests.py

这是我正在使用命令执行测试的目录结构:

$python manage.py test_coverage module1 module2 -v2

然后它很好地执行了测试,但是现在我稍微更改了目录结构,我创建了一个新目录 tests/,因为我保留了所有测试文件

项目名称/

       apps/

            module1/
                    tests/
                         test_basic.py
                         test_detail.py

现在我可以使用上述相同的命令执行目录中的那些测试,它们是执行此类测试的任何替代方法吗?

【问题讨论】:

    标签: python django unit-testing django-unittest


    【解决方案1】:

    最简单的解决方案是将__init__.py 文件添加到包含以下行的tests/ 包中:

    from .test_basic import *
    from .test_detail import *
    

    然后运行所有测试:

    $ python manage.py test module1
    

    【讨论】:

    • Hye Łukasz 它的作品......非常感谢。但是你能解释一下为什么以及何时我们需要在导入文件时使用点(。)。
    猜你喜欢
    • 2022-01-25
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多