Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take TestCaseFilter from runsettings #2356

Merged
merged 15 commits into from
Mar 25, 2020

Conversation

nohwnd
Copy link
Member

@nohwnd nohwnd commented Mar 5, 2020

Description

Take default filter from RunSettings.

# file runsettings.settings
<RunSettings>
    <RunConfiguration>
        <TestCaseFilter>TestCategory=include</TestCaseFilter>
    </RunConfiguration>
</RunSettings>

Related issue

Fixes #2273

@nohwnd nohwnd requested a review from vritant24 March 5, 2020 15:16
@AbhitejJohn
Copy link
Contributor

@nohwnd : Just confirming that this works for discovery as well as execution? I'm hoping this just works in Test Explorer to show only the tests specified in this filter after this change...

@nohwnd
Copy link
Member Author

nohwnd commented Mar 9, 2020

@AbhitejJohn it doesn't, I have another fix, now I am figuring out why xunit ignores the filter.

@vritant24
Copy link
Member

We would need update the documentation to include this feature, I'm sure a good number of people would like to make use of it.

@nohwnd nohwnd changed the title Take TestCaseFilter from runsettings WIP: Take TestCaseFilter from runsettings Mar 10, 2020
@nohwnd
Copy link
Member Author

nohwnd commented Mar 10, 2020

Marking as WIP till I figure out the remaining requirements, was to fast to post this as a PR.

@nohwnd
Copy link
Member Author

nohwnd commented Mar 10, 2020

@vritant24 it now works during discovery (but only for MSTest, unload the xunit project). The simplest way to see this is, to use the vstest console and with list tests:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\IntPreview\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" /Platform:x86 /ListTests /TestCaseFilter:TestCategory=include C:\Users\jajares\source\repos\UnitTestProject29\XUnitTestProject1\bin\Debug\netcoreapp3.1\XUnitTestProject1.dll C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll

I can also see in VS that it reports that it discovered 1 test. Running the tests also runs only the one that passes filter. Selecting just the excluded test and running it will write errors into test output, and incorrectly report it as passed in the status bar and output but in TE it stays as the blue outdated icon.

image

[10/03/2020 12:26:31.679 pm] ========== Test discovery finished: 1 Tests found in 923,2 ms ==========
[10/03/2020 12:26:31.683 pm] ---------- Starting test run ----------
[10/03/2020 12:26:31.683 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection 
[10/03/2020 12:26:31.683 pm] Event:VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection key: VS.UnitTest.TestWindow.RunSettingsProvider.UserFile value:True
[10/03/2020 12:26:31.683 pm] Event:VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection key: VS.UnitTest.TestWindow.RunSettingsProvider.SolutionFile value:False
[10/03/2020 12:26:31.683 pm] Event:VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection key: VS.UnitTest.TestWindow.RunSettingsProvider.AutoDetect value:False
[10/03/2020 12:26:31.684 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.684 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:BoostTestInternalSettings
[10/03/2020 12:26:31.684 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.684 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:VSTest Run Configuration
[10/03/2020 12:26:31.684 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.684 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:Fakes
[10/03/2020 12:26:31.684 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.684 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:Dynamic Code Coverage
[10/03/2020 12:26:31.685 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.685 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:GoogleTestAdapterSettings
[10/03/2020 12:26:31.686 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.686 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:TestPropertySettingsForBoostAdapter
[10/03/2020 12:26:31.686 pm] Using runsettings file C:\Projects\temp\UnitTestProject29\runsettings.runsettings for C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll
[10/03/2020 12:26:31.686 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettingsService 
[10/03/2020 12:26:31.687 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettingsService key: VS.UnitTest.TestWindow.RunSettingsService.Name value:TestPropertySettingsForGoogleAdapter
[10/03/2020 12:26:31.687 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/Ext/RunSettings 
[10/03/2020 12:26:31.687 pm] Event:VS/UnitTest/TestWindow/Ext/RunSettings key: VS.UnitTest.TestWindow.RunSettings.Services value:7
[10/03/2020 12:26:31.687 pm] TelemetrySession: Creating the event: VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection 
[10/03/2020 12:26:31.687 pm] Event:VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection key: VS.UnitTest.TestWindow.RunSettingsProvider.ProjectFileCount value:0
[10/03/2020 12:26:31.687 pm] Event:VS/UnitTest/TestWindow/RunSettingsProvider/FileSelection key: VS.UnitTest.TestWindow.RunSettingsProvider.ContainerCount value:1
[10/03/2020 12:26:31.688 pm] Some tests from the test run selection will be executed by name.
[10/03/2020 12:26:31.688 pm] Tests run settings for C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll:
 <RunSettings>
  <RunConfiguration>
    <TestCaseFilter>TestCategory=include</TestCaseFilter>
    <ResultsDirectory>C:\Users\jajares\source\repos\UnitTestProject29\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Users\jajares\source\repos\UnitTestProject29\</SolutionDirectory>
    <TargetPlatform>X86</TargetPlatform>
    <CollectSourceInformation>False</CollectSourceInformation>
  </RunConfiguration>
  <BoostTestInternalSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <VSProcessId>37624</VSProcessId>
  </BoostTestInternalSettings>
  <GoogleTestAdapterSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SolutionSettings>
      <Settings />
    </SolutionSettings>
    <ProjectSettings />
  </GoogleTestAdapterSettings>
</RunSettings>.
[10/03/2020 12:26:31.978 pm] Trying to update view -642072036
[10/03/2020 12:26:31.979 pm] Updating view -642072036
[10/03/2020 12:26:31.982 pm] Updating view -642072036
[10/03/2020 12:26:32.011 pm] UpdateSummary Detail Unchanged: SKIPPED
[10/03/2020 12:26:32.269 pm] Logging TestHost Diagnostics in file: C:\Users\jajares\AppData\Local\Temp\TestPlatformLogs\7156_03_10_2020_12_18_25\logs.host.20-03-10_12-26-31_71477_10.txt
[10/03/2020 12:26:32.352 pm] UpdateSummary Detail Unchanged: SKIPPED
[10/03/2020 12:26:32.635 pm] Microsoft.VisualStudio.TestStorage.TestStoreIndexingException: Failed to add result for test 'UnitTestProject29.UnitTest1.ExcludedMsTest' with ID 'd641ebf9-1b08-dc32-d0f0-69404b824a5a'.
   at Microsoft.VisualStudio.TestStorage.TestStoreIndexSet.AddToIndexSet(TestBaseRecord record)
   at Microsoft.VisualStudio.TestStorage.TestStore.Add(TestBaseRecord record)
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResult(TestResultRecord testResultRecord)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
[10/03/2020 12:26:32.635 pm] ========== Test run finished: 1 Tests run in 951,7 ms (1 Passed, 0 Failed, 0 Skipped) ==========
[10/03/2020 12:26:32.635 pm] Microsoft.VisualStudio.TestStorage.TestStoreIndexingException: Failed to add result for test 'UnitTestProject29.UnitTest1.ExcludedMsTest' with ID 'd641ebf9-1b08-dc32-d0f0-69404b824a5a'.
   at Microsoft.VisualStudio.TestStorage.TestStoreIndexSet.AddToIndexSet(TestBaseRecord record)
   at Microsoft.VisualStudio.TestStorage.TestStore.Add(TestBaseRecord record)
   at Microsoft.VisualStudio.TestWindow.Host.TestRunSession.AddTestResult(TestResultRecord testResultRecord)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
[10/03/2020 12:26:32.940 pm] Trying to update view -642072036
[10/03/2020 12:26:32.940 pm] Updating view -642072036
[10/03/2020 12:26:32.942 pm] Updating view -642072036
[10/03/2020 12:26:32.944 pm] UpdateSummary Detail Unchanged: SKIPPED
[10/03/2020 12:26:32.954 pm] Discovering tests in C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\UnitTest1.cs
[10/03/2020 12:26:32.964 pm] Discovering tests in C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\obj\Debug\netcoreapp3.1\UnitTestProject29.AssemblyInfo.cs
[10/03/2020 12:26:32.972 pm] Discovering tests in C:\Users\jajares\source\repos\UnitTestProject29\packages\microsoft.net.test.sdk\16.6.0-dev\build\netcoreapp2.1\Microsoft.NET.Test.Sdk.Program.cs
[10/03/2020 12:26:32.976 pm] Discovering tests in C:\Users\jajares\source\repos\UnitTestProject29\UnitTestProject29\obj\Debug\netcoreapp3.1\.NETCoreApp,Version=v3.1.AssemblyAttributes.cs
[10/03/2020 12:26:33.305 pm] UpdateSummary Detail Unchanged: SKIPPED

@nohwnd
Copy link
Member Author

nohwnd commented Mar 12, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@nohwnd
Copy link
Member Author

nohwnd commented Mar 13, 2020

@ViktorHofer Currently we are figuring out how to filter also while the source based test discovery runs. It looks like we would have to adopt all the filtering logic from all the supported adapters (so xunit, mstest, nunit).

/cc @vritant24

@ViktorHofer
Copy link
Member

@nohwnd thanks for the update.

I just thought about this further and I believe that we should not ignore a passed in filter (if set by the CLI) when a default filter is provided by the runsettings file. That would effectively mean that consumers won't be able to use the filter argument in dotnet test scenarios when a default one is set by the repository. Can we instead concatenate the filters together?

@nohwnd
Copy link
Member Author

nohwnd commented Mar 16, 2020

@ViktorHofer what you describe is not how it works. The filter that you provide on command line will win.

This is a decision that is (or should be) used in all cases, so the user gets the flexibility of providing a custom value, while falling back to the default if not providing any.


I am not 100% sure now what will happen if you do this:

dotnet test --filter someFilter `
    --settings runsettings.settings -- <TestCaseFilter>someFilter</TestCaseFilter>

But I would expect that the options are applied like this: settings file -> settings from commandline -> parameters from command line.

@ViktorHofer
Copy link
Member

so the user gets the flexibility of providing a custom value, while falling back to the default if not providing any.

Understood. So you don't think concatenating the filters makes sense?

@nohwnd
Copy link
Member Author

nohwnd commented Mar 16, 2020

@ViktorHofer in some cases yes, but it might not be what the user wants and it would be different than how the other settings behave, so I would rather keep it as is.

@ViktorHofer
Copy link
Member

This might be problematic. Our plan in dotnet/runtime was to use this feature to disable tests with the trait "failing" by default in the .runsettings file. What you are saying would mean that the user would always need to append Trait!=failing in the filter as the default one is overridden.

@nohwnd
Copy link
Member Author

nohwnd commented Mar 16, 2020

That is true, we will have a meeting on Tuesday about this, so I will discuss it there.

@ViktorHofer
Copy link
Member

I don't think there is any harm in concatenating filters specified in different sources (CLI vs runsettings). Thanks for following up on that.

@nohwnd
Copy link
Member Author

nohwnd commented Mar 25, 2020

# old console
PS> dotnet test --list-tests
Test run for C:\Projects\temp\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.6.0-preview-20200318-01
Copyright (c) Microsoft Corporation.  All rights reserved.
The following Tests are available:
    IncludedMsTest
    ExcludedMsTest
    IncludedMsTest2
    IncludedMsTest3
    ExcludedMsTest2
    ExcludedMsTest3

# with the fix
PS> dotnet test -p:VsTestConsolePath=C:\Projects\vstest\artifacts\Debug\netcoreapp2.1\vstest.console.dll --list-tests
Test run for C:\Projects\temp\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.6.0-dev
Copyright (c) Microsoft Corporation.  All rights reserved.
The following Tests are available:
    IncludedMsTest
    IncludedMsTest2
    IncludedMsTest3

# with the fix and additional filter
PS> dotnet test -p:VsTestConsolePath=C:\Projects\vstest\artifacts\Debug\netcoreapp2.1\vstest.console.dll --list-tests --filter Name~3
Test run for C:\Projects\temp\UnitTestProject29\UnitTestProject29\bin\Debug\netcoreapp3.1\UnitTestProject29.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.6.0-dev
Copyright (c) Microsoft Corporation.  All rights reserved.
The following Tests are available:
    IncludedMsTest3

In VS with Source Based discovery disabled.

filtered-disovery

image

<!-- file UnitTestProject29\UnitTestProject29.csproj -->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
    <RunSettingsFilePath>$(MSBuildProjectDirectory)\..\runsettings.runsettings</RunSettingsFilePath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

</Project>
# file runsettings.runsettings
<RunSettings>
    <RunConfiguration>
        <TestCaseFilter>TestCategory=include</TestCaseFilter>
    </RunConfiguration>
</RunSettings>
// file UnitTestProject29\UnitTest1.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject29
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [TestCategory("include")]
        public void IncludedMsTest()
        {
        }

        [TestMethod]
        [TestCategory("exclude")]
        public void ExcludedMsTest()
        {
        }

        [TestMethod]
        [TestCategory("include")]
        public void IncludedMsTest2()
        {
        }

        [TestMethod]
        [TestCategory("include")]
        public void IncludedMsTest3()
        {
        }

        [TestMethod]
        [TestCategory("exclude")]
        public void ExcludedMsTest2()
        {
        }

        [TestMethod]
        [TestCategory("exclude")]
        public void ExcludedMsTest3()
        {
        }

    }
}

We agreed that this option should remain undocumented, so we are able to redesign this if we start fixing the source based discovery, for example moving it to adapters.

nohwnd added 2 commits March 25, 2020 13:45
It fails because of pin to newer dotnet, it's nice to have will fix later.
@nohwnd nohwnd changed the title WIP: Take TestCaseFilter from runsettings Take TestCaseFilter from runsettings Mar 25, 2020
@nohwnd nohwnd merged commit d10bcbb into microsoft:master Mar 25, 2020
@L4ZZA
Copy link

L4ZZA commented Mar 26, 2020

Will this work for c++ tests? [i.e GoogleTest, CppUnitTests (MS) ]

@nohwnd
Copy link
Member Author

nohwnd commented Mar 26, 2020

Will this work for c++ tests? [i.e GoogleTest, CppUnitTests (MS) ]

It should if the adapter implements filtering during discovery (if not there already). Also notice that it comes with a limitation that it does not work for source based discovery.

@L4ZZA
Copy link

L4ZZA commented Mar 27, 2020

source based discovery

and what's that?

@nohwnd
Copy link
Member Author

nohwnd commented Mar 27, 2020

In Visual Studio you get tests discovered even before you build your project, you can enable (default), or disable it by the checkbox shown above. This solution only works when it is disabled, which is not the default setting.

@L4ZZA
Copy link

L4ZZA commented Mar 29, 2020

as far as I know that's only an option for C# and .NET
All the issues we're having are with C++

@nohwnd
Copy link
Member Author

nohwnd commented Mar 30, 2020

@L4ZZA then probably give it a try when it's released and report back (VS 16.6 preview 3 should have it if all goes well), I don't know much about C++ tests yet so I can't reliably tell you, sorry 🙂

@ffMathy
Copy link

ffMathy commented Nov 22, 2020

Should this still work for .NET 5? I can't get it working.

@ViktorHofer
Copy link
Member

Jep that should work. Make sure to disable real time test discovery in VS:

image

@ffMathy
Copy link

ffMathy commented Nov 25, 2020

I rely on this feature (or something similar) in the Pruner CLI (https://github.com/pruner/cli). I would be sad if the feature was removed.

I think it should be possible for commandline tools or VS Code plugins to still be able to customize the filter this way through.

It's also a perfect fix for this issue: #2625

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filtering default traits/categories in the .runsettings file
8 participants