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

TestTimeout in RunSettings #403

Merged
merged 2 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
foreach (var methodInfo in classType.GetTypeInfo().DeclaredMethods)
{
// Update test initialize/cleanup method
this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, isBase: false, instanceMethods: instanceMethods, testInitializeAttributeType: testInitializeAttributeType, testCleanupAttributeType: testCleanupAttributeType);
this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, isBase: false, instanceMethods: instanceMethods, testInitializeAttributeType: testInitializeAttributeType, testCleanupAttributeType: testCleanupAttributeType);

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

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

return timeoutAttribute.Timeout;
}
else if (globalTimeout > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we asked @pvlakshm about precedence order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when we passing runsettings arguments through commandline??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
return globalTimeout;
}

return TestMethodInfo.TimeoutWhenNotSet;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Adapter/MSTest.CoreAdapter/MSTestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public MSTestSettings()
this.ForcedLegacyMode = false;
this.TestSettingsFile = null;
this.DisableParallelization = false;
this.TestTimeout = 0;
}

/// <summary>
Expand Down Expand Up @@ -143,6 +144,11 @@ private set
/// </remarks>
public bool DisableParallelization { get; private set; }

/// <summary>
/// Gets specified global test case timeout
/// </summary>
public int TestTimeout { get; private set; }

/// <summary>
/// Populate settings based on existing settings object.
/// </summary>
Expand All @@ -157,6 +163,7 @@ public static void PopulateSettings(MSTestSettings settings)
CurrentSettings.ParallelizationWorkers = settings.ParallelizationWorkers;
CurrentSettings.ParallelizationScope = settings.ParallelizationScope;
CurrentSettings.DisableParallelization = settings.DisableParallelization;
CurrentSettings.TestTimeout = settings.TestTimeout;
}

/// <summary>
Expand Down Expand Up @@ -274,6 +281,7 @@ private static MSTestSettings ToSettings(XmlReader reader)
// <CaptureTraceOutput>true</CaptureTraceOutput>
// <MapInconclusiveToFailed>false</MapInconclusiveToFailed>
// <EnableBaseClassTestMethodsFromOtherAssemblies>false</EnableBaseClassTestMethodsFromOtherAssemblies>
// <TestTimeout>5000<TestTimeout>
// <Parallelize>
// <Workers>4</Workers>
// <Scope>TestClass</Scope>
Expand Down Expand Up @@ -362,6 +370,16 @@ private static MSTestSettings ToSettings(XmlReader reader)
break;
}

case "TESTTIMEOUT":
{
if (int.TryParse(reader.ReadInnerXml(), out int testTimeout) && testTimeout > 0)
{
settings.TestTimeout = testTimeout;
}

break;
}

default:
{
PlatformServiceProvider.Instance.SettingsProvider.Load(reader.ReadSubtree());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void TestInit()
public void Cleanup()
{
PlatformServiceProvider.Instance = null;
MSTestSettings.Reset();
}

#region GetTestMethodInfo tests
Expand Down Expand Up @@ -815,6 +816,81 @@ public void GetTestMethodInfoShouldThrowWhenTimeoutIsIncorrect()
Assert.AreEqual(expectedMessage, exception.Message);
}

[TestMethodV1]
public void GetTestMethodInfoWhenTimeoutAttributeNotSetShouldReturnTestMethodInfoWithGlobalTimeout()
{
string runSettingxml =
@"<RunSettings>
<MSTestV2>
<TestTimeout>4000</TestTimeout>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also add one negative test case where timeout is invalid(negative value or non-integer value)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

</MSTestV2>
</RunSettings>";

MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

var type = typeof(DummyTestClassWithTestMethods);
var methodInfo = type.GetMethod("TestMethod");
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);

var testMethodInfo = this.typeCache.GetTestMethodInfo(
testMethod,
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
false);

Assert.AreEqual(4000, testMethodInfo.TestMethodOptions.Timeout);
}

[TestMethodV1]
public void GetTestMethodInfoWhenTimeoutAttributeSetShouldReturnTimeoutBasedOnAtrributeEvenIfGlobalTimeoutSet()
{
string runSettingxml =
@"<RunSettings>
<MSTestV2>
<TestTimeout>4000</TestTimeout>
</MSTestV2>
</RunSettings>";

MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

var type = typeof(DummyTestClassWithTestMethods);
var methodInfo = type.GetMethod("TestMethodWithTimeout");
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);

this.mockReflectHelper.Setup(rh => rh.IsAttributeDefined(methodInfo, typeof(UTF.TimeoutAttribute), false))
.Returns(true);

var testMethodInfo = this.typeCache.GetTestMethodInfo(
testMethod,
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
false);

Assert.AreEqual(10, testMethodInfo.TestMethodOptions.Timeout);
}

[TestMethodV1]
public void GetTestMethodInfoForInvalidGLobalTimeoutShouldReturnTestMethodInfoWithTimeoutZero()
{
string runSettingxml =
@"<RunSettings>
<MSTestV2>
<TestTimeout>30.5</TestTimeout>
</MSTestV2>
</RunSettings>";

MSTestSettings.PopulateSettings(MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

var type = typeof(DummyTestClassWithTestMethods);
var methodInfo = type.GetMethod("TestMethod");
var testMethod = new TestMethod(methodInfo.Name, type.FullName, "A", isAsync: false);

var testMethodInfo = this.typeCache.GetTestMethodInfo(
testMethod,
new TestContextImplementation(testMethod, null, new Dictionary<string, object>()),
false);

Assert.AreEqual(0, testMethodInfo.TestMethodOptions.Timeout);
}

[TestMethodV1]
public void GetTestMethodInfoShouldReturnTestMethodInfoForMethodsAdornedWithADerivedTestMethodAttribute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@ public void CaptureDebugTracesShouldBeConsumedFromRunSettingsWhenSpecified()
Assert.AreEqual(adapterSettings.CaptureDebugTraces, false);
}

[TestMethod]
public void TestTimeoutShouldBeConsumedFromRunSettingsWhenSpecified()
{
string runSettingxml =
@"<RunSettings>
<MSTestV2>
<TestTimeout>4000</TestTimeout>
</MSTestV2>
</RunSettings>";

MSTestSettings adapterSettings = MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias);

Assert.AreEqual(adapterSettings.TestTimeout, 4000);
}

[TestMethod]
public void TestTimeoutShouldBeSetToZeroIfNotSpecifiedInRunSettings()
{
string runSettingxml =
@"<RunSettings>
<MSTestV2>
</MSTestV2>
</RunSettings>";

MSTestSettings adapterSettings = MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias);

Assert.AreEqual(adapterSettings.TestTimeout, 0);
}

[TestMethod]
public void ParallelizationSettingsShouldNotBeSetByDefault()
{
Expand Down Expand Up @@ -524,7 +553,7 @@ public void GetSettingsShouldOnlyPassTheElementSubTreeToPlatformService()
actualReader.Read();
observedxml = actualReader.ReadOuterXml();
}
});
});

MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsName);
Assert.AreEqual(expectedrunSettingxml, observedxml);
Expand Down