Very important thing is for any developer to write good code without any bugs and easy to understandable to other developers.
I'm not going to talk about coding standard and coding style because it's all depend on company standard and developer to developer.
Today, I'm more talk about Asp.Net Core Unit Test case Code Coverage.
I assumed you already know how to write xUnit test case in asp.net core. If you want to know about xUnit test case then please read this blog
For code coverage, I'm using OpenCover and for the report using ReportGenerator tool.
For both the mentions, OpenCover and ReportGenerator added into project.json file under the Test project.
{ "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "Microsoft.EntityFrameworkCore.InMemory": "1.1.0", "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029", "NETStandard.Library": "1.6.0", "InMemoryDatabaseAspNetCore.Model": "1.0.0-*", "InMemoryDatabaseAspNetCore": "1.0.0-*", "OpenCover": "4.6.519", "ReportGenerator": "2.5.2" }, "frameworks": { "netcoreapp1.1": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" } } } } }Once you added both the package on project.json file. Now time to create one batch file with below code and place at Test project.
@echo off SET dotnet="C:/Program Files/dotnet/dotnet.exe" SET opencover="C:\Users\%USERNAME%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" SET reportgenerator="C:\Users\%USERNAME%\.nuget\packages\ReportGenerator\2.5.2\tools\ReportGenerator.exe" SET targetargs="test" SET filter="+[*]InMemoryDatabaseAspNetCore.* -[.Model]* -[InMemory.Test]* -[.Startup]* -[.Test]* -[xunit.*]* -[FluentAssertions]* -[crypto]*" SET coveragefile=Coverage.xml SET coveragedir=Coverage REM Run code coverage analysis %opencover% -oldStyle -register:user -target:%dotnet% -output:%coveragefile% -targetargs:%targetargs% -filter:%filter% -skipautoprops -hideskipped:All REM Generate the report %reportgenerator% -targetdir:%coveragedir% -reporttypes:Html;Badges -reports:%coveragefile% -verbosity:Error REM Open the report start "report" "%coveragedir%\index.htm"On above batch file, you can set all variables as per your system and project path.
Once setup variables then one important point are filters. Filters are that which mentions the which dll are included\excluded for code coverage.
SET filter="+[*]InMemoryDatabaseAspNetCore.* -[.Model]* -[InMemory.Test]* -[.Startup]* -[.Test]* -[xunit.*]* -[FluentAssertions]* -[crypto]*"
After all the set, you are ready to run the batch file and it will show the result into the browser and it look like below. Green and red color based on your code coverage.
Hope you like it!!
0 comments:
Post a Comment