And*_*ara 4 python testing django
我试图遵循我经常在 GitHub 存储库中找到的最佳实践:我希望在项目的顶层有一个 /src 和一个 /test 文件夹。(随机示例https://github.com/bitcoin/bitcoin)。我不确定如何配置 Django 来接受这一点。
特别是,Django 希望测试位于项目文件夹内,但理想情况下,这些测试应位于 /src/project_name 之外和 /test 内。
非常感谢任何建议。太感谢了!
project_root
|-- src
| |-- project_name
| |-- app_name
| | |-- views.py
| | |-- serializers.py
| | |-- etc...
| |-- manage.py
|-- test
|-- project_name
|-- test_feature1.py
|-- test_feature2.py
Run Code Online (Sandbox Code Playgroud)
我找到了答案!使用pytest-django模块。他们有一些关于通过 pythonpath 环境变量更改测试目录的文档(此处)。所有 django 测试也应该向后兼容 pytest,因此不需要对各个测试进行任何更改。
因此要从/test目录进行测试,
pytest-django:(pip install pytest-django另请确保添加pytest-django==4.5.2到您的requirements.txt 文件中)。这还会安装最新版本的pytest./src/<app_name>/tests/目录移至/test. 它应该如下所示:project_root
|-- src
| |-- project_name
| |-- app_name
| | |-- views.py
| | |-- serializers.py
| | |-- settings.py
| | |-- etc...
| |-- manage.py
|-- test
|-- test_feature1.py
|-- test_feature2.py
Run Code Online (Sandbox Code Playgroud)
pytest.ini在项目的根目录中创建一个文件。它必须指定DJANGO_SETTINGS_MODULE和pythonpath环境变量:project_root
|-- src
| |-- project_name
| |-- app_name
| | |-- views.py
| | |-- serializers.py
| | |-- settings.py
| | |-- etc...
| |-- manage.py
|-- test
|-- test_feature1.py
|-- test_feature2.py
Run Code Online (Sandbox Code Playgroud)
pytest(或python -m pytest)。这取代了python src/manage.py test src.希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
546 次 |
| 最近记录: |