座机 发表于 2018-1-12 14:18:20

DotNetCore跨平台~xUnit生成xml报告

  在CI/CD流行至极的今天,你的项目没有自动化测试绝对是不可以接受的,在进行自动化部署和持续集成时,我们的dotnet core项目也是可以实现自动化的,之前说过gitlab,jenkins对持续集成的影响,而今天说一下微观点的概念,“单元测试”,这个早已不是什么新东西了,像dotnet平台也有自己的测试工具,不过在进入core平台之后,大叔还是推荐大家使用xunit工具,这个工具可以帮助我们升级测试报告,以XML文件存在!
  1 相关nuget包
  可以通过查看项目的csproj文件获得代码
  

<Project Sdk="Microsoft.NET.Sdk">  
<PropertyGroup>
  
<TargetFramework>netcoreapp2.0</TargetFramework>
  
<IsPackable>false</IsPackable>
  
</PropertyGroup>
  
<ItemGroup>
  
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
  
<PackageReference Include="xunit" Version="2.3.1" />
  
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
  
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
  
</ItemGroup>
  
</Project>
  

  2 windows/linux平台自动化测试命令
  

dotnet restore&& dotnet xunit -xml test.xml  

  注意如果你的nuget包里,包括了私有仓库包,需要在nuget.config里添加包的引用地址,否则咱们的nuget包只会从官方进行下载
  

<?xml version="1.0" encoding="utf-8"?>  
<configuration>
  
<packageSources>
  
<add key="nugetlocal" value="http://111.111.111.111:9090/nuget" />
  
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  
</packageSources>
  
<packageRestore>
  
<add key="enabled" value="True" />
  
<add key="automatic" value="True" />
  
</packageRestore>
  
<bindingRedirects>
  
<add key="skip" value="False" />
  
</bindingRedirects>
  
<packageManagement>
  
<add key="format" value="0" />
  
<add key="disabled" value="False" />
  
</packageManagement>
  
</configuration>
  

  3 查看xml的测试报告
https://images2017.cnblogs.com/blog/118538/201712/118538-20171222223620881-1940939461.png
  4 和gitlab的pipeline进行结合
  事实上,我们的xunit也可以和gitlab的管道结合在一起使用,当你的代码迁入后,自动进行测试并生成报告!
https://images2017.cnblogs.com/blog/118538/201712/118538-20171222223904662-509487632.png
https://images2017.cnblogs.com/blog/118538/201712/118538-20171222223926084-972447552.png
  相关gitlab-ci的介绍,我们看大叔这篇文件!《Git~GitLab当它是一个CI工具时》
  感谢各位的阅读!
页: [1]
查看完整版本: DotNetCore跨平台~xUnit生成xml报告