背景
使用django开发了个人博客,欲单元测试,后遍寻网络,然相关资料甚少,遂成此文,望对汝有所助
环境
- pytest
- pytest-django
- pytest-cov
- pytest-html
- django
- mixer
测试配置
项目根目录下新建pytest.ini和conftest.py
pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE = carp.settings
python_files = tests.py test_*.py *_tests.py
addopts = --cov=blog/
carp是我的的项目名称
conftest.py
#使用测试的数据库
@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'test_db.sqlite3'),
}