SonarQube series Second, analyze dotnet core / C # Code

[Introduction]

This series focuses on installation and deployment sonarqube how to integrate jenkins automated analysis .netcore project. Contents are as follows:

  1. SonarQube a series, Linux installation and deployment
  2. SonarQube series Second, analyze dotnet core / C # Code
  3. SonarQube series of three, automated analysis of how to achieve integration jenkins

[] Function realization

  The main contents of this article will be introduced as follows:

  1. C # code analysis .netcore project under sonarqube
  2. sonarqube test report generating unit (code coverage)

[SonarQube C # code analysis]

1.sonarqube account token generation

sonarqube supports the generation of user token, so the command line or a script using the token on behalf of account operation sonarbue, to avoid disclosure of account passwords.

Click on the upper right corner sonarqube Home avatar into my account

Then enter the security tab page, just enter a logo, click Generate to generate a unique token account

The token is generated just once, and follow-up can not query, so put him back down manually, follow-up will be used.

2. Installation netcore analyzer

Analysis netcore project, in collaboration with Microsoft and sonar done a lot of work, greatly simplifying the tools we use, the official website can view the tools and commands: https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for- msbuild /

According to the official prompts us to find MSBuild .NET Core Global Tool, installed directly dotnet global tool

dotnet tool install --global dotnet-sonarscanner --version 4.3.1

After installation, we put our sonar token is injected into the configuration of the command, so as to automatically associate a corresponding sonar account when executing the command.

In the installation directory dotnet tool to find a configuration file called SonarQube.Analysis.xml.

My xml in the directory: /root/.dotnet/tools/.store/dotnet-sonarscanner/4.6.2/dotnet-sonarscanner/4.6.2/tools/netcoreapp2.1/any

Then we will sonarweb in the corresponding node address and fill out the token on to.

Of course, if no configurations are possible, then you need to fill in as a parameter with the token each time a command is executed, certainly not as good a convenient one-time configuration.

3. Start Analysis Code

First, just to find a project, this is not to say. With the code and then later into the code directory, in turn enter the following command:

Start command, the following command has three parameters Note the use of

dotnet sonarscanner begin / k: where only code to be generated to fill SonarQube item / n: project name will be displayed sonarqube / v: the currently executing active numbers (or a timestamp can be increased dynamically)

The full path to the compiler command, build to dotnet core argument behind the project xxx.sln file

dotnet build xxx.csproj

Analysis and results pushed sonarqube site

dotnet sonarscanner end 

E.g:

dotnet sonarscanner begin /k:SevenTiny.Bantina /n:SevenTiny.Bantina /v:11

dotnet build 20-Solution/SevenTiny.Bantina.sln

dotnet sonarscanner end 

After a while, you can see the push to enter a log sonarqube site has been successful, then you can go sonarqube site to view the results of the project will be created automatically by api.

上述步骤并不会有覆盖率,先忽略,我们后面会继续讲解如何提供单元测试覆盖率。

我们点击标题进入项目详情页:

这里主要报告了几个指标(sonar有默认标准,如果不达标会报警,例如顶端红色错误提示):

Bugs 漏洞:代码中的重大漏洞,可能影响到项目的正常运行,急需改正;

异味:无关紧要的编码不规范问题,建议改正,一般不会影响功能,点开详细信心可以看到规范的使用案例,对规范自己的变成水平有相当大的帮助;

覆盖率:项目的单元测试情况;

重复:项目中的重复代码块,建议重构;

sonarqube 还提供了很多图表来展示多维度的代码分析情况。

【添加单元测试信息】

经过上述步骤并不能实现单元测试的结果展示,事实上,sonarqube 本身也办不到对单元测试的分析,通常的做法是将其他工具生成的单元测试结果整合到 sonarqube 中,借助 sonarqube 来更好地展示单元测试结果。

首先我们的项目要有专门的单元测试项目,并且规范地使用 Assert 等单元测试语句对项目代码进行了单元测试。

1.使用 coverlet 分析单元测试覆盖率

在单元测试项目安装 coverlet.msbuild nuget包(单测项目,不是正式的项目),这种方式便于和dotnet test命令集成。这种方式下, 当它被启用后, 它会集成到dotnet test 这个命令架构里, 在测试运行后自动生成覆盖率报告.

2.通过 dotnet test 命令输出单元测试结果

dotnet test 是 dotnet 默认的集成工具,用于执行单元测试项目并输出测试结果。

执行命令

dotnet test xxxtest.csproj --logger:"trx;LogFileName=test.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput='./TestResults/'

上述命令指定了某个测试项目,并输出测试结果为 test.trx 文件,同时输出单元测试覆盖率分析文件为 opencover 格式的文件。

也就是说,上述命令会生成两个文件,一个是单测的结果,一个是单测覆盖率的详情描述 xml 文件。

3.将单测文件集成到sonarqube

打开 sonarqube 的单个项目设置(不是全局设置)

然后找到对应语言的页签,当然我们今天的语言是C#,找到单元测试覆盖率 opencover 的设置区域,用通配符 ** 去匹配生成的单测覆盖率文件。

还有单元测试报告文件设置

这样下次执行 dotnet sonarscanner 命令的时候,就会自动把单元测试的相关文件输出结果也推送到 sonarqube 中。

最后的结果也就是图中展示的那样,显示出了项目代码的单元测试覆盖率,甚至是本次有提交新代码的时候新代码的单元测试覆盖率,如果低于阈值(默认80%)则会出现上面的报警:

点进单元测试覆盖率详情页,我们可以看到更加详细地对单元测试的描述,例如覆盖了多少行代码,漏掉了多少行代码:

点开具体的代码行还可以看到那些行有单元测试的覆盖,总之是非常详尽了...

【扩展】

SonarQube 如何排除不需要分析地代码文件?

有些时候,引入了很多第三方的库(尤其是web项目的jquery库),我们并不需要对第三方引用的库进行分析(甚至会因为包含的杂七杂八的的各种语言脚本太多),那么我们就要将无关的代码排除。

 打开单独项目的设置(非全局设置)

找到排除,可以采用通配符排除文件或文件夹,下面有通配符的使用介绍。

当然了也可以指定哪些文件是需要包含的,这样可以按需进行分析。

【总结】

使用 sonarqube 分析dotnet core/C#代码的全部过程已经完成了,下一章我们会将过程中的所有命令脚本化,使其便于集成CI/CD工具,也便于脚本通用化,方便地应用到多个项目中。

如有任何疑问,欢迎在评论区讨论~

Guess you like

Origin www.cnblogs.com/7tiny/p/11342902.html