(適用于DOTNET)
在專案中,我們的CI 分成三類:PR CI, Fast CI和QA CI, 其中QA CI中我們不僅要使用Sonar Qube來檢查代碼規范,還運行單元測驗并查看代碼覆寫率,但是因為專案中參考了一些其他的類別庫, 這樣在統計代碼覆寫率的時候,也會一并算入進來,對統計結果造成了影響,
因此,我們需要指定代碼覆寫率的檢查范圍,使用runsettings檔案就是一個不錯的方法,
我們先看看幫助說明
> dotnet test --help Options: -h, --help Show command line help. -s, --settings <SETTINGS_FILE> The settings file to use when running tests. -t, --list-tests List the discovered tests instead of running the tests. ……
其中 -s 開關就是指定runsettings檔案的
關于runsettings檔案的介紹, 可以參考《使用 .runsettings 檔案配置單元測驗》,
本專案中使用的runsettings檔案內容如下
1 <?xml version="1.0" encoding="utf-8"?> 2 <RunSettings> 3 <!-- Configurations that affect the Test Framework --> 4 <RunConfiguration> 5 <MaxCpuCount>4</MaxCpuCount> 6 <BatchSize>4</BatchSize> 7 <TestCaseFilter>TestCategory=passed</TestCaseFilter> 8 </RunConfiguration> 9 <DataCollectionRunSettings> 10 <DataCollectors> 11 <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> 12 <Configuration> 13 <CodeCoverage> 14 <ModulePaths> 15 <Include> 16 <ModulePath>.*myproj\..*\.dll$</ModulePath> 17 <ModulePath>.*\.exe$</ModulePath> 18 </Include> 19 <Exclude> 20 <ModulePath>.*myproj\.common.*\.dll$</ModulePath> 21 <ModulePath>.*myproj\.data.*\.dll$</ModulePath> 22 </Exclude> 23 </ModulePaths> 24 <CollectAspDotNet>False</CollectAspDotNet> 25 </CodeCoverage> 26 </Configuration> 27 </DataCollector> 28 </DataCollectors> 29 </DataCollectionRunSettings> 30 </RunSettings>
其中 include節點中是指要包括的檔案, exclude中是指不包括的檔案,應該很好理解,需要注意的是點號需要轉義成“\.” , 星號需要轉義成".*"
然后就可以使用命令執行單元測驗,并收集代碼覆寫率了,
dotnet test $(TestProjectPath) -s $(RunSettings) --filter "TestCategory=passed"
代碼覆寫率的結果會被管道自動收集,并且在結果頁上提供下載,

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/288702.html
標籤:其他
