11# CodeQuality
22
3- This project provides an easy way to set up .Net Core [ static code analysis] ( #coding-style-and-analysis ) with pre-configured quality and coding style
3+ This project provides an easy way to set up .NET [ static code analysis] ( #coding-style-and-analysis ) with pre-configured quality and coding style
44rules for your Test and your Prod C# projects.
55
66It also provides:
@@ -14,15 +14,20 @@ It also provides:
1414[ ![ Build - CI] ( https://github.com/xaviersolau/CodeQuality/actions/workflows/build-ci.yml/badge.svg )] ( https://github.com/xaviersolau/CodeQuality/actions/workflows/build-ci.yml )
1515[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( LICENSE )
1616
17- | Package | Nuget.org | Pre-release |
18- | -----------------------------------------| -----------| -------------|
19- |** SoloX.CodeQuality.Prod** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Prod.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Prod ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Prod.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Prod )
20- |** SoloX.CodeQuality.Test** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test )
21- |** SoloX.CodeQuality.Playwright** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Playwright.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Playwright ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Playwright.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Playwright )
22- |** SoloX.CodeQuality.Test.Helpers** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers )
23- |** SoloX.CodeQuality.Test.Helpers.XUnit** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.XUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.XUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit )
24- |** SoloX.CodeQuality.Test.Helpers.NUnit** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.NUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.NUnit ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.NUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.NUnit )
25- |** SoloX.CodeQuality.WebHost** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.WebHost.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.WebHost ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.WebHost.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.WebHost )
17+ | Package | Nuget.org | Pre-release |
18+ | ---------------------------------------------| -----------| -------------|
19+ |** SoloX.CodeQuality.Prod** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Prod.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Prod ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Prod.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Prod )
20+ |** SoloX.CodeQuality.Test** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test )
21+ |** SoloX.CodeQuality.Playwright** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Playwright.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Playwright ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Playwright.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Playwright )
22+ |** SoloX.CodeQuality.Test.Helpers** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers )
23+ |~~ ** SoloX.CodeQuality.Test.Helpers.XUnit** ~~ |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.XUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.XUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit )
24+ |** SoloX.CodeQuality.Test.Helpers.XUnit.V3** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.XUnit.V3.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit.V3 ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.XUnit.V3.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.XUnit.V3 )
25+ |** SoloX.CodeQuality.Test.Helpers.NUnit** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.Test.Helpers.NUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.NUnit ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.Test.Helpers.NUnit.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.Test.Helpers.NUnit )
26+ |** SoloX.CodeQuality.WebHost** |[ ![ NuGet Beta] ( https://img.shields.io/nuget/v/SoloX.CodeQuality.WebHost.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.WebHost ) |[ ![ NuGet Beta] ( https://img.shields.io/nuget/vpre/SoloX.CodeQuality.WebHost.svg )] ( https://www.nuget.org/packages/SoloX.CodeQuality.WebHost )
27+
28+ > Find out the [ Breaking changes] ( documents/BreakingChanges.md ) from one version to another.
29+
30+ > Note that from version 3.0.* the ` SoloX.CodeQuality.Test.Helpers.XUnit ` package is obsolete since [ xUnit.net] ( https://xunit.net ) switched to V3.
2631
2732## License and credits
2833
@@ -42,7 +47,7 @@ This project is not implementing analysis by itself, it is using existing analys
4247
4348
4449** Note that before version 2.1.0 the AnalysisLevel property was set to "latest" resulting in breaking the build just on framework update.**
45- ** So from version 2.1.0 the analysis level is fixed to a specific version (in our case 8 .0)**
50+ ** So from version 2.1.0 the analysis level is fixed to a specific version (currently 10.0 with version 3.0 .0)**
4651
4752* * *
4853
@@ -52,26 +57,26 @@ You can checkout this Github repository or you can use the NuGet package:
5257
5358** Install using the command line from the Package Manager:**
5459``` bash
55- Install-Package SoloX.CodeQuality.Prod -version 2.3.6
60+ Install-Package SoloX.CodeQuality.Prod -version 3.0.0-preview.1
5661or
57- Install-Package SoloX.CodeQuality.Test -version 2.3.6
62+ Install-Package SoloX.CodeQuality.Test -version 3.0.0-preview.1
5863```
5964
6065** Install using the .Net CLI:**
6166``` bash
62- dotnet add package SoloX.CodeQuality.Prod --version 2.3.6
67+ dotnet add package SoloX.CodeQuality.Prod --version 3.0.0-preview.1
6368or
64- dotnet add package SoloX.CodeQuality.Test --version 2.3.6
69+ dotnet add package SoloX.CodeQuality.Test --version 3.0.0-preview.1
6570```
6671
6772** Install editing your project file (csproj):**
6873``` xml
69- <PackageReference Include =" SoloX.CodeQuality.Prod" Version =" 2.3.6 " >
74+ <PackageReference Include =" SoloX.CodeQuality.Prod" Version =" 3.0.0-preview.1 " >
7075 <PrivateAssets >all</PrivateAssets >
7176 <IncludeAssets >runtime; build; native; contentfiles; analyzers</IncludeAssets >
7277</PackageReference >
7378or
74- <PackageReference Include =" SoloX.CodeQuality.Test" Version =" 2.3.6 " >
79+ <PackageReference Include =" SoloX.CodeQuality.Test" Version =" 3.0.0-preview.1 " >
7580 <PrivateAssets >all</PrivateAssets >
7681 <IncludeAssets >runtime; build; native; contentfiles; analyzers</IncludeAssets >
7782</PackageReference >
@@ -218,17 +223,17 @@ You can checkout this Github repository or use the NuGet package:
218223
219224** Install using the command line from the Package Manager:**
220225``` bash
221- Install-Package SoloX.CodeQuality.Playwright -version 2.3.6
226+ Install-Package SoloX.CodeQuality.Playwright -version 3.0.0-preview.1
222227```
223228
224229** Install using the .Net CLI:**
225230``` bash
226- dotnet add package SoloX.CodeQuality.Playwright --version 2.3.6
231+ dotnet add package SoloX.CodeQuality.Playwright --version 3.0.0-preview.1
227232```
228233
229234** Install editing your project file (csproj):**
230235``` xml
231- <PackageReference Include =" SoloX.CodeQuality.Playwright" Version =" 2.3.6 " />
236+ <PackageReference Include =" SoloX.CodeQuality.Playwright" Version =" 3.0.0-preview.1 " />
232237```
233238
234239* * *
@@ -309,8 +314,8 @@ builder.WithLocalHost(localHostBuilder =>
309314 });
310315```
311316
312- > Note that your host must be based on .Net 8.0 and it must use the ` WebApplication.CreateBuilder(args) ` method to build your Web host. If
313- > you still use the hold way with ` Host.CreateDefaultBuilder(args) ` and a ` Startup.cs ` file, you will get an exception like this
317+ > Note that your host must be based on .NET 8.0 or later and it must use the ` WebApplication.CreateBuilder(args) ` method to build your Web host. If
318+ > you still use the old way with ` Host.CreateDefaultBuilder(args) ` and a ` Startup.cs ` file, you will get an exception like this
314319> ` System.InvalidOperationException : Build can only be called once. ` and you won't be able to start your test.
315320
316321On-line host:
@@ -424,29 +429,35 @@ You can checkout this Github repository or you can use the NuGet package:
424429
425430** Install using the command line from the Package Manager:**
426431``` bash
427- Install-Package SoloX.CodeQuality.Test.Helpers -version 2.3.6
432+ Install-Package SoloX.CodeQuality.Test.Helpers -version 3.0.0-preview.1
428433
429- Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 2.3.6
434+ Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 3.0.0-preview.1
430435
431- Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 2.3.6
436+ Install-Package SoloX.CodeQuality.Test.Helpers.XUnit.V3 -version 3.0.0-preview.1
437+
438+ Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 3.0.0-preview.1
432439```
433440
434441** Install using the .Net CLI:**
435442``` bash
436- dotnet add package SoloX.CodeQuality.Test.Helpers --version 2.3.6
443+ dotnet add package SoloX.CodeQuality.Test.Helpers --version 3.0.0-preview.1
444+
445+ dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 3.0.0-preview.1
437446
438- dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 2.3.6
447+ dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit.V3 --version 3.0.0-preview.1
439448
440- dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 2.3.6
449+ dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 3.0.0-preview.1
441450```
442451
443452** Install editing your project file (csproj):**
444453``` xml
445- <PackageReference Include =" SoloX.CodeQuality.Test.Helpers" Version =" 2.3.6 " />
454+ <PackageReference Include =" SoloX.CodeQuality.Test.Helpers" Version =" 3.0.0-preview.1 " />
446455
447- <PackageReference Include =" SoloX.CodeQuality.Test.Helpers.XUnit" Version =" 2.3.6 " />
456+ <PackageReference Include =" SoloX.CodeQuality.Test.Helpers.XUnit" Version =" 3.0.0-preview.1 " />
448457
449- <PackageReference Include =" SoloX.CodeQuality.Test.Helpers.NUnit" Version =" 2.3.6" />
458+ <PackageReference Include =" SoloX.CodeQuality.Test.Helpers.XUnit.V3" Version =" 3.0.0-preview.1" />
459+
460+ <PackageReference Include =" SoloX.CodeQuality.Test.Helpers.NUnit" Version =" 3.0.0-preview.1" />
450461```
451462
452463 * * *
@@ -479,23 +490,68 @@ var response = await httpClient.GetFromJsonAsync<Person>("target");
479490
480491```
481492
493+ * * *
494+
495+ ### Snapshot testing
496+
497+ Snapshot testing is a powerful technique for testing text and image outputs. The ` SnapshotHelper ` class provided in
498+ ` SoloX.CodeQuality.Test.Helpers ` makes it easy to create and maintain snapshot tests.
499+
500+ Snapshots are stored on disk and compared against test outputs. This is useful for testing scenarios such as:
501+ * Text comparisons (JSON, XML, code generation outputs, etc.)
502+ * Image comparisons
503+ * Large data structure validations
504+
505+ #### Basic usage
506+
507+ To use the ` SnapshotHelper ` in your tests, simply create an instance and use its methods to verify outputs:
508+
509+ ``` csharp
510+ using SoloX .CodeQuality .Test .Helpers .Snapshot ;
511+
512+ var snapshotHelper = new SnapshotHelper ();
513+
514+ // For text snapshots
515+ string generatedOutput = GenerateContent ();
516+ await snapshotHelper .CompareTextSnapshotAsync (nameof (MyTest ), generatedOutput );
517+
518+ // For image snapshots
519+ Stream generatedPngImage = GeneratePngImage ();
520+ await snapshotHelper .ComparePngSnapshotAsync (nameof (MyTest ), generatedImage );
521+ ```
522+
523+ #### Configuration
524+
525+ You can configure the ` SnapshotHelper ` with custom options:
526+
527+ ``` csharp
528+ var snapshotHelper = new SnapshotHelper (options =>
529+ {
530+ options .IntermediateFolder = " CustomSnapshotsFolder" ;
531+ options .RootPath = " /path/to/test/project" ;
532+ });
533+ ```
534+
535+ The snapshots are stored in a ` Snapshots ` folder by default and can be reviewed and updated when the expected output changes.
536+
537+ * * *
538+
482539### ILogger\< T> mocking
483540
484541We often need to provide a ILogger\< T> instance to the class we want to test. This is easy to provide a
485542mock of this interface but sometime it is actually useful to be able to write the logs into the test
486543output. In order to write the logs in the test console output you can use the TestLogger available for
487544XUnit and NUnit.
488545
489- #### With XUnit
546+ #### With XUnit or XUnit.V3
490547
491- The XUnit TestLogger is provided in the package ` SoloX.CodeQuality.Test.Helpers.XUnit ` and it can be
492- used like this:
548+ The XUnit TestLogger is provided in the package ` SoloX.CodeQuality.Test.Helpers.XUnit.V3 ` (or
549+ ` SoloX.CodeQuality.Test.Helpers.XUnit ` for XUnit V2) and it can be used like this:
493550
494551``` csharp
495552using Microsoft .Extensions .Logging ;
496- using SoloX .CodeQuality .Test .Helpers .XUnit .Logger ;
553+ using SoloX .CodeQuality .Test .Helpers .XUnit .V3 . Logger ;
497554using Xunit ;
498- using Xunit .Abstractions ;
499555
500556public class LoggerTest
501557{
@@ -524,9 +580,8 @@ some integration tests using a service provider:
524580``` csharp
525581using Microsoft .Extensions .DependencyInjection ;
526582using Microsoft .Extensions .Logging ;
527- using SoloX .CodeQuality .Test .Helpers .XUnit .Logger ;
583+ using SoloX .CodeQuality .Test .Helpers .XUnit .V3 . Logger ;
528584using Xunit ;
529- using Xunit .Abstractions ;
530585
531586public class LoggerTest
532587{
@@ -555,7 +610,6 @@ public class LoggerTest
555610}
556611```
557612
558-
559613#### With NUnit
560614
561615The NUnit TestLogger is provided in the package ` SoloX.CodeQuality.Test.Helpers.NUnit ` and it can be
@@ -603,9 +657,8 @@ public class LoggerTest
603657 var logger = serviceProvider .GetRequiredService <ILogger <LoggerTest >>();
604658
605659 logger .LogError (" This is an error log message!" );
606-
660+
607661 Assert .Pass ();
608662 }
609663}
610664```
611-
0 commit comments