This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021 “.
.net Core platform testing framework has several options, built-in MSTest framework, NUnit framework and powerful xUnit framework, relying on the universe’s strongest editor Visual Studio, you can carry out very friendly testing and happy to show the results of the test. If that’s all, then this article is superfluous to you. But if you want to get out of the comfort zone of VisualStudio and produce HTML reports that you can publish and view freely on your own, then maybe this article will help you save a lot of time and get rid of the frustration completely and have a fun ride.
- 📢 welcome to like: 👍 collect ⭐ message 📝 if there are mistakes please correct, give people rose, hand left lingering fragrance!
- 📢 This article was originally published by Webmote and published by CSDN.
- 📢 author’s motto: life is toss about, when you don’t toss about life, life will start to toss about you, let us come on together! 💪 💪 💪
1. Get started with xUnit testing
Use the wizard to generate a new xUnit test project.Of course, you can also modify a class library file by referring to it in the Project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="Against 2.4.1." />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Copy the code
Writing xUnit test cases is super easy, just adding declarations [Fact] to class functions.
About how to write unit tests, you can refer to the [about unit testing, you don’t know the things]. (blog.csdn.net/codeex/arti…
All right, we’re ready to start our report generation journey.
2. Code Coverage
According to Microsoft’s documentation, data collection was added to test platform 15.3 support, which is integrated with VS 2017 15.3+ and dotnet-CLI 2.0.0+. With the data collection extension, session and test case events can be analyzed. You can extend this to measure coverage, cyclomatic complexity, and other metrics.
Collect tests for coverage, etc., using the following command:
dotnet test --collect "Code Coverage"
Copy the code
On Windows, you can collect Code Coverage using the — Collect “Code Coverage” option. This option generates the “. Coverage “file, which can be opened in Visual Studio 2019 Enterprise.
On Linux, of course, the response to this command is:
Data collector “Code Coverage” message: No Code Coverage data is available. Code coverage is currently supported only on Windows.
To collect Code Coverage on any platform supported by.NET Core, install Coverlet and use the — Collect :”XPlat Code Coverage” option.
Coverlet package support is already installed in the example given in section 1.
So we can execute the following command to generate coverage.cobertura.xml
dotnet test --collect:"XPlat Code Coverage"
Copy the code
Please wait while test execution is starting…
A total of 1 test files match the specified pattern. Attachment: / root/dotnet/TestResults / 93 b42aca – e39 c0e7-4-9253 – c176506b3be0 / coverage cobertura. XML has passed! – Failed: 0, passed: 1, skipped: 0, total: 1, duration: < 1 ms
The results are stored in a randomly generated GUID folder in the TestResults folder.
This should be a dotnet-CLI limitation. The collected results cannot be specified as an exact folder, so don’t bother looking for parameters.
Note: If you want to integrate into an automation tool, you need to consider how to copy files from this random folder. One way is to use the shell’s find/grep command to search for the text output of the command to find the full output path.
With this XML file andReportGeneratorTool, we can generate reports on test coverage and cyclomatic complexity.
reportgenerator -reports:coverage.cobertura.xml -targetdir:report
Copy the code
This command produces a bunch of HTML, CSS, and JS files. Don’t panic, just go to index.html and open it.
Cool reports are presented naked in front of you.
3. Test Results
The test results report that it’s easy or easy, and when it says it’s complicated, it’s very complicated.
Why do you say that?
This is mainly because dotnet-CLI already has its own output for reporting results.
For example, the following command (in the project directory) outputs simple statistics:
dotnet test
Copy the code
Starting test execution, please wait…
A total of 1 test files matched the specified pattern. Passed! Deflected: 0, Passed: 3, Skipped: 0, Total: 3, Duration: 3 ms-myxunittest.dll (net5.0).
Yes, if that’s enough, then it’s pretty simple, isn’t it?
Of course, if you want a more detailed output, this is also supported, we use the console to collect information, command:
dotnet test -l "console; verbosity=detailed"Copy the code
The output is as follows:
Starting test execution, please wait…
A total of 1 test files matched the specified pattern. D: github\xunit-xml2html\MyXunitTest\bin\Debug\net5.0\ myxUnittest. DLL [xUnit.net 00:00:00.00] xUnit.net VSTest Adapter V2.4.3 + 1b45F5407b (64-bit.net 5.0.11) [xUnit.net 00:00:00.24] Livio: MyXunitTest [xUnit.net 00:00:00.27] Discovered: MyXunitTest [xUnit.net 00:00:00.27] MyXunitTest [xUnit.net 00:00:00. 32] Finished: MyXunitTest Passed MyXunitTest.UnitTest1.Test2 [1 ms] Passed MyXunitTest.UnitTest1.Test1 [< 1 ms] Passed MyXunitTest.UnitTest1.Test3 [< 1 ms]
Test Run Successful.
Total tests: 3 Passed: 3 Total time: 1.1061 Seconds
Well, it’s still dotnet-CLI, so if you have to have an HTML report, there’s a problem.
3.1 Value of TRX format
TRX test results, it is estimated that only Microsoft can understand it, so the output results can be opened through VisualStudio.
Well, yes, you can share it with colleagues who have VS installed. Command:
dotnet test --logger trx
Copy the code
One good thing about this result file is that it does not require the enterprise version of VS.
There are many trX-to-HTML tools on GitHub that are crippled by age and disrepair.
For example, TrxerConsole and trx2HTML, if you like to play around with friends, you can come down to see, trx2HTML can still generate half of HTML, the interface is very beautiful, but unfortunately compatibility has been very poor, if there is a master maintenance, should still be a good tool.
There are others that are not mainstream, almost all of them useless.
3.2 Output to XML file
Now that TRX is dead in the water, we have to go back to XML.
Then came the moment of truth. Here you need to import a package, xunitxml. TestLogger, to the Github address.
It supports NUnit and Junit and is a very good XML generation tool.
The XML generated by the Xunit Logger is Xunit V2 format.
The command is as follows:
dotnet test --logger:"xunit; LogFilePath=result.xml"Copy the code
The output path is where you hit it.
Generating from XML to HTML isn’t that hard. You can write your own conversion tool.
There is, of course, a ready-made conversion tool: xunit-xml2html.
With tools, you can easily generate HTML from XML.
Yeah, yeah, we’re done. Everything’s taken care of.
3.3 the DEPs. json file is Missing when.net Core tests the Web
Sometimes, the project will be prompted to threw the exception: System. InvalidOperationException: Can’t find'[path removed]\TestRepro.Tests\bin\Debug\TestRepro.deps.json’.
This may be due to the lack of reference to the package, this error is very subtle, CI time occurrence rate is very high.
Microsoft.AspNetCore.Mvc.Testing
Copy the code
4. Summary
Net Core test report section is finished, you should be able to give the boss a satisfactory answer sheet?
👓 have seen this, but also care about a thumbs-up?
👓 has been liked, but also care about a collection?
👓 are collected, but also care about a comment?