如何使用django-nose和coverage获得测试覆盖率

时间:2019-04-12 21:25:40

标签: django python-3.x django-rest-framework travis-ci

我正在使用DRF创建API,能够安装Docker,还能够集成Travis CI。一切运行正常,Travis构建通过并通过,直到我尝试获得以下错误时的测试覆盖率:

PermissionError: [Errno 13] Permission denied: '/app/.coverage'
The command "docker-compose run app sh -c "python manage.py test"" exited with 1.

Dockerfile文件

FROM python:3.7-alpine
LABEL maintainer ="Daniel Otieno"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN pip install -r requirements.txt

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user

docker-compose.yml文件

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"

manage.py文件

import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc

    is_testing = 'test' in sys.argv

    if is_testing:
        import coverage
        cov = coverage.coverage(source=['core'], omit=['*/tests/*'])
        cov.set_option('report:show_missing', True)
        cov.erase()
        cov.start()

    execute_from_command_line(sys.argv)

    if is_testing:
        cov.stop()
        cov.save()
        cov.report()


if __name__ == '__main__':
    main()

settings.py

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--cover-erase',
    '--cover-package=core',
]

0 个答案:

没有答案