-
Notifications
You must be signed in to change notification settings - Fork 261
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
Offer alternative to serialization of data #4467
base: main
Are you sure you want to change the base?
Changes from 5 commits
4475789
d5fed9b
f01baa6
f06e4d2
ca492b3
987325a
b9fc0d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,17 +181,12 @@ internal UnitTestResult[] RunTestMethod() | |
|
||
bool isDataDriven = false; | ||
var parentStopwatch = Stopwatch.StartNew(); | ||
if (_test.DataType == DynamicDataType.ITestDataSource) | ||
if (TryExecuteITestDataSource(results)) | ||
{ | ||
object?[]? data = DataSerializationHelper.Deserialize(_test.SerializedData); | ||
TestResult[] testResults = ExecuteTestWithDataSource(null, data); | ||
results.AddRange(testResults); | ||
} | ||
else if (TryExecuteDataSourceBasedTests(results)) | ||
{ | ||
isDataDriven = true; | ||
// Do nothing | ||
} | ||
else if (TryExecuteFoldedDataDrivenTests(results)) | ||
else if (TryExecuteDataSourceBasedTests(results) | ||
|| TryExecuteFoldedDataDrivenTests(results)) | ||
{ | ||
isDataDriven = true; | ||
} | ||
|
@@ -254,6 +249,44 @@ internal UnitTestResult[] RunTestMethod() | |
return results.ToUnitTestResults(); | ||
} | ||
|
||
private bool TryExecuteITestDataSource(List<TestResult> results) | ||
{ | ||
if (_test.DataType != DynamicDataType.ITestDataSource) | ||
{ | ||
return false; | ||
} | ||
|
||
UTF.ITestDataSource? dataSource; | ||
object?[]? data; | ||
if (_test.SerializedData?.Length == 3) | ||
{ | ||
if (!Enum.TryParse(_test.SerializedData[0], out TestDataSourceUnfoldingStrategy _) | ||
|| !int.TryParse(_test.SerializedData[1], out int dataSourceIndex) | ||
|| !int.TryParse(_test.SerializedData[2], out int dataIndex)) | ||
Comment on lines
+264
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised there was no CA warning here. I need to verify the existing rules. |
||
{ | ||
throw ApplicationStateGuard.Unreachable(); | ||
} | ||
|
||
dataSource = _testMethodInfo.GetAttributes<Attribute>(false)?.OfType<UTF.ITestDataSource>().Skip(dataSourceIndex).FirstOrDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be a good idea to check with runtime team that it's safe to assume reflection will return attributes in a deterministic order. I think it should be a safe assumption, but just a sanity check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my tests it is deterministic but yes I'll double check with them. |
||
if (dataSource is null) | ||
{ | ||
throw ApplicationStateGuard.Unreachable(); | ||
} | ||
|
||
data = dataSource.GetData(_testMethodInfo.MethodInfo).Skip(dataIndex).FirstOrDefault(); | ||
} | ||
else | ||
{ | ||
dataSource = null; | ||
data = DataSerializationHelper.Deserialize(_test.SerializedData); | ||
} | ||
|
||
TestResult[] testResults = ExecuteTestWithDataSource(dataSource, data); | ||
results.AddRange(testResults); | ||
|
||
return true; | ||
} | ||
|
||
private bool TryExecuteDataSourceBasedTests(List<TestResult> results) | ||
{ | ||
DataSourceAttribute[] dataSourceAttribute = _testMethodInfo.GetAttributes<DataSourceAttribute>(false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,17 +22,34 @@ public enum TestDataSourceUnfoldingStrategy : byte | |
/// <summary> | ||
/// MSTest will decide whether to unfold the parameterized test based on value from the assembly level attribute | ||
/// <see cref="TestDataSourceOptionsAttribute" />. If no assembly level attribute is specified, then the default | ||
/// configuration is to unfold. | ||
/// configuration is to unfold using <see cref="System.Runtime.Serialization.Json.DataContractJsonSerializer"/>. | ||
/// </summary> | ||
Auto, | ||
|
||
/// <summary> | ||
/// Each data row is treated as a separate test case. | ||
/// </summary> | ||
[Obsolete("Use 'UnfoldUsingDataContractJsonSerializer' instead")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider EditorBrowsable(Never), and maybe also inheritdoc from |
||
Unfold, | ||
|
||
/// <summary> | ||
/// The parameterized test is not unfolded; all data rows are treated as a single test case. | ||
/// </summary> | ||
Fold, | ||
|
||
/// <summary> | ||
/// Each data row is treated as a separate test case, and the data is unfolded using | ||
/// <see cref="System.Runtime.Serialization.Json.DataContractJsonSerializer"/>. | ||
/// </summary> | ||
UnfoldUsingDataContractJsonSerializer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this the same numeric value as |
||
|
||
/// <summary> | ||
/// Each data row is treated as a separate test case, and the data is unfolded using the data | ||
/// source index and data index. | ||
/// </summary> | ||
/// <remarks> | ||
/// Using this strategy will alter the test ID if the data source is reordered, as it depends | ||
/// on the index of the data. This may affect the ability to track test cases over time. | ||
/// </remarks> | ||
UnfoldUsingDataIndex, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
#nullable enable | ||
Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.IdentifierStrategy.get -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2
Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Release)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2
Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build Linux Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.IdentifierStrategy.set -> void | ||
Check failure on line 3 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L3
Check failure on line 3 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Release)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L3
Check failure on line 3 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build Linux Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L3
|
||
Evangelink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName, Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType dynamicDataSourceType) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName, System.Type! dynamicDataDeclaringType) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName, System.Type! dynamicDataDeclaringType, Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType dynamicDataSourceType) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.IdentifierStrategy.get -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Check failure on line 8 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L8
Check failure on line 8 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Release)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L8
Check failure on line 8 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build Linux Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L8
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.IdentifierStrategy.set -> void | ||
Check failure on line 9 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L9
Check failure on line 9 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Release)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L9
Check failure on line 9 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build Linux Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L9
|
||
Evangelink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.AutoDetect = 2 -> Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType | ||
*REMOVED*Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName, Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType dynamicDataSourceType = Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Property) -> void | ||
*REMOVED*Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute.DynamicDataAttribute(string! dynamicDataSourceName, System.Type! dynamicDataDeclaringType, Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType dynamicDataSourceType = Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Property) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.ITestDataIdentifierStrategy | ||
Check failure on line 13 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L13
Check failure on line 13 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build MacOS Release)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L13
Check failure on line 13 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Azure Pipelines / microsoft.testfx (Build Linux Debug)src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L13
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.ITestDataIdentifierStrategy.IdentifierStrategy.get -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy.Auto = 0 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy.DataContractSerialization = 1 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy.DataIndex = 2 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceOptionsAttribute.IdentifierStrategy.get -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceOptionsAttribute.TestDataSourceOptionsAttribute(Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy identifierStrategy) -> void | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceOptionsAttribute.TestDataSourceOptionsAttribute(Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceUnfoldingStrategy unfoldingStrategy, Microsoft.VisualStudio.TestTools.UnitTesting.TestDataIdentifierStrategy identifierStrategy) -> void | ||
Evangelink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceUnfoldingStrategy.UnfoldUsingDataContractJsonSerializer = 3 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceUnfoldingStrategy | ||
Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceUnfoldingStrategy.UnfoldUsingDataIndex = 4 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestDataSourceUnfoldingStrategy | ||
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Throws<TException>(System.Action! action, string! message = "", params object![]! messageArgs) -> TException! | ||
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsAsync<TException>(System.Func<System.Threading.Tasks.Task!>! action, string! message = "", params object![]! messageArgs) -> System.Threading.Tasks.Task<TException!>! | ||
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExactly<TException>(System.Action! action, string! message = "", params object![]! messageArgs) -> TException! | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am reusing the serialized data property as it's not used and allows me to avoid adding more properties to the TestCase object as it has some perf impact. This isn't perfect but as far as I can tell, there is no way for the serialized data to be serialized this way as we prefix with the data type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine for me to reuse. At least until we have a strong reason not to reuse.