Skip to content

Commit 6d10ac9

Browse files
authored
TestTimeout in RunSettings (#403)
* Adding global test timeout configurable via runsettings * Adding negative test as per comments.
1 parent bad3e20 commit 6d10ac9

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
286286
foreach (var methodInfo in classType.GetTypeInfo().DeclaredMethods)
287287
{
288288
// Update test initialize/cleanup method
289-
this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, isBase: false, instanceMethods: instanceMethods, testInitializeAttributeType: testInitializeAttributeType, testCleanupAttributeType: testCleanupAttributeType);
289+
this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, isBase: false, instanceMethods: instanceMethods, testInitializeAttributeType: testInitializeAttributeType, testCleanupAttributeType: testCleanupAttributeType);
290290

291291
if (this.IsAssemblyOrClassInitializeMethod(methodInfo, classInitializeAttributeType))
292292
{
@@ -634,6 +634,7 @@ private int GetTestTimeout(MethodInfo methodInfo, TestMethod testMethod)
634634
{
635635
Debug.Assert(methodInfo != null, "TestMethod should be non-null");
636636
var timeoutAttribute = this.reflectionHelper.GetAttribute<TimeoutAttribute>(methodInfo);
637+
var globalTimeout = MSTestSettings.CurrentSettings.TestTimeout;
637638

638639
if (timeoutAttribute != null)
639640
{
@@ -645,6 +646,10 @@ private int GetTestTimeout(MethodInfo methodInfo, TestMethod testMethod)
645646

646647
return timeoutAttribute.Timeout;
647648
}
649+
else if (globalTimeout > 0)
650+
{
651+
return globalTimeout;
652+
}
648653

649654
return TestMethodInfo.TimeoutWhenNotSet;
650655
}

src/Adapter/MSTest.CoreAdapter/MSTestSettings.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public MSTestSettings()
5555
this.ForcedLegacyMode = false;
5656
this.TestSettingsFile = null;
5757
this.DisableParallelization = false;
58+
this.TestTimeout = 0;
5859
}
5960

6061
/// <summary>
@@ -143,6 +144,11 @@ private set
143144
/// </remarks>
144145
public bool DisableParallelization { get; private set; }
145146

147+
/// <summary>
148+
/// Gets specified global test case timeout
149+
/// </summary>
150+
public int TestTimeout { get; private set; }
151+
146152
/// <summary>
147153
/// Populate settings based on existing settings object.
148154
/// </summary>
@@ -157,6 +163,7 @@ public static void PopulateSettings(MSTestSettings settings)
157163
CurrentSettings.ParallelizationWorkers = settings.ParallelizationWorkers;
158164
CurrentSettings.ParallelizationScope = settings.ParallelizationScope;
159165
CurrentSettings.DisableParallelization = settings.DisableParallelization;
166+
CurrentSettings.TestTimeout = settings.TestTimeout;
160167
}
161168

162169
/// <summary>
@@ -274,6 +281,7 @@ private static MSTestSettings ToSettings(XmlReader reader)
274281
// <CaptureTraceOutput>true</CaptureTraceOutput>
275282
// <MapInconclusiveToFailed>false</MapInconclusiveToFailed>
276283
// <EnableBaseClassTestMethodsFromOtherAssemblies>false</EnableBaseClassTestMethodsFromOtherAssemblies>
284+
// <TestTimeout>5000<TestTimeout>
277285
// <Parallelize>
278286
// <Workers>4</Workers>
279287
// <Scope>TestClass</Scope>
@@ -362,6 +370,16 @@ private static MSTestSettings ToSettings(XmlReader reader)
362370
break;
363371
}
364372

373+
case "TESTTIMEOUT":
374+
{
375+
if (int.TryParse(reader.ReadInnerXml(), out int testTimeout) && testTimeout > 0)
376+
{
377+
settings.TestTimeout = testTimeout;
378+
}
379+
380+
break;
381+
}
382+
365383
default:
366384
{
367385
PlatformServiceProvider.Instance.SettingsProvider.Load(reader.ReadSubtree());

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void TestInit()
5454
public void Cleanup()
5555
{
5656
PlatformServiceProvider.Instance = null;
57+
MSTestSettings.Reset();
5758
}
5859

5960
#region GetTestMethodInfo tests
@@ -815,6 +816,81 @@ public void GetTestMethodInfoShouldThrowWhenTimeoutIsIncorrect()
815816
Assert.AreEqual(expectedMessage, exception.Message);
816817
}
817818

819+
[TestMethodV1]
820+
public void GetTestMethodInfoWhenTimeoutAttributeNotSetShouldReturnTestMethodInfoWithGlobalTimeout()
821+
{
822+
string runSettingxml =
823+
@"<RunSettings>
824+
<MSTestV2>
825+
<TestTimeout>4000</TestTimeout>
826+
</MSTestV2>
827+
</RunSettings>";
828+
829+
MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));
830+
831+
var type = typeof(DummyTestClassWithTestMethods);
832+
var methodInfo = type.GetMethod("TestMethod");
833+
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);
834+
835+
var testMethodInfo = this.typeCache.GetTestMethodInfo(
836+
testMethod,
837+
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
838+
false);
839+
840+
Assert.AreEqual(4000, testMethodInfo.TestMethodOptions.Timeout);
841+
}
842+
843+
[TestMethodV1]
844+
public void GetTestMethodInfoWhenTimeoutAttributeSetShouldReturnTimeoutBasedOnAtrributeEvenIfGlobalTimeoutSet()
845+
{
846+
string runSettingxml =
847+
@"<RunSettings>
848+
<MSTestV2>
849+
<TestTimeout>4000</TestTimeout>
850+
</MSTestV2>
851+
</RunSettings>";
852+
853+
MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));
854+
855+
var type = typeof(DummyTestClassWithTestMethods);
856+
var methodInfo = type.GetMethod("TestMethodWithTimeout");
857+
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);
858+
859+
this.mockReflectHelper.Setup(rh => rh.IsAttributeDefined(methodInfo, typeof(UTF.TimeoutAttribute), false))
860+
.Returns(true);
861+
862+
var testMethodInfo = this.typeCache.GetTestMethodInfo(
863+
testMethod,
864+
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
865+
false);
866+
867+
Assert.AreEqual(10, testMethodInfo.TestMethodOptions.Timeout);
868+
}
869+
870+
[TestMethodV1]
871+
public void GetTestMethodInfoForInvalidGLobalTimeoutShouldReturnTestMethodInfoWithTimeoutZero()
872+
{
873+
string runSettingxml =
874+
@"<RunSettings>
875+
<MSTestV2>
876+
<TestTimeout>30.5</TestTimeout>
877+
</MSTestV2>
878+
</RunSettings>";
879+
880+
MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));
881+
882+
var type = typeof(DummyTestClassWithTestMethods);
883+
var methodInfo = type.GetMethod("TestMethod");
884+
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);
885+
886+
var testMethodInfo = this.typeCache.GetTestMethodInfo(
887+
testMethod,
888+
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
889+
false);
890+
891+
Assert.AreEqual(0, testMethodInfo.TestMethodOptions.Timeout);
892+
}
893+
818894
[TestMethodV1]
819895
public void GetTestMethodInfoShouldReturnTestMethodInfoForMethodsAdornedWithADerivedTestMethodAttribute()
820896
{

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTestSettingsTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,35 @@ public void CaptureDebugTracesShouldBeConsumedFromRunSettingsWhenSpecified()
195195
Assert.AreEqual(adapterSettings.CaptureDebugTraces, false);
196196
}
197197

198+
[TestMethod]
199+
public void TestTimeoutShouldBeConsumedFromRunSettingsWhenSpecified()
200+
{
201+
string runSettingxml =
202+
@"<RunSettings>
203+
<MSTestV2>
204+
<TestTimeout>4000</TestTimeout>
205+
</MSTestV2>
206+
</RunSettings>";
207+
208+
MSTestSettings adapterSettings = MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias);
209+
210+
Assert.AreEqual(adapterSettings.TestTimeout, 4000);
211+
}
212+
213+
[TestMethod]
214+
public void TestTimeoutShouldBeSetToZeroIfNotSpecifiedInRunSettings()
215+
{
216+
string runSettingxml =
217+
@"<RunSettings>
218+
<MSTestV2>
219+
</MSTestV2>
220+
</RunSettings>";
221+
222+
MSTestSettings adapterSettings = MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias);
223+
224+
Assert.AreEqual(adapterSettings.TestTimeout, 0);
225+
}
226+
198227
[TestMethod]
199228
public void ParallelizationSettingsShouldNotBeSetByDefault()
200229
{
@@ -524,7 +553,7 @@ public void GetSettingsShouldOnlyPassTheElementSubTreeToPlatformService()
524553
actualReader.Read();
525554
observedxml = actualReader.ReadOuterXml();
526555
}
527-
});
556+
});
528557

529558
MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsName);
530559
Assert.AreEqual(expectedrunSettingxml, observedxml);

0 commit comments

Comments
 (0)