From bd3feb1e5ed133864f60da35143941889cae44c2 Mon Sep 17 00:00:00 2001 From: Alen Lovric Date: Mon, 31 Aug 2026 17:00:32 +0100 Subject: [PATCH] Fix discovery and running of tests in Visual Studio Test Explorer --- templates/integrated/Source/MyProject.csproj | 43 +++++++++++-------- templates/integrated/Source/Program.cs | 8 ++-- .../Source/Tests/TestDiscoveryHandler.cs | 28 ++++++++++++ 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 templates/integrated/Source/Tests/TestDiscoveryHandler.cs diff --git a/templates/integrated/Source/MyProject.csproj b/templates/integrated/Source/MyProject.csproj index 1d11217..3919447 100644 --- a/templates/integrated/Source/MyProject.csproj +++ b/templates/integrated/Source/MyProject.csproj @@ -1,37 +1,42 @@ - + net10.0 enable enable - Exe CS1591;CA2016;CS7022 true + true - - - - - - - - - + + + + + + + + + - + - - - - - + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - - + + diff --git a/templates/integrated/Source/Program.cs b/templates/integrated/Source/Program.cs index d274075..5695946 100644 --- a/templates/integrated/Source/Program.cs +++ b/templates/integrated/Source/Program.cs @@ -2,10 +2,12 @@ using Amazon.SimpleEmailV2; using Dom; using LettuceEncrypt; -using Xunit.MicrosoftTestingPlatform; -if (args.Any(a => a == "dotnettestcli")) // this is a 'dotnet test' run - return await TestPlatformTestFramework.RunAsync(args, SelfRegisteredExtensions.AddSelfRegisteredExtensions); +#if DEBUG +using MyProject.Tests; +if (TestDiscoveryHandler.IsTestRun(args, out var testRunner)) + return await testRunner(); +#endif var bld = WebApplication.CreateBuilder(args); bld.Services diff --git a/templates/integrated/Source/Tests/TestDiscoveryHandler.cs b/templates/integrated/Source/Tests/TestDiscoveryHandler.cs new file mode 100644 index 0000000..6c7568b --- /dev/null +++ b/templates/integrated/Source/Tests/TestDiscoveryHandler.cs @@ -0,0 +1,28 @@ +using System.Diagnostics.CodeAnalysis; +using Xunit.MicrosoftTestingPlatform; +using Xunit.Runner.InProc.SystemConsole; + +namespace MyProject.Tests; + +public static class TestDiscoveryHandler +{ + public static bool IsTestRun(string[] args, [NotNullWhen(true)] out Func>? testRunner) + { + if (args.Contains("@@")) // this is a 'dotnet test' run from the test explorer + { + testRunner = () => + ConsoleRunner.Run(args); + return true; + } + + if (args.Any(a => a == "dotnettestcli")) // this is a 'dotnet test' run from the CLI + { + testRunner = () => + TestPlatformTestFramework.RunAsync(args, SelfRegisteredExtensions.AddSelfRegisteredExtensions); + return true; + } + + testRunner = null; + return false; + } +}