1. 程式人生 > >IOS測試之sonar檢查ios程式碼質量

IOS測試之sonar檢查ios程式碼質量

ios專案

我提供了一個sample的專案,首先git clone該專案到本地。

git clone https://github.com/DoctorQ/ios_test.git
Cloning into 'ios_test'...
remote: Counting objects: 88, done.
remote: Total 88 (delta 0), reused 0 (delta 0), pack-reused 87
Unpacking objects: 100% (88/88), done.
Checking connectivity... done.

安裝OCLint

因為sonar-objective-c使用的是OCLint來掃描程式碼的,所以需要安裝OCLint,使用下面的命令來安裝 (前提是你有brew工具)。

brew install https://gist.githubusercontent.com/TonyAnhTran/e1522b93853c5a456b74/raw/157549c7a77261e906fb88bc5606afd8bd727a73/ocli nt.rb

安裝sonar

看我另外兩篇關於sonar的安裝使用。

其他工具安裝

安裝XCtool

詳細安裝方法參考官網

brew install xctool

安裝gcovr

詳細安裝方法參考官網

brew install gcovr

xcode安裝

直接app store上安裝xcode最新版

新增sonar-objective-c檔案

檔案新增

如果想要分析程式碼,需要在待分析專案根目錄下新增2個檔案:

檔案修改

sonar-project.properties檔案中修改一些配置資訊。詳細的配置說明我已經寫在各個指令碼的註釋上了:

##########################
# Required configuration #
##########################
#sonar伺服器上的訪問地址字尾,比如我們可以設定下面的屬性為ios,就可以通過http://server:port/dashboard/index/ios來訪問該專案的資料
sonar.projectKey=ios #專案的名稱 sonar.projectName=ios #版本號 sonar.projectVersion=1.0 sonar.language=objc # Project description sonar.projectDescription=test for sonar-objective-c # 要檢測的原始碼的目錄 sonar.sources=0403_test # 要檢測的測試原始碼目錄,如果沒有註釋掉即可 # sonar.tests=testSrcDir # Xcode project configuration (.xcodeproj or .xcworkspace) # -> If you have a project: configure only sonar.objectivec.project # -> If you have a workspace: configure sonar.objectivec.workspace and sonar.objectivec.project # and use the later to specify which project(s) to include in the analysis (comma separated list) #專案根目錄下的xcodeproj的名稱 sonar.objectivec.project=0403_test.xcodeproj # sonar.objectivec.workspace=myApplication.xcworkspace # 應用的scheme資訊,如果不知道沒關係,先執行該指令碼,它會提示你當前專案的scheme資訊 sonar.objectivec.appScheme=0403_test # 同上,只是用於測試,如果沒有就直接註釋掉 # sonar.objectivec.testScheme=myApplicationTests ########################## # Optional configuration # ########################## # Encoding of the source code sonar.sourceEncoding=UTF-8 # JUnit report generated by run-sonar.sh is stored in sonar-reports/TEST-report.xml # Change it only if you generate the file on your own # The XML files have to be prefixed by TEST- otherwise they are not processed # sonar.junit.reportsPath=sonar-reports/ # Cobertura report generated by run-sonar.sh is stored in sonar-reports/coverage.xml # Change it only if you generate the file on your own # sonar.objectivec.coverage.reportPattern=sonar-reports/coverage*.xml # OCLint report generated by run-sonar.sh is stored in sonar-reports/oclint.xml # Change it only if you generate the file on your own # sonar.objectivec.oclint.report=sonar-reports/oclint.xml # Paths to exclude from coverage report (tests, 3rd party libraries etc.) # sonar.objectivec.excludedPathsFromCoverage=pattern1,pattern2 sonar.objectivec.excludedPathsFromCoverage=.*Tests.* # Project SCM settings # sonar.scm.enabled=true # sonar.scm.url=scm:git:https://...

執行sonar檢測

命令列定位到專案根目錄下,執行sh run-sonar.sh命令

sh run-sonar.sh
Running run-sonar.sh...
-n Extracting Xcode project information
-n .

-n .

Skipping tests as no test scheme has been provided!
-n Running OCLint...

-n Running SonarQube using SonarQube Runner
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .
-n .

執行成功後,檢視結果如下:

這裡寫圖片描述

DONE!