- 项目是Django项目,使用tarven结合pytest编写、测试
- 所有测试用例都是test_xxx.tavern.yaml格式的,语法严格按照tavern要求编写
requirements.txt
pytest==7.1.1
tavern==1.23.1
pytest-html==2.1.1
coverage==6.4.1
pytest-cov==3.0.0
tox.ini
[tox]
; The only reason we'd want to `sdist` is if we distributed
; this as a Python package in PyPI, so let's skip it:
skipsdist = True
envlist =
{py37}-django{2}
lint
skip_missing_interpreters = true
[testenv]
whitelist_externals = *
passenv = *
deps = -rrequirements-test.txt
commands = {[testenv:unit]commands}
[testenv:unit]
deps = {[testenv]deps}
commands = pytest {posargs:--cov=djproject}
[pytest]
DJANGO_SETTINGS_MODULE = djproject.settings.local
python_files = test_*tavern.yaml
addopts =
-v -s
--color=yes
--cov
--cov-append
--cov-report=term-missing
--cov-config=tox.ini
--ignore=venv
--cov-report html
--cov-fail-under=80
[coverage:report]
show_missing = True
omit =
*/usr/*
*/.tox/*py
*/.virtualenvs/*
*/migrations/*
*/tests/*
*/venv/*
[report]
omit =
*/usr/*
*/.tox/*
*/migrations/*
*/tests/*
*/venv/*
下面是项目的结构
vuserver
├── apitests
│ ├── all.html
│ ├── assets
│ │ └── style.css
│ ├── get-coverage.sh
│ ├── parallel
│ │ ├── includes.yaml
│ │ ├── test_01_init_gets.tavern.yaml
│ │ └── utils.py
│ ├── requirements-test.txt
├── assets
│ └── style.css
├── build_vuserver.sh
├── Dockerfile
├── __init__.py
├── manage.py
├── parallel
│ ├── admin.py
│ ├── capture.py
│ ├── consumers.py
│ ├── event.py
│ ├── __init__.py
│ ├── models.py
│ ├── serializers.py
│ ├── stat.py
│ ├── template.yml
│ ├── urls.py
│ └── views.py
├── pylintrc
├── requirements.txt
├── script
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
│ ├── nodejs
│ │ └── vtaas-v2default
│ ├── serializers.py
│ ├── urls.py
│ └── views.py
├── tox.ini
├── venv
│ ├── bin
│ ├── lib
│ │ └── python3.7
│ └── pyvenv.cfg
├── vuserver
│ ├── asgi.py
│ ├── __init__.py
│ ├── routing.py
│ ├── settings
│ │ ├── base.py
│ │ ├── __init__.py
│ │ ├── local.py
│ │ ├── __pycache__
│ │ └── production.py
│ ├── urls.py
│ └── wsgi.py
下面的脚本是我执行服务,进行单元测试获取覆盖率的脚本
set -xv
basepath=$(cd `dirname $0` || exit; pwd)
echo "$basepath"
cd "$basepath" || exit
cd ..
if [ -f "db.sqlite3" ];then
rm -f "db.sqlite3"
fi
if [ -f ".coverage" ];then
rm -f ".coverage"
fi
source venv/bin/activate
cd "apitests" || exit
pip install -r requirements-test.txt
cd ..
python manage.py migrate --settings=vuserver.settings.local
coverage erase
cd ..
find . -regex '*.pyc' -delete
coverage run ./manage.py runserver --noreload 0.0.0.0:9000 --settings=vuserver.settings.local &
pytest
sleep 5
coverage report -m
coverage html
pgrep -f 9000 | xargs kill -9
执行上述脚本,测试用例全部通过,可是覆盖率始终是0
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
__init__.py 2 2 0% 1-3
container_pool/admin.py 9 9 0% 4-24
container_pool/apps.py 3 3 0% 4-8
container_pool/deploy.py 188 188 0% 4-263
container_pool/models.py 60 60 0% 4-78
container_pool/serializers.py 6 6 0% 4-11
container_pool/urls.py 5 5 0% 5-12
container_pool/views.py 27 27 0% 4-53
manage.py 13 13 0% 2-22
parallel/admin.py 21 21 0% 4-53
parallel/capture.py 171 171 0% 4-285
parallel/consumers.py 66 66 0% 4-151
parallel/event.py 290 290 0% 4-464
parallel/models.py 238 238 0% 4-313
parallel/serializers.py 122 122 0% 4-252
parallel/stat.py 201 201 0% 4-273
parallel/urls.py 15 15 0% 4-20
parallel/views.py 635 635 0% 4-1013
script/admin.py 6 6 0% 4-13
script/apps.py 3 3 0% 4-8
script/models.py 25 25 0% 4-33
script/serializers.py 6 6 0% 4-11
script/urls.py 4 4 0% 4-11
script/views.py 223 223 0% 4-331
vuserver/asgi.py 6 6 0% 1-7
vuserver/routing.py 5 5 0% 4-10
vuserver/settings/__init__.py 8 8 0% 1-8
vuserver/settings/base.py 50 50 0% 5-250
vuserver/settings/decc.py 24 24 0% 4-60
vuserver/settings/local.py 22 22 0% 4-43
vuserver/settings/staging.py 29 29 0% 4-66
vuserver/urls.py 19 19 0% 4-38
vuserver/wsgi.py 4 4 0% 5-11
------------------------------------------------------------------------------
TOTAL 2572 2572 0%
请问如何才能获取到正确的覆盖率,问题出在上述哪一步,谢谢!