首页 > 解决方案 > 将 Python 配置为复杂的 Django 项目

问题描述

我需要在我的 django 应用程序中配置 pytest。但是目录的结构与我习惯的不同。

.
├── apps
│   ├── __pycache__
│   ├── admcore 
│   ├── ...
│   ├── cauth 
│   ├── webservice
│   └── webshell
├── doc
│   └── ...
├── lib 
│   └── ...
├── ...
└── project  
     ├── __init__.py  
     ├── globalsettings.py 
     ├── local
     │   ├── __init__.py  
     │   ├── app.uwsgi 
     │   ├── manage.py 
     │   ├── settings.py  
     │   └── wsgi.py
     ├── mailconf.py 
     └── urls.py 

我建立pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE=sgp.local.test_settings
addopts = --nomigrations --cov=. --cov-report=html 

test_settings.py文件

#!/usr/bin/env python
import os
import sys
from project import globalsettings

SECRET_KEY = "12341234AFDQFDQSDFAZR123D" 

ROOT_PATH = '/usr/local/sgp'
BASEURL_ROOT = ''

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:'
    }
}

我把这两个文件放在project目录中。

.
├── apps
│   ├── __pycache__
│   ├── admcore 
│   ├── ...
│   ├── cauth 
│   ├── webservice
│   └── webshell
├── doc
│   └── ...
├── lib 
│   └── ...
├── ...
└── project  
     ├── __init__.py  
     ├── globalsettings.py 
     ├── local
     │   ├── __init__.py  
     │   ├── app.uwsgi 
     │   ├── manage.py
     │   ├── pytest.ini
     │   ├── settings.py 
     │   ├── test_settings.py
     │   └── wsgi.py
     ├── mailconf.py 
     └── urls.py 

但它并没有像我预期的那样工作。我在 webservice 应用程序中进行了一项测试,但是当我运行pytest命令时,我得到了:

platform darwin -- Python 3.7.4, pytest-5.2.0, py-1.8.0, pluggy-0.13.0
Django settings: project.local.test_settings (from environment variable)
rootdir: /project/local, inifile: pytest.ini
plugins: django-3.5.1, cov-2.7.1
collected 0 items


---------- coverage: platform darwin, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov

=========================================================================================================== no tests ran in 0.15s ============================================================================================================

我不知道如何为这个项目配置 pytest。

标签: pythondjangotestingpytest

解决方案


推荐阅读