Skip to content

Commit

Permalink
Updated information on WebApplicationFactory issue (#865) d4672a8
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Dec 4, 2023
1 parent ea230d6 commit 516f09d
Show file tree
Hide file tree
Showing 3 changed files with 672 additions and 667 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ <h4 id="why-cant-my-tests-target-net-standard">Why can't my tests target .NET St
<h4 id="my-tests-arent-showing-up-in-visual-studio-2022-or-later">My tests aren't showing up in Visual Studio 2022 or later</h4>
<ul>
<li>Are you using the NuGet adapter package?</li>
<li>Are you using version 4.5.0 or newer of the NuGet package?</li>
<li>Are you using version 4.5.0 or newer of the NuGet adapter package?</li>
<li>Do your tests target .NET Core or the full .NET Framework? (see above)</li>
<li>Have you added a Package Reference to <code>Microsoft.NET.Test.Sdk</code>?</li>
<li>Have you restarted Visual Studio? It is still a bit temperamental.</li>
Expand All @@ -151,6 +151,11 @@ <h4 id="my-tests-arent-showing-up-in-visual-studio-2022-or-later">My tests aren'
</ul>
<pre><code class="lang-csharp"> public partial class Program { }
</code></pre>
<p>The reason for this is that the generated/hidden Minimal API (Top Level statement) Program class is internal.
By declaring the partial part, it is changed to public.
See
<a href="https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-8.0#project-file-csproj">Microsoft docs</a>
for more info, and some alternatives.</p>
<h4 id="my-tests-multi-target-net-core-and-net-framework-why-cant-i-run-both-in-visual-studio">My tests multi-target .NET Core and .NET Framework, why can't I run both in Visual Studio</h4>
<p>This was a limitation in earlier versions of Visual Studio, but is fixed in Visual Studio 2022.</p>
<p><em>If</em> you must run an earlier version and have this issue, you can run specific tests using the <code>--framework</code> command
Expand Down
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,7 @@
"articles/nunit/getting-started/dotnet-core-and-dotnet-standard.html": {
"href": "articles/nunit/getting-started/dotnet-core-and-dotnet-standard.html",
"title": ".NET Core | NUnit Docs",
"keywords": ".NET Core More information and getting started tutorials are available for NUnit and .NET Core targeting C#, F# and Visual Basic in the .NET Core documentation's Unit Testing in .NET Core page. The other information on this page is older documentation. If you follow the instructions in the Installation section your project will work with .NET Core. The test projects have to be .NET (Core) or .NET Framework; .NET Standard can't be used as a test project, since it can't be run on its own, but any code in a .NET Standard library can be tested from a .NET (Core) or .NET Framework test project. TL;DR Adding the adapter and Microsoft.NET.Test.Sdk version 17.8.0 or greater to your NUnit test projects will also enable the dotnet test command for .NET Core projects. Any tests using the new style CSPROJ format, either .NET Core or .NET 4.x, need to add a PackageReference to the NuGet package Microsoft.NET.Test.Sdk. Your test assemblies must also be .NET Core or .NET 4.x, not .NET Standard. You can create a new NUnit test project using dotnet new nunit. It will create an ItemGroup in the csproj file with the necessary references. <ItemGroup> <PackageReference Include=\"Microsoft.NET.Test.Sdk\" Version=\"17.8.0\" /> <PackageReference Include=\"NUnit\" Version=\"4.0.1\" /> <PackageReference Include=\"NUnit3TestAdapter\" Version=\"4.5.0\" /> <PackageReference Include=\"NUnit.Analyzers\" Version=\"3.10.0\" /> <PackageReference Include=\"coverlet.collector\" Version=\"6.0.0\" /> </ItemGroup> .NET Core test can be run on the command line with dotnet test, for example, From the solution root folder dotnet test or from the test project folder dotnet test Or you can specify the csproj file you want to test dotnet test .\\test\\NetCore10Tests\\NetCore10Tests.csproj For a more complete walk-through, please see Testing .NET Core with NUnit in Visual Studio 2017 Using the NUnit project templates The NUnit test project templates come included with dotnet. You can run dotnet new nunit to create an NUnit test project. FAQ Why can't my tests target .NET Standard Visual Studio and VSTest require that the tests target a specific platform. .NET Standard is like a Portable library in that it does not target any specific platform, but can run on any supported platform. Microsoft decided that your tests should be compiled to target a platform so they know which platform to run your tests under and you get the behavior you expect for the platform you are targeting. You can however target multiple platforms in your tests and compile and run each from the command line. It still only runs one platform from Visual Studio, but I would hope that is being worked on. I haven't tested 15.3 yet. It is similar to a console application, it cannot be .NET Standard, it must target a platform, .NET Core or .NET Framework. This limitation is the same for all test adapters including xUnit and MSTest2. My tests aren't showing up in Visual Studio 2022 or later Are you using the NuGet adapter package? Are you using version 4.5.0 or newer of the NuGet package? Do your tests target .NET Core or the full .NET Framework? (see above) Have you added a Package Reference to Microsoft.NET.Test.Sdk? Have you restarted Visual Studio? It is still a bit temperamental. Are you doing integration testing with Asp.Net Core WebApplicationFactory and using minimal API, and it complains about missing testhost.deps.json? Then you're accessing the wrong Program class in your WebApplicationFactory. Fix it by adding a code line at the end of the Program.cs file, and verify that your WebApplicationFactory is actually using this Program class: public partial class Program { } My tests multi-target .NET Core and .NET Framework, why can't I run both in Visual Studio This was a limitation in earlier versions of Visual Studio, but is fixed in Visual Studio 2022. If you must run an earlier version and have this issue, you can run specific tests using the --framework command line option of dotnet test How do I produce a test results file dotnet test can generate an NUnit3 test result file by adding a runsettings property. The property to add is TestOutputXml. This generation is done using the NUnit Engine report service, and produce the same result as the NUnit3-console. This is available through the NUnit3TestAdapter. You run it by adding the setting on the command line (or in a runsettings file): dotnet test -- NUnit.TestOutputXml=yourfoldername The folder is relative to a base folder determined by the OutputXmlFolder settings, or you can use an absolute path. The latter is useful in CI scenarios. Alternatively, there is a 3rd party package, NUnitXml.TestLogger which also produces a NUnit3 xml format. Details for use see here. Note that this is a re-implementation of the NUnit3 format, and may differ."
"keywords": ".NET Core More information and getting started tutorials are available for NUnit and .NET Core targeting C#, F# and Visual Basic in the .NET Core documentation's Unit Testing in .NET Core page. The other information on this page is older documentation. If you follow the instructions in the Installation section your project will work with .NET Core. The test projects have to be .NET (Core) or .NET Framework; .NET Standard can't be used as a test project, since it can't be run on its own, but any code in a .NET Standard library can be tested from a .NET (Core) or .NET Framework test project. TL;DR Adding the adapter and Microsoft.NET.Test.Sdk version 17.8.0 or greater to your NUnit test projects will also enable the dotnet test command for .NET Core projects. Any tests using the new style CSPROJ format, either .NET Core or .NET 4.x, need to add a PackageReference to the NuGet package Microsoft.NET.Test.Sdk. Your test assemblies must also be .NET Core or .NET 4.x, not .NET Standard. You can create a new NUnit test project using dotnet new nunit. It will create an ItemGroup in the csproj file with the necessary references. <ItemGroup> <PackageReference Include=\"Microsoft.NET.Test.Sdk\" Version=\"17.8.0\" /> <PackageReference Include=\"NUnit\" Version=\"4.0.1\" /> <PackageReference Include=\"NUnit3TestAdapter\" Version=\"4.5.0\" /> <PackageReference Include=\"NUnit.Analyzers\" Version=\"3.10.0\" /> <PackageReference Include=\"coverlet.collector\" Version=\"6.0.0\" /> </ItemGroup> .NET Core test can be run on the command line with dotnet test, for example, From the solution root folder dotnet test or from the test project folder dotnet test Or you can specify the csproj file you want to test dotnet test .\\test\\NetCore10Tests\\NetCore10Tests.csproj For a more complete walk-through, please see Testing .NET Core with NUnit in Visual Studio 2017 Using the NUnit project templates The NUnit test project templates come included with dotnet. You can run dotnet new nunit to create an NUnit test project. FAQ Why can't my tests target .NET Standard Visual Studio and VSTest require that the tests target a specific platform. .NET Standard is like a Portable library in that it does not target any specific platform, but can run on any supported platform. Microsoft decided that your tests should be compiled to target a platform so they know which platform to run your tests under and you get the behavior you expect for the platform you are targeting. You can however target multiple platforms in your tests and compile and run each from the command line. It still only runs one platform from Visual Studio, but I would hope that is being worked on. I haven't tested 15.3 yet. It is similar to a console application, it cannot be .NET Standard, it must target a platform, .NET Core or .NET Framework. This limitation is the same for all test adapters including xUnit and MSTest2. My tests aren't showing up in Visual Studio 2022 or later Are you using the NuGet adapter package? Are you using version 4.5.0 or newer of the NuGet adapter package? Do your tests target .NET Core or the full .NET Framework? (see above) Have you added a Package Reference to Microsoft.NET.Test.Sdk? Have you restarted Visual Studio? It is still a bit temperamental. Are you doing integration testing with Asp.Net Core WebApplicationFactory and using minimal API, and it complains about missing testhost.deps.json? Then you're accessing the wrong Program class in your WebApplicationFactory. Fix it by adding a code line at the end of the Program.cs file, and verify that your WebApplicationFactory is actually using this Program class: public partial class Program { } The reason for this is that the generated/hidden Minimal API (Top Level statement) Program class is internal. By declaring the partial part, it is changed to public. See Microsoft docs for more info, and some alternatives. My tests multi-target .NET Core and .NET Framework, why can't I run both in Visual Studio This was a limitation in earlier versions of Visual Studio, but is fixed in Visual Studio 2022. If you must run an earlier version and have this issue, you can run specific tests using the --framework command line option of dotnet test How do I produce a test results file dotnet test can generate an NUnit3 test result file by adding a runsettings property. The property to add is TestOutputXml. This generation is done using the NUnit Engine report service, and produce the same result as the NUnit3-console. This is available through the NUnit3TestAdapter. You run it by adding the setting on the command line (or in a runsettings file): dotnet test -- NUnit.TestOutputXml=yourfoldername The folder is relative to a base folder determined by the OutputXmlFolder settings, or you can use an absolute path. The latter is useful in CI scenarios. Alternatively, there is a 3rd party package, NUnitXml.TestLogger which also produces a NUnit3 xml format. Details for use see here. Note that this is a re-implementation of the NUnit3 format, and may differ."
},
"articles/nunit/getting-started/installation.html": {
"href": "articles/nunit/getting-started/installation.html",
Expand Down
Loading

0 comments on commit 516f09d

Please sign in to comment.