SonarQube集成python检测(11)

匿名 (未验证) 提交于 2019-12-02 22:11:45

sonar检测python的检测工具

sonar中的插件python自带一些检测规则,但sonar想要集成python的覆盖率、单元测试报告、和更多的代码规则,需要集成其他python的检测工具

单元测试(Ant模式下)

  • 如上所至项目根目录的sonar-project.properties文件添加如下配置
sonar.python.coverage.reportPath=*coverage-*.xml 
  • sonar-project.properties文件配置修改说明为:
Property Scope Example Description
sonar.python.coverage.reportPath Project-wide coverage-.xmlt SINCE 1.1 Ant pattern describing the path to coverage reports, relative to projects root. Leave unset to use the default (“coverage-reports/coverage-.xml”).The reports have to conform to the Cobertura XML format.

覆盖率

  • 通过coverage工具生成覆盖率统计报告(见代码检测工具-python测试工具)
  • 报告需要直接使用工具生成的报告
  • 报告生成运行如下命令
coverage run xx/xx_test.py             #运行指定文件 coverage xml xx/xx.py                  #生成xml报告,生成前必须先运行coverage run才行 
  • sonar的项目配置文件sonar-project.properties中添加如下配置:
sonar.python.coverage.reportPath=xxxxx 
  • sonar-project.properties文件配置修改说明为:
Property Scope Example Description
sonar.python.coverage.reportPath Project-wide coverage-.xmlt SINCE 1.1 Ant pattern describing the path to coverage reports, relative to projects root. Leave unset to use the default (“coverage-reports/coverage-.xml”).The reports have to conform to the Cobertura XML format.

外部规则

pylint

  • 通过pylint生成覆盖率统计报告(见代码检测工具-python
  • sonar-scanner执行不会运行pylint,可能ant或jenkins集成会运行(未验证)
  • 报告必须直接使用终端报告
pylint xx/xxx.py > xxxx.txt 
  • sonar的项目配置文件sonar-project.properties中添加如下配置:
sonar.go.golint.reportPaths=xx/xx/xx 
  • sonar的项目配置文件sonar-project.properties中添加如下配置:
sonar.python.pylint=/usr/bin/pylint sonar.python.pylint_config=.pylintrc sonar.python.pylint.reportPath=pylint_report.txt 
  • sonar-project.properties文件配置修改说明为:
Property Scope Example Description
sonar.python.pylint System-wide /usr/bin/pylint Path to the pylint executable to use in pylint analysis. Set to empty to use the default one (default is pylint).
sonar.python.pylint_config Project-wide .pylintrc Path to the pylint configuration file (relative to project root or absolute) to use in pylint analysis. Set to empty to use the default.
sonar.python.pylint.reportPath Project-wide pylint-report.txt SINCE 1.4 Ant pattern describing the path to Pylint report, relative to projects root. No default value. The report have to conform to format “{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}”.
  • 其实这些配置也可以直接在sonar管理端配置,但是为了实现’sonarqube做成服务器,使用各机器安装sonar-scanner扩展使用‘所以强烈推荐这个配置写在配置文件中

配置样例展示

sonar.projectKey=nativeapp sonar.projectName=nativeapp sonar.sources=testpython sonar.exclusions=**/*_test.py,**/test_*.py,**/*.xml,**/vendor/** sonar.tests= sonar.test.inclusions=**/*_test.py,**/test_*.py sonar.test.inclusions=**/vendor/** sonar.python.coverage.reportPath=**/coverage.xml 
文章来源: https://blog.csdn.net/baidu_36943075/article/details/90634181
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!