Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dimitrietataru/csharp-unit-testing
.NET - Unit Testing
https://github.com/dimitrietataru/csharp-unit-testing
arrange-act-assert asp-net-core-web-api assertions bogus code-coverage coverage-report csharp dot-net-core fluentassertions given-when-then moqui-framework net-5 net-core-3-1 nunit-framework reportgenerator shouldly unit-testing xunit-framework
Last synced: 16 days ago
JSON representation
.NET - Unit Testing
- Host: GitHub
- URL: https://github.com/dimitrietataru/csharp-unit-testing
- Owner: dimitrietataru
- License: mit
- Created: 2020-02-04T10:59:59.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-15T00:04:45.000Z (about 4 years ago)
- Last Synced: 2024-11-16T20:50:25.315Z (3 months ago)
- Topics: arrange-act-assert, asp-net-core-web-api, assertions, bogus, code-coverage, coverage-report, csharp, dot-net-core, fluentassertions, given-when-then, moqui-framework, net-5, net-core-3-1, nunit-framework, reportgenerator, shouldly, unit-testing, xunit-framework
- Language: C#
- Homepage:
- Size: 286 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# C# Unit Testing
![Coverage](https://github.com/dimitrietataru/csharp-unit-testing/blob/master/Coverage.png)
## Table of contents
* [NuGet packages](#nuget-packages)
* [Syntax](#syntax)
* [Given-When-Then](#given-when-then-pattern)
* [Arrange-Act-Assert](#arrange-act-assert-pattern)
* [Moq](#moq-setup-and-verify)
* [Assertions](#assertions)
* [Collect code coverage](#collect-code-coverage)
* [Generate code coverage report](#generate-code-coverage-report)## NuGet packages
- **Frameworks**
- [NUnit](https://www.nuget.org/packages/NUnit)
- [xUnit](https://www.nuget.org/packages/xunit)
- **Mocking**
- [Moq](https://www.nuget.org/packages/Moq)
- **Code coverage**
- [coverlet.collector](https://www.nuget.org/packages/coverlet.collector)
- [ReportGenerator](https://www.nuget.org/packages/ReportGenerator)
- **Utils**
- [Bogus](https://www.nuget.org/packages/Bogus)
- [Shouldly](https://www.nuget.org/packages/shouldly)
- [FluentAssertions](https://www.nuget.org/packages/FluentAssertions)``` powershell
PM> Install-Package Bogus -Version 32.0.2
PM> Install-Package coverlet.collector -Version 1.3.0
PM> Install-Package FluentAssertions -Version 5.10.3
PM> Install-Package Microsoft.NET.Test.Sdk -Version 16.8.3
PM> Install-Package Moq -Version 4.15.2
PM> Install-Package ReportGenerator -Version 4.8.1
PM> Install-Package Shouldly -Version 4.0.1# NUnit
PM> Install-Package NUnit -Version 3.12.0
PM> Install-Package NUnit3TestAdapter -Version 3.17.0# xUnit
PM> Install-Package xunit -Version 2.4.1
PM> Install-Package xunit.runner.visualstudio -Version 2.4.3
```## Syntax
### Given-When-Then pattern
[Martin Fowler's blog](https://www.martinfowler.com/bliki/GivenWhenThen.html)### Arrange-Act-Assert pattern
[Docs.Microsoft](https://docs.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2019#write-your-tests)### Moq setup and verify
``` csharp
// Setup async method when it returns data
mockService
.Setup(_ => _.GetAsync(It.IsAny()))
.ReturnsAsync(It.IsAny())
.Verifiable();// Setup async method when it executes without a return type
mockService
.Setup(_ => _.RunAsync())
.ReturnsAsync(Task.CompletedTask)
.Verifiable();// Setup async method when it throws an exception
mockService
.Setup(_ => _.RunAsync(It.IsAny()))
.Throws()
.Verifiable();// Verify all service methods, marked as 'Verifiable'
mockService.VerifyAll();// Verify individual service method
mockService.Verify(_ => _.GetAsync(It.IsAny()), Times.Once);
mockService.Verify(_ => _.RunAsync(), Times.Exactly(2));
mockService.Verify(_ => _.RunAsync(It.IsAny()), Times.Never);
```### Assertions
``` csharp
// NUnit
Assert.That(result, Is.InstanceOf());
var apiResponse = result as OkObjectResult;
Assert.That(apiResponse.StatusCode, Is.EqualTo((int)HttpStatusCode.OK));// xUnit
var apiResponse = Assert.IsType(result);
Assert.Equal((int)HttpStatusCode.OK, apiResponse.StatusCode);// FluentAssertions
result
.Should().BeOfType("because we return content")
.Which.StatusCode.Should().Be((int)HttpStatusCode.OK);
// Shouldly
result.ShouldSatisfyAllConditions(
() => result.ShouldBeOfType(),
() => (result as OkObjectResult).StatusCode.ShouldBe((int)HttpStatusCode.OK));
```## Collect code coverage
### Install *coverlet.collector* NuGet package
``` powershell
PM> Install-Package coverlet.collector -Version 1.3.0
```### *TestProject.csproj* configuration
``` xmlall
runtime; build; native; contentfiles; analyzers; buildtransitive```
### Collector configuration. Add a file (e.g. *runsettings*) anywhere in the solution/project
``` xml
json,cobertura
true
false
**/**/Program.cs,**/**/Startup.cs,**/Dir/SubDir/*.cs
```
### Run tests and collect data
``` powershell
PM> dotnet test --collect:"XPlat Code Coverage" --settings .\runsettingsPM> dotnet test .\Specific.Test.Project --collect:"XPlat Code Coverage" --settings .\Specific.Test.Project\runsettings
```## Generate Code Coverage Report
### Install *ReportGenerator* NuGet package
``` powershell
PM> Install-Package ReportGenerator -Version 4.8.1
```### Generate reports
[ReportGenerator usage page](https://danielpalme.github.io/ReportGenerator/usage.html)
``` powershell
PM> dotnet $(UserProfile)\.nuget\packages\reportgenerator\x.y.z\tools\netcoreapp3.0\ReportGenerator.dll "-reports:coverage.xml" "-targetdir:coveragereport" -reporttypes:Html
PM> dotnet $(UserProfile)\.nuget\packages\reportgenerator\x.y.z\tools\netcoreapp3.0\ReportGenerator.dll "-reports:coverage.xml" "-targetdir:coveragereport" -reporttypes:Badges
```
``` powershell
# CSharp.UnitTesting.Api NUnit
PM> dotnet $(UserProfile)\.nuget\packages\reportgenerator\4.8.1\tools\netcoreapp3.0\ReportGenerator.dll "-reports:CSharp.UnitTesting.Api.Nunit.Test\TestResults\*\coverage.cobertura.xml" "-targetdir:_CoverageReport\Api\NUnit" "-historydir:_CoverageReport\_History\Api\NUnit" -reporttypes:Html# CSharp.UnitTesting.Api xUnit
PM> dotnet $(UserProfile)\.nuget\packages\reportgenerator\4.8.1\tools\netcoreapp3.0\ReportGenerator.dll "-reports:CSharp.UnitTesting.Api.Xunit.Test\TestResults\*\coverage.cobertura.xml" "-targetdir:_CoverageReport\Api\xUnit" "-historydir:_CoverageReport\_History\Api\xUnit" -reporttypes:Html
```