Skip to content

Commit 25e984e

Browse files
authored
Fixed a bug in ITestDataSource data deserialization (microsoft#864)
- Fixed a bug in `ITestDataSource` data deserialization when trying to deserialize a type outside of current assembly. This should fix the last item in microsoft#844. - Added extra tests cases for `ITestDataSource`, we're now testing almost all cases we can think of.
1 parent 5d4bb67 commit 25e984e

File tree

15 files changed

+766
-53
lines changed

15 files changed

+766
-53
lines changed

TestFx.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinUI", "WinUI", "{35D010CC
216216
src\Adapter\Build\WinUI\MSTest.TestAdapter.targets = src\Adapter\Build\WinUI\MSTest.TestAdapter.targets
217217
EndProjectSection
218218
EndProject
219+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReferencedProjectFromDataSourceTest", "test\E2ETests\TestAssets\ReferencedProjectFromDataSourceTest\ReferencedProjectFromDataSourceTest.csproj", "{6B4DE65C-4162-4C52-836A-8F9FA901814A}"
220+
EndProject
219221
Global
220222
GlobalSection(SharedMSBuildProjectFiles) = preSolution
221223
src\Adapter\PlatformServices.Shared\PlatformServices.Shared.projitems*{2177c273-ae07-43b3-b87a-443e47a23c5a}*SharedItemsImports = 13
@@ -1270,6 +1272,30 @@ Global
12701272
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|x64.Build.0 = Release|x64
12711273
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|x86.ActiveCfg = Release|x86
12721274
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|x86.Build.0 = Release|x86
1275+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|Any CPU.ActiveCfg = Debug|Any CPU
1276+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|Any CPU.Build.0 = Debug|Any CPU
1277+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|ARM.ActiveCfg = Debug|Any CPU
1278+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|ARM.Build.0 = Debug|Any CPU
1279+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|x64.ActiveCfg = Debug|Any CPU
1280+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
1281+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
1282+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
1283+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1284+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|Any CPU.Build.0 = Debug|Any CPU
1285+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|ARM.ActiveCfg = Debug|Any CPU
1286+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|ARM.Build.0 = Debug|Any CPU
1287+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|x64.ActiveCfg = Debug|Any CPU
1288+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|x64.Build.0 = Debug|Any CPU
1289+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|x86.ActiveCfg = Debug|Any CPU
1290+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Debug|x86.Build.0 = Debug|Any CPU
1291+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|Any CPU.ActiveCfg = Release|Any CPU
1292+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|Any CPU.Build.0 = Release|Any CPU
1293+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|ARM.ActiveCfg = Release|Any CPU
1294+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|ARM.Build.0 = Release|Any CPU
1295+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x64.ActiveCfg = Release|Any CPU
1296+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x64.Build.0 = Release|Any CPU
1297+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x86.ActiveCfg = Release|Any CPU
1298+
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x86.Build.0 = Release|Any CPU
12731299
EndGlobalSection
12741300
GlobalSection(SolutionProperties) = preSolution
12751301
HideSolutionNode = FALSE
@@ -1339,6 +1365,7 @@ Global
13391365
{D11C6664-1C4E-48F0-AA92-7F5BADC6F82C} = {CA01DAF5-8D9D-496E-9AD3-94BB7FBB2D34}
13401366
{F4E2876F-6E42-4DCF-B629-041A9DF7C579} = {24088844-2107-4DB2-8F3F-CBCA94FC4B28}
13411367
{35D010CC-CDF2-4115-BCFB-E2E3D21C1055} = {CA01DAF5-8D9D-496E-9AD3-94BB7FBB2D34}
1368+
{6B4DE65C-4162-4C52-836A-8F9FA901814A} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
13421369
EndGlobalSection
13431370
GlobalSection(ExtensibilityGlobals) = postSolution
13441371
SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9}

src/Adapter/MSTest.CoreAdapter/Execution/TestMethodRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal UnitTestResult[] RunTestMethod()
206206
{
207207
if (this.test.DataType == DynamicDataType.ITestDataSource)
208208
{
209-
var data = DataSerializationHelper.Deserialize(this.test.SerializedData, this.testMethodInfo.Parent.Parent.Assembly);
209+
var data = DataSerializationHelper.Deserialize(this.test.SerializedData);
210210
var testResults = this.ExecuteTestWithDataSource(null, data);
211211
results.AddRange(testResults);
212212
}

src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ public static string[] Serialize(object[] data)
4444
{
4545
serializedData[typeIndex] = null;
4646
serializedData[dataIndex] = null;
47+
4748
continue;
4849
}
4950

5051
var type = data[i].GetType();
51-
var typeName = type.FullName;
52+
var typeName = type.AssemblyQualifiedName;
5253

5354
serializedData[typeIndex] = typeName;
5455

@@ -70,9 +71,8 @@ public static string[] Serialize(object[] data)
7071
/// Deserialzes the data serialzed by <see cref="Serialize(object[])" /> method.
7172
/// </summary>
7273
/// <param name="serializedData">Serialized data array to deserialize.</param>
73-
/// <param name="assemblies">Assemblies that serialized types defined in.</param>
7474
/// <returns>Deserialized array.</returns>
75-
public static object[] Deserialize(string[] serializedData, params Assembly[] assemblies)
75+
public static object[] Deserialize(string[] serializedData)
7676
{
7777
if (serializedData == null || serializedData.Length % 2 != 0)
7878
{
@@ -94,7 +94,7 @@ public static object[] Deserialize(string[] serializedData, params Assembly[] as
9494
continue;
9595
}
9696

97-
var serializer = GetSerializer(typeName, assemblies);
97+
var serializer = GetSerializer(typeName);
9898

9999
var serialzedDataBytes = Encoding.UTF8.GetBytes(serializedValue);
100100
using (var memoryStream = new MemoryStream(serialzedDataBytes))
@@ -106,7 +106,7 @@ public static object[] Deserialize(string[] serializedData, params Assembly[] as
106106
return data;
107107
}
108108

109-
private static DataContractJsonSerializer GetSerializer(string typeName, Assembly[] assemblies)
109+
private static DataContractJsonSerializer GetSerializer(string typeName)
110110
{
111111
var serializer = SerializerCache.SingleOrDefault(i => i.Key.FullName == typeName);
112112
if (serializer.Value != null)
@@ -120,18 +120,6 @@ private static DataContractJsonSerializer GetSerializer(string typeName, Assembl
120120
return GetSerializer(type);
121121
}
122122

123-
if (assemblies != null)
124-
{
125-
foreach (var assembly in assemblies)
126-
{
127-
type = assembly.GetType(typeName);
128-
if (type != null)
129-
{
130-
return GetSerializer(type);
131-
}
132-
}
133-
}
134-
135123
return GetSerializer(typeof(object));
136124
}
137125

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.MSTestV2.Smoke.DiscoveryAndExecutionTests
5+
{
6+
using Microsoft.MSTestV2.CLIAutomation;
7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
using System.IO;
11+
using System.Linq;
12+
13+
[TestClass]
14+
public class TestDataSourceTests : CLITestBase
15+
{
16+
private const string TestAssembly = "DataSourceTestProject.dll";
17+
18+
[TestMethod]
19+
public void ExecuteCsvTestDataSourceTests()
20+
{
21+
// Arrange
22+
var assemblyPath = Path.IsPathRooted(TestAssembly) ? TestAssembly : this.GetAssetFullPath(TestAssembly);
23+
24+
// Act
25+
var testCases = DiscoverTests(assemblyPath, "CsvTestMethod");
26+
var testResults = RunTests(assemblyPath, testCases);
27+
28+
// Assert
29+
Assert.That.ContainsTestsPassed(testResults,
30+
"CsvTestMethod (Data Row 0)",
31+
"CsvTestMethod (Data Row 2)"
32+
);
33+
34+
Assert.That.ContainsTestsFailed(testResults,
35+
"CsvTestMethod (Data Row 1)",
36+
"CsvTestMethod (Data Row 3)"
37+
);
38+
}
39+
40+
[TestMethod]
41+
public void ExecuteDynamicDataTests()
42+
{
43+
// Arrange
44+
var assemblyPath = Path.IsPathRooted(TestAssembly) ? TestAssembly : this.GetAssetFullPath(TestAssembly);
45+
46+
// Act
47+
var testCases = DiscoverTests(assemblyPath, "DynamicDataTest");
48+
var testResults = RunTests(assemblyPath, testCases);
49+
50+
// Assert
51+
Assert.That.ContainsTestsPassed(testResults,
52+
"DynamicDataTest (John;Doe,DataSourceTestProject.ITestDataSourceTests.User)"
53+
);
54+
55+
Assert.That.FailedTestCount(testResults, 0);
56+
}
57+
58+
[TestMethod]
59+
public void ExecuteDataRowTests_Enums()
60+
{
61+
// Arrange
62+
var assemblyPath = Path.IsPathRooted(TestAssembly) ? TestAssembly : this.GetAssetFullPath(TestAssembly);
63+
64+
// Act
65+
var testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DataRowTests_Enums");
66+
var testResults = RunTests(assemblyPath, testCases);
67+
68+
// Assert
69+
Assert.That.TestsPassed(testResults,
70+
"DataRowEnums_SByte (Alfa)",
71+
"DataRowEnums_SByte (Beta)",
72+
"DataRowEnums_SByte (Gamma)",
73+
"DataRowEnums_Byte (Alfa)",
74+
"DataRowEnums_Byte (Beta)",
75+
"DataRowEnums_Byte (Gamma)",
76+
"DataRowEnums_Short (Alfa)",
77+
"DataRowEnums_Short (Beta)",
78+
"DataRowEnums_Short (Gamma)",
79+
"DataRowEnums_UShort (Alfa)",
80+
"DataRowEnums_UShort (Beta)",
81+
"DataRowEnums_UShort (Gamma)",
82+
"DataRowEnums_Int (Alfa)",
83+
"DataRowEnums_Int (Beta)",
84+
"DataRowEnums_Int (Gamma)",
85+
"DataRowEnums_UInt (Alfa)",
86+
"DataRowEnums_UInt (Beta)",
87+
"DataRowEnums_UInt (Gamma)",
88+
"DataRowEnum_Long (Alfa)",
89+
"DataRowEnum_Long (Beta)",
90+
"DataRowEnum_Long (Gamma)",
91+
"DataRowEnum_ULong (Alfa)",
92+
"DataRowEnum_ULong (Beta)",
93+
"DataRowEnum_ULong (Gamma)",
94+
95+
"DataRowEnums_Nullable_SByte ()",
96+
"DataRowEnums_Nullable_SByte (Alfa)",
97+
"DataRowEnums_Nullable_SByte (Beta)",
98+
"DataRowEnums_Nullable_SByte (Gamma)",
99+
"DataRowEnums_Nullable_Byte ()",
100+
"DataRowEnums_Nullable_Byte (Alfa)",
101+
"DataRowEnums_Nullable_Byte (Beta)",
102+
"DataRowEnums_Nullable_Byte (Gamma)",
103+
"DataRowEnums_Nullable_Short ()",
104+
"DataRowEnums_Nullable_Short (Alfa)",
105+
"DataRowEnums_Nullable_Short (Beta)",
106+
"DataRowEnums_Nullable_Short (Gamma)",
107+
"DataRowEnums_Nullable_UShort ()",
108+
"DataRowEnums_Nullable_UShort (Alfa)",
109+
"DataRowEnums_Nullable_UShort (Beta)",
110+
"DataRowEnums_Nullable_UShort (Gamma)",
111+
"DataRowEnums_Nullable_Int ()",
112+
"DataRowEnums_Nullable_Int (Alfa)",
113+
"DataRowEnums_Nullable_Int (Beta)",
114+
"DataRowEnums_Nullable_Int (Gamma)",
115+
"DataRowEnums_Nullable_UInt ()",
116+
"DataRowEnums_Nullable_UInt (Alfa)",
117+
"DataRowEnums_Nullable_UInt (Beta)",
118+
"DataRowEnums_Nullable_UInt (Gamma)",
119+
"DataRowEnums_Nullable_Long ()",
120+
"DataRowEnums_Nullable_Long (Alfa)",
121+
"DataRowEnums_Nullable_Long (Beta)",
122+
"DataRowEnums_Nullable_Long (Gamma)",
123+
"DataRowEnums_Nullable_ULong ()",
124+
"DataRowEnums_Nullable_ULong (Alfa)",
125+
"DataRowEnums_Nullable_ULong (Beta)",
126+
"DataRowEnums_Nullable_ULong (Gamma)",
127+
128+
"DataRowEnums_MixedTypes_Byte (Alfa,True,1)",
129+
"DataRowEnums_MixedTypes_Byte (Beta,False,2)",
130+
"DataRowEnums_MixedTypes_Byte (Gamma,True,3)"
131+
);
132+
133+
Assert.That.FailedTestCount(testResults, 0);
134+
}
135+
136+
[TestMethod]
137+
public void ExecuteDataRowTests_NonSerializablePaths()
138+
{
139+
// Arrange
140+
var assemblyPath = Path.IsPathRooted(TestAssembly) ? TestAssembly : this.GetAssetFullPath(TestAssembly);
141+
142+
// Act
143+
var testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DataRowTests_NonSerializablePaths");
144+
var testResults = RunTests(assemblyPath, testCases);
145+
146+
// Assert
147+
Assert.That.TestsPassed(testResults,
148+
"DataRowNonSerializable (System.String)",
149+
"DataRowNonSerializable (System.Int32)",
150+
"DataRowNonSerializable (DataSourceTestProject.ITestDataSourceTests.DataRowTests_Enums)");
151+
Assert.That.FailedTestCount(testResults, 0);
152+
}
153+
154+
[TestMethod]
155+
public void ExecuteRegular_DataRowTests()
156+
{
157+
// Arrange
158+
var assemblyPath = Path.IsPathRooted(TestAssembly) ? TestAssembly : this.GetAssetFullPath(TestAssembly);
159+
160+
// Act
161+
var testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~Regular_DataRowTests");
162+
var testResults = RunTests(assemblyPath, testCases);
163+
164+
// Assert
165+
Assert.That.TestsPassed(testResults,
166+
"DataRow1 (10)",
167+
"DataRow1 (20)",
168+
"DataRow1 (30)",
169+
"DataRow1 (40)",
170+
"DataRow2 (10,String parameter,True,False)",
171+
"DataRow2 (20,String parameter,True,False)",
172+
"DataRow2 (30,String parameter,True,False)",
173+
"DataRow2 (40,String parameter,True,False)",
174+
"DataRowTestMethodFailsWithInvalidArguments ()",
175+
"DataRowTestMethodFailsWithInvalidArguments (2)",
176+
"DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)",
177+
"DataRowTestDouble (10.01,20.01)",
178+
"DataRowTestDouble (10.02,20.02)",
179+
"DataRowTestMixed (1,10,10,10,10,10,10,10,10)",
180+
"DataRowTestMixed (2,10,10,10,10,10,10,10,10)",
181+
"DataRowTestMixed (3,10,10,10,10,10,10,10,10)",
182+
"DataRowTestMixed (4,10,10,10,10,10,10,10,10)",
183+
"NullValueInData ([email protected],abc123,)",
184+
"NullValueInData ([email protected],abc123,/unit/test)");
185+
186+
Assert.That.FailedTestCount(testResults, 0);
187+
}
188+
}
189+
}

test/E2ETests/Smoke.E2E.Tests/DataSourceTests.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/E2ETests/Smoke.E2E.Tests/Smoke.E2E.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<Compile Include="CompatTests.cs" />
5656
<Compile Include="TestProjectFSharpTests.cs" />
5757
<Compile Include="CustomTestExecutionExtensibilityTests.cs" />
58-
<Compile Include="DataSourceTests.cs" />
58+
<Compile Include="TestDataSourceTests.cs" />
5959
<Compile Include="DeploymentTests.cs" />
6060
<Compile Include="DesktopCSharpCLITests.cs" />
6161
<Compile Include="ParallelExecutionTests.cs" />

0 commit comments

Comments
 (0)