Docker runner pytest不收集测试用例

问题描述 投票:1回答:1

我成功地把ssh-key放到了docker runner,它可以git clone做依赖安装。

如果我使用普通的内置python manage.py test它工作正常。但我现在正在与pytest合作。我可以在命令行上运行pytest并获得正常的结果。

$ pytest
===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
Django settings: poinkbackend.config.settings.local (from ini file)
rootdir: /Users/sarit/Code/poink, inifile: pytest.ini
plugins: django-3.1.2
collected 66 items

poinkbackend/apps/activities/tests.py ..
poinkbackend/apps/amendments/tests.py ........
poinkbackend/apps/branches/tests.py ....
poinkbackend/apps/company_perms/tests.py ......
poinkbackend/apps/news/tests.py ......
poinkbackend/apps/orders/tests.py .....
poinkbackend/apps/poinks/tests.py .
poinkbackend/apps/redemptions/tests.py ..s....
poinkbackend/apps/rewards/tests.py ..
poinkbackend/apps/roles/tests.py ........
poinkbackend/apps/userprofiles/tests.py ............ss...

但是当我在gitlab-ci执行时。我使用docker作为runner。 更新1: 我确定。我在顶级目录中有pytest.inipytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = poinkbackend.config.settings.local
python_files = tests.py test_*.py *_tests.py



Successfully built django-dirtyfields django-reversion django-s3-folder-storage olefile simplegeneric
Installing collected packages: django-geoposition, pytz, django, py-moneyed, django-money, appnope, jmespath, six, python-dateutil, docutils, botocore, s3transfer, boto3, certifi, chardet, django-storages, collectfast, decorator, django-choices, django-cors-headers, django-countries, sqlparse, django-debug-toolbar, django-dirtyfields, django-environ, typing, django-extensions, django-filter, django-guardian, django-reversion, django-s3-folder-storage, pyjwt, djangorestframework-jwt, djangorestframework, freezegun, greenlet, gevent, gunicorn, idna, ipython-genutils, pygments, simplegeneric, pickleshare, ptyprocess, pexpect, traitlets, wcwidth, prompt-toolkit, parso, jedi, ipython, model-mommy, olefile, pillow, psycopg2, py, pytest, pytest-django, urllib3, requests, rest-framework-generic-relations, werkzeug
  Running setup.py develop for django-geoposition
  Running setup.py develop for django-money
Successfully installed appnope-0.1.0 boto3-1.4.7 botocore-1.7.47 certifi-2017.11.5 chardet-3.0.4 collectfast-0.5.2 decorator-4.1.2 django-1.11.7 django-choices-1.6.0 django-cors-headers-2.1.0 django-countries-5.0 django-debug-toolbar-1.9.1 django-dirtyfields-1.3 django-environ-0.4.4 django-extensions-1.9.7 django-filter-1.1.0 django-geoposition django-guardian-1.4.9 django-money django-reversion-2.0.10 django-s3-folder-storage-0.5 django-storages-1.6.5 djangorestframework-3.7.3 djangorestframework-jwt-1.11.0 docutils-0.14 freezegun-0.3.9 gevent-1.2.2 greenlet-0.4.12 gunicorn-19.7.1 idna-2.6 ipython-6.2.1 ipython-genutils-0.2.0 jedi-0.11.0 jmespath-0.9.3 model-mommy-1.4.0 olefile-0.44 parso-0.1.0 pexpect-4.3.0 pickleshare-0.7.4 pillow-4.3.0 prompt-toolkit-1.0.15 psycopg2-2.7.3.2 ptyprocess-0.5.2 py-1.5.2 py-moneyed-0.7.0 pygments-2.2.0 pyjwt-1.5.3 pytest-3.2.5 pytest-django-3.1.2 python-dateutil-2.6.1 pytz-2017.3 requests-2.18.4 rest-framework-generic-relations-1.1.0 s3transfer-0.1.11 simplegeneric-0.8.1 six-1.11.0 sqlparse-0.2.4 traitlets-4.3.2 typing-3.6.2 urllib3-1.22 wcwidth-0.1.7 werkzeug-0.12.2
$ pwd
/builds/sarit/poink
$ ls
DockerFileBackend
README.md
delete_migrations.sh
docker-compose.yml
f2
frontend
manage.py
poinkbackend
pytest.ini
renew_db.sh
requirements.in
requirements.txt
src
static
static_files
test_gitlab.sh
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
Django settings: poinkbackend.config.settings.local (from ini file)
rootdir: /builds/sarit/poink, inifile: pytest.ini
plugins: django-3.1.2
collected 0 items / 1 errors

==================================== ERRORS ====================================
______________________________ ERROR collecting  _______________________________
/usr/local/lib/python3.6/site-packages/_pytest/config.py:342: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/builds/sarit/poink/src/django-money/tests')

During handling of the above exception, another exception occurred:
/usr/local/lib/python3.6/site-packages/_pytest/config.py:373: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/builds/sarit/poink/src/django-money/tests/conftest.py')

During handling of the above exception, another exception occurred:
/usr/local/lib/python3.6/site-packages/_pytest/config.py:379: in _importconftest
    mod = conftestpath.pyimport()
/usr/local/lib/python3.6/site-packages/py/_path/local.py:668: in pyimport
    __import__(modname)
<frozen importlib._bootstrap>:971: in _find_and_load
    ???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:656: in _load_unlocked
    ???
<frozen importlib._bootstrap>:626: in _load_backward_compatible
    ???
/usr/local/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:212: in load_module
    py.builtin.exec_(co, mod.__dict__)
src/django-money/tests/conftest.py:9: in <module>
    from tests.testapp.models import InheritorModel, ModelWithDefaultAsInt
src/django-money/tests/testapp/models.py:21: in <module>
    class ModelWithVanillaMoneyField(models.Model):
/usr/local/lib/python3.6/site-packages/django/db/models/base.py:118: in __new__
    "INSTALLED_APPS." % (module, name)
E   RuntimeError: Model class tests.testapp.models.ModelWithVanillaMoneyField doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

During handling of the above exception, another exception occurred:
/usr/local/lib/python3.6/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
/usr/local/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
/usr/local/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
/usr/local/lib/python3.6/site-packages/py/_path/common.py:418: in gen
    dirs = self.optsort([p for p in entries
/usr/local/lib/python3.6/site-packages/py/_path/common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
/usr/local/lib/python3.6/site-packages/_pytest/main.py:737: in _recurse
    ihook = self.gethookproxy(path)
/usr/local/lib/python3.6/site-packages/_pytest/main.py:641: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
/usr/local/lib/python3.6/site-packages/_pytest/config.py:356: in _getconftestmodules
    mod = self._importconftest(conftestpath)
/usr/local/lib/python3.6/site-packages/_pytest/config.py:381: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: RuntimeError("Model class tests.testapp.models.ModelWithVanillaMoneyField doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.",)
E     File "<frozen importlib._bootstrap>", line 971, in _find_and_load
E     File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
E     File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
E     File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
E     File "/usr/local/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
E       py.builtin.exec_(co, mod.__dict__)
E     File "/builds/sarit/poink/src/django-money/tests/conftest.py", line 9, in <module>
E       from tests.testapp.models import InheritorModel, ModelWithDefaultAsInt
E     File "/builds/sarit/poink/src/django-money/tests/testapp/models.py", line 21, in <module>
E       class ModelWithVanillaMoneyField(models.Model):
E     File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 118, in __new__
E       "INSTALLED_APPS." % (module, name)
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.57 seconds ============================
ERROR: Job failed: exit code 1

.gitlab-ci.yml

image: python:3.6
services:
  - postgres:latest

variables:
  POSTGRES_DB: poinkdb
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan github.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - git config --global user.email "[email protected]"
  - git config --global user.name "sarit"
  - python -V
  - pip install -r requirements.txt

test:
  tags:
    - poink
    - Elcolie
  variables:
    DATABASE_URL : "postgres://postgres:postgres@postgres:5432/poinkdb"
  script:
  - pytest

问题: UPDATE2: UPDATE3:

  1. 如何使用gitlab-ci运行pytest运行docker runner
python docker pytest gitlab-ci pytest-django
1个回答
1
投票

感谢https://github.com/nicoddemus

他对我最后一行问题的引用向我展示了/src,这不是我的目录。它是在路上创建的。

我的陷阱是runner/src中创建python依赖项而不是在其他隐藏目录中

[pytest]
DJANGO_SETTINGS_MODULE = poinkbackend.config.settings.local
python_files = tests.py test_*.py *_tests.py
norecursedirs = src
© www.soinside.com 2019 - 2024. All rights reserved.