-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: Add filter support for TestAdapter #2788
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
Merged
timcassell
merged 1 commit into
dotnet:master
from
filzrev:feat-add-vstest-filter-supports
Jul 21, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace BenchmarkDotNet.TestAdapter; | ||
|
||
internal class LoggerHelper | ||
{ | ||
public LoggerHelper(IMessageLogger logger, Stopwatch stopwatch) | ||
{ | ||
InnerLogger = logger; | ||
Stopwatch = stopwatch; | ||
} | ||
|
||
public IMessageLogger InnerLogger { get; private set; } | ||
|
||
public Stopwatch Stopwatch { get; private set; } | ||
|
||
public void Log(string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Informational, null, string.Format(format, args)); | ||
} | ||
|
||
public void LogWithSource(string source, string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Informational, source, string.Format(format, args)); | ||
} | ||
|
||
public void LogError(string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Error, null, string.Format(format, args)); | ||
} | ||
|
||
public void LogErrorWithSource(string source, string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Error, source, string.Format(format, args)); | ||
} | ||
|
||
public void LogWarning(string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Warning, null, string.Format(format, args)); | ||
} | ||
|
||
public void LogWarningWithSource(string source, string format, params object[] args) | ||
{ | ||
SendMessage(TestMessageLevel.Warning, source, string.Format(format, args)); | ||
} | ||
|
||
private void SendMessage(TestMessageLevel level, string? assemblyName, string message) | ||
{ | ||
var assemblyText = assemblyName == null | ||
? "" : | ||
$"{Path.GetFileNameWithoutExtension(assemblyName)}: "; | ||
|
||
InnerLogger.SendMessage(level, $"[BenchmarkDotNet {Stopwatch.Elapsed:hh\\:mm\\:ss\\.ff}] {assemblyText}{message}"); | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
src/BenchmarkDotNet.TestAdapter/Utility/TestCaseFilter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace BenchmarkDotNet.TestAdapter; | ||
|
||
internal class TestCaseFilter | ||
{ | ||
private const string DisplayNameString = "DisplayName"; | ||
private const string FullyQualifiedNameString = "FullyQualifiedName"; | ||
|
||
private readonly HashSet<string> knownTraits; | ||
private List<string> supportedPropertyNames; | ||
private readonly ITestCaseFilterExpression? filterExpression; | ||
private readonly bool successfullyGotFilter; | ||
private readonly bool isDiscovery; | ||
|
||
public TestCaseFilter(IDiscoveryContext discoveryContext, LoggerHelper logger) | ||
{ | ||
// Traits are not known at discovery time because we load them from benchmarks | ||
isDiscovery = true; | ||
knownTraits = []; | ||
supportedPropertyNames = GetSupportedPropertyNames(); | ||
successfullyGotFilter = GetTestCaseFilterExpressionFromDiscoveryContext(discoveryContext, logger, out filterExpression); | ||
} | ||
|
||
public TestCaseFilter(IRunContext runContext, LoggerHelper logger, string assemblyFileName, HashSet<string> knownTraits) | ||
{ | ||
this.knownTraits = knownTraits; | ||
supportedPropertyNames = GetSupportedPropertyNames(); | ||
successfullyGotFilter = GetTestCaseFilterExpression(runContext, logger, assemblyFileName, out filterExpression); | ||
} | ||
|
||
public string GetTestCaseFilterValue() | ||
{ | ||
return successfullyGotFilter | ||
? filterExpression?.TestCaseFilterValue ?? "" | ||
: ""; | ||
} | ||
|
||
public bool MatchTestCase(TestCase testCase) | ||
{ | ||
if (!successfullyGotFilter) | ||
{ | ||
// Had an error while getting filter, match no testcase to ensure discovered test list is empty | ||
return false; | ||
} | ||
else if (filterExpression == null) | ||
{ | ||
// No filter specified, keep every testcase | ||
return true; | ||
} | ||
|
||
return filterExpression.MatchTestCase(testCase, p => PropertyProvider(testCase, p)); | ||
} | ||
|
||
public object? PropertyProvider(TestCase testCase, string name) | ||
{ | ||
// Traits filtering | ||
if (isDiscovery || knownTraits.Contains(name)) | ||
{ | ||
var result = new List<string>(); | ||
|
||
foreach (var trait in GetTraits(testCase)) | ||
if (string.Equals(trait.Key, name, StringComparison.OrdinalIgnoreCase)) | ||
result.Add(trait.Value); | ||
|
||
if (result.Count > 0) | ||
return result.ToArray(); | ||
} | ||
|
||
// Property filtering | ||
switch (name.ToLowerInvariant()) | ||
{ | ||
// FullyQualifiedName | ||
case "fullyqualifiedname": | ||
return testCase.FullyQualifiedName; | ||
// DisplayName | ||
case "displayname": | ||
return testCase.DisplayName; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
private bool GetTestCaseFilterExpression(IRunContext runContext, LoggerHelper logger, string assemblyFileName, out ITestCaseFilterExpression? filter) | ||
{ | ||
filter = null; | ||
|
||
try | ||
{ | ||
filter = runContext.GetTestCaseFilter(supportedPropertyNames, null!); | ||
return true; | ||
} | ||
catch (TestPlatformFormatException e) | ||
{ | ||
logger.LogWarning("{0}: Exception filtering tests: {1}", Path.GetFileNameWithoutExtension(assemblyFileName), e.Message); | ||
return false; | ||
} | ||
} | ||
|
||
private bool GetTestCaseFilterExpressionFromDiscoveryContext(IDiscoveryContext discoveryContext, LoggerHelper logger, out ITestCaseFilterExpression? filter) | ||
{ | ||
filter = null; | ||
|
||
if (discoveryContext is IRunContext runContext) | ||
{ | ||
try | ||
{ | ||
filter = runContext.GetTestCaseFilter(supportedPropertyNames, null!); | ||
return true; | ||
} | ||
catch (TestPlatformException e) | ||
{ | ||
logger.LogWarning("Exception filtering tests: {0}", e.Message); | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
// GetTestCaseFilter is present on DiscoveryContext but not in IDiscoveryContext interface | ||
var method = discoveryContext.GetType().GetRuntimeMethod("GetTestCaseFilter", [typeof(IEnumerable<string>), typeof(Func<string, TestProperty>)]); | ||
filter = (ITestCaseFilterExpression)method?.Invoke(discoveryContext, [supportedPropertyNames, null])!; | ||
|
||
return true; | ||
} | ||
catch (TargetInvocationException e) | ||
{ | ||
if (e?.InnerException is TestPlatformException ex) | ||
{ | ||
logger.LogWarning("Exception filtering tests: {0}", ex.InnerException.Message ?? ""); | ||
return false; | ||
} | ||
|
||
throw e!.InnerException; | ||
} | ||
} | ||
} | ||
|
||
private List<string> GetSupportedPropertyNames() | ||
{ | ||
// Returns the set of well-known property names usually used with the Test Plugins (Used Test Traits + DisplayName + FullyQualifiedName) | ||
if (supportedPropertyNames == null) | ||
{ | ||
supportedPropertyNames = knownTraits.ToList(); | ||
supportedPropertyNames.Add(DisplayNameString); | ||
supportedPropertyNames.Add(FullyQualifiedNameString); | ||
} | ||
|
||
return supportedPropertyNames; | ||
} | ||
|
||
private static IEnumerable<KeyValuePair<string, string>> GetTraits(TestCase testCase) | ||
{ | ||
var traitProperty = TestProperty.Find("TestObject.Traits"); | ||
return traitProperty != null | ||
? testCase.GetPropertyValue(traitProperty, Array.Empty<KeyValuePair<string, string>>()) | ||
: []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/BenchmarkDotNet.TestAdapter/build/BenchmarkDotNet.TestAdapter.props
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.