How to spread django unit tests over multiple files?

后端 未结 10 1920
后悔当初
后悔当初 2020-12-07 11:46
  • I have a python-django application
  • I\'m using the unit testing framework
  • The tests are arranged in the file \"tests.py\" in the module directory
10条回答
  •  無奈伤痛
    2020-12-07 12:26

    Note that this approach is no longer valid from Django 1.6, see this post.

    You can create tests folder with ___init___.py inside (so that it becomes a package). Then you add your split test .py files there and import all of them in ___init___.py.

    I.e: Substitute the test.py file with a module that looks and acts like the file:

    Create a tests Directory under the app in question

    app
    app\models.py
    app\views.py
    app\tests
    app\tests\__init__.py
    app\tests\bananas.py
    app\tests\apples.py
    

    Import the submodules into app\tests\__init__.py:

    from bananas import *
    from apples import *
    

    Now you can use ./manage.py as if they were all in a single file:

    ./manage.py test app.some_test_in_bananas
    

提交回复
热议问题