使用SonarQube的Gitlab CI for Python项目

使用SonarQube的Gitlab CI for Python项目,sonarqube,gitlab,pytest,gitlab-ci,Sonarqube,Gitlab,Pytest,Gitlab Ci,我想用SonarQube为我的python项目设置Gitlab CI。我有一个问题。 我在设置gitlab ci中设置了SonarQube变量 这是我的gitlab-ci.yml文件: variables: SONARQUBE_ARGUMENTS_NORMAL: -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD --stacktrace SON

我想用SonarQube为我的python项目设置Gitlab CI。我有一个问题。 我在设置gitlab ci中设置了SonarQube变量

这是我的gitlab-ci.yml文件:

variables:

    SONARQUBE_ARGUMENTS_NORMAL: -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD --stacktrace
    SONARQUBE_ARGUMENTS_PREVIEW: -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD --stacktrace -Dsonar.analysis.mode=preview -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
    STAGE_ID: ${CI_PROJECT_NAME}_${CI_BUILD_REF_NAME}_${CI_JOB_NAME}_${CI_JOB_ID}

image: "python:3.7"

before_script:
  - python --version
  - python -c 'import struct;print( 8 * struct.calcsize("P"))'
  - pip install --upgrade pip
  - pip install --upgrade setuptools
  - pip install pytest
  - pip install -r requirements.txt

stages:
  - Static Analysis
  - Test

mypy:
  stage: Static Analysis
  script:
  - python -m pip install mypy
  - pwd
  - ls -l
  - python -m mypy --ignore-missing-imports *.py

flake8:
  stage: Static Analysis
  script:
  - python -m pip install flake8
  - flake8 --max-line-length=120 /*.py

pylint:
  stage: Static Analysis
  script:
  - pip install pylint
  - pylint -d C0301,C0114 *.py

test:
  stage: Test
  script:
  - pwd
  - ls -l
  - export PYTHONPATH="$PYTHONPATH:."
  - python -c "import sys;print(sys.path)"
  - pytest sonarqube $SONARQUBE_ARGUMENTS_NORMAL

after_script:
  - echo "End CI"
静态分析(flake8,mypy,pylint)工作正常,但我在使用Pytest的测试阶段遇到了问题。 在CI日志中,我有:

 $ pytest sonarqube $SONARQUBE_ARGUMENTS_NORMAL
 ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
 pytest: error: unrecognized arguments: -Dsonar.host.url=http://127.0.0.0:8000 -Dsonar.login=my_login -Dsonar.password=my_password --stacktrace
你能告诉我我做错了什么吗? 我和gradle在android项目中也做了同样的工作,效果很好

./gradlew test sonarqube $SONARQUBE_ARGUMENTS_NORMAL

pytest
gradlew
是两种不同的工具。您不能只是将一个工具的相同命令行参数传递给另一个工具,并期望它能够正常工作。我怀疑您想使用
pytest
运行测试并将结果发布到sonarqube?也许会有帮助。