Skip to content
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
5 changes: 4 additions & 1 deletion src/TestFramework/TestFramework/Assertions/Assert.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public sealed partial class Assert
public static void Fail(string message = "")
{
TelemetryCollector.TrackAssertionCall("Assert.Fail");
ThrowAssertFailed("Assert.Fail", BuildUserMessage(message));

StructuredAssertionMessage structured = new(string.Empty);
structured.WithUserMessage(message);
ThrowAssertFailed(structured);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public static void Inconclusive(string message = "")
TelemetryCollector.TrackAssertionCall("Assert.Inconclusive");

string userMessage = BuildUserMessage(message);
throw new AssertInconclusiveException(
FormatAssertionFailed("Assert.Inconclusive", userMessage));
throw new AssertInconclusiveException(FormatInconclusive(userMessage));
}

private static string FormatInconclusive(string? message)
=> FormatPrefixAndMessage(FrameworkMessages.InconclusivePrefix, message);
}
13 changes: 8 additions & 5 deletions src/TestFramework/TestFramework/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,16 @@ private static string NormalizeCallSitePlaceholder(string placeholderOrParamName
private static string FormatAssertionFailed(string assertionName, string? message)
{
string failedMessage = string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, assertionName);
return string.IsNullOrWhiteSpace(message)
? failedMessage
: message![0] is '\n' or '\r'
? string.Concat(failedMessage, message)
: $"{failedMessage} {message}";
return FormatPrefixAndMessage(failedMessage, message);
}

private static string FormatPrefixAndMessage(string prefix, string? message)
=> string.IsNullOrWhiteSpace(message)
? prefix
: message![0] is '\n' or '\r'
? string.Concat(prefix, message)
: $"{prefix} {message}";

/// <summary>
/// Builds the formatted message using the given user format message and parameters.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ Actual: {2}</value>
<data name="AssertionFailed" xml:space="preserve">
<value>{0} failed.</value>
</data>
<data name="InconclusivePrefix" xml:space="preserve">
<value>Assert.Inconclusive.</value>
</data>
<data name="IsInstanceOfFailMsg" xml:space="preserve">
<value>{0} Expected type:&lt;{1}&gt;. Actual type:&lt;{2}&gt;.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Skutečnost: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">Vlastnost TestContext.{0} souvisí s aktuálním testem a není k dispozici během sestavení nebo používání testovacích přípravků tříd.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Tatsächlich: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">Die Eigenschaft „TestContext.{0}“ im Zusammenhang mit dem aktuellen Test steht während Assembly- oder Klassenfixierungen nicht zur Verfügung.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Real: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">La propiedad "TestContext.{0}" está relacionado con la prueba actual y no está disponible durante los accesorios de ensamblado o clase.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Réel : {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">La propriété « TestContext.{0} », liée au test en cours, n’est pas disponible pendant les fixtures d’assembly ou de classe.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Effettivo: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">La proprietà 'TestContext.{0}' relativa al test corrente non è disponibile durante le fixture di assembly o classe.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Actual: {2}</source>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">プロパティ 'TestContext.{0}' は現在のテストに関連しており、アセンブリまたはクラス フィクスチャの間は使用できません。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Actual: {2}</source>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">현재 테스트와 관련된 'TestContext.{0}' 속성은 어셈블리나 클래스 픽스처 실행 중에는 사용할 수 없습니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Rzeczywiste: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">Właściwość „TestContext.{0}” jest powiązana z bieżącym testem i nie jest dostępna podczas montażu lub konfiguracji klasy.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Real: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">A propriedade "TestContext.{0}" está relacionada ao teste atual não está disponível durante os acessórios de assembly ou classe.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Actual: {2}</source>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">Свойство "TestContext.{0}" связано с текущим тестом и недоступно во время выполнения средств тестирования сборок или классов.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Gerçekte olan: {2}</target>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">'TestContext.{0}' özelliği, derleme veya sınıf sabitlemeleri sırasında mevcut testle ilişkili değildir.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Actual: {2}</source>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">属性‘TestContext.{0}’与当前测试相关,在程序集或类固定例程期间不可用。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Actual: {2}</source>
<target state="new">Expected collection to contain a specific number of elements.</target>
<note />
</trans-unit>
<trans-unit id="InconclusivePrefix">
<source>Assert.Inconclusive.</source>
<target state="new">Assert.Inconclusive.</target>
<note />
</trans-unit>
<trans-unit id="InvalidAccessToTestContextProperty">
<source>The property 'TestContext.{0}' is related to current test is not available during assembly or class fixtures.</source>
<target state="translated">屬性 'TestContext.{0}' 與目前測試相關,無法在組件或類別測試夾具期間使用。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public async Task TestDifferentGenericMethodTestCases()
The generic test method 'NonParameterizedTestMethod' doesn't have arguments, so the generic parameter cannot be inferred\.
.+?
failed ParameterizedMethodSimple \(1\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodSimple' did run with parameter '1' and type 'System\.Byte'\.
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodSimple' did run with parameter '1' and type 'System\.Byte'\.
.+?
failed ParameterizedMethodSimple \(2\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodSimple' did run with parameter '2' and type 'System\.Int32'\.
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodSimple' did run with parameter '2' and type 'System\.Int32'\.
.+?
failed ParameterizedMethodSimple \("Hello world"\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodSimple' did run with parameter 'Hello world' and type 'System\.String'\.
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodSimple' did run with parameter 'Hello world' and type 'System\.String'\.
.+?
failed ParameterizedMethodSimple \(null\) \((\d+s )?\d+ms\)
Test method TestClass\.ParameterizedMethodSimple threw exception:
Expand All @@ -44,7 +44,7 @@ public async Task TestDifferentGenericMethodTestCases()
System\.InvalidOperationException: Found two conflicting types for generic parameter 'T2'\. The conflicting types are 'System\.Byte' and 'System\.Int32'\.
.+?
failed ParameterizedMethodTwoGenericParametersAndFourMethodParameters \(null,"Hello world","Hello again",3\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodTwoGenericParametersAndFourMethodParameters' did run with parameters '<null>', 'Hello world', 'Hello again', '3' and generic types 'System\.Int32', 'System\.String'\.
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodTwoGenericParametersAndFourMethodParameters' did run with parameters '<null>', 'Hello world', 'Hello again', '3' and generic types 'System\.Int32', 'System\.String'\.
.+?
failed ParameterizedMethodTwoGenericParametersAndFourMethodParameters \("Hello hello","Hello world",null,null\) \((\d+s )?\d+ms\)
Test method TestClass\.ParameterizedMethodTwoGenericParametersAndFourMethodParameters threw exception:
Expand All @@ -70,10 +70,10 @@ Cannot create an instance of T\[] because Type\.ContainsGenericParameters is tru
Cannot create an instance of T\[] because Type\.ContainsGenericParameters is true\.
.+?
failed ParameterizedMethodWithNestedGeneric \(System\.Collections\.Generic\.List`1\[System.String],System\.Collections\.Generic\.List`1\[System.String]\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodWithNestedGeneric' did run with first list \[Hello, World] and second list \[Unit, Testing]
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodWithNestedGeneric' did run with first list \[Hello, World] and second list \[Unit, Testing]
.+?
failed ParameterizedMethodWithNestedGeneric \(System\.Collections\.Generic\.List`1\[System.Int32],System\.Collections\.Generic\.List`1\[System.Int32]\) \((\d+s )?\d+ms\)
Assert\.Fail failed\. Test method 'ParameterizedMethodWithNestedGeneric' did run with first list \[0, 1] and second list \[2, 3]
Assertion failed\.[\r\n]+\s+Test method 'ParameterizedMethodWithNestedGeneric' did run with first list \[0, 1] and second list \[2, 3]
.+?
""", RegexOptions.Singleline);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ TestMethod5 executed 4 times.
""");

testHostResult.AssertOutputContains("failed TestMethod5");
testHostResult.AssertOutputContains("Assert.Fail failed. Failing TestMethod4. Attempts: 4 (from TestContext: 4)");
testHostResult.AssertOutputMatchesRegex(
"""Assertion failed\.[\r\n]+\s+Failing TestMethod4\. Attempts: 4 \(from TestContext: 4\)""");
testHostResult.AssertOutputContainsSummary(failed: 1, passed: 4, skipped: 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ public sealed class STAThreadingTests : AcceptanceTestBase<STAThreadingTests.Tes
[Ignore("Tracked by https://github.com/microsoft/testfx/issues/8313. MSTest does not inspect [STAThread] on the entry-point Main; this scenario only ever passed when the async chain between MTP and the test runner happened to complete synchronously, which no longer holds on the Windows Debug leg after recent MTP refactors. Re-enable when implicit STA detection from Main is implemented.")]
public async Task TestMethodThreading_MainIsSTAThread_OnWindows_NoRunsettingsProvided_ThreadIsSTA(string tfm)
{
// Test cannot work on non-Windows OSes as the main method is marked with [STAThread]
// Test cannot work on non-Windows OSes as the main method is marked with [STAThread].
var testHost = TestHost.LocateFrom(AssetFixture.TargetAssetPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync(
environmentVariables: new()
{
["MSTEST_THREAD_STATE_IS_STA"] = "1",
},
cancellationToken: TestContext.CancellationToken);
TestHostResult testHostResult = await testHost.ExecuteAsync(cancellationToken: TestContext.CancellationToken);

testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContains("Passed!");
Expand Down Expand Up @@ -177,11 +172,13 @@ public ValueTask TestMethod5()
private static void AssertCorrectThreadApartmentState()
{
var apartmentState = Thread.CurrentThread.GetApartmentState();
if (Environment.GetEnvironmentVariable("MSTEST_THREAD_STATE_IS_STA") == "1")
string expectedThreadStateIsSta = Environment.GetEnvironmentVariable("MSTEST_THREAD_STATE_IS_STA");

if (expectedThreadStateIsSta == "1")
{
Assert.AreEqual(ApartmentState.STA, apartmentState);
}
else
else if (expectedThreadStateIsSta == "0")
Comment thread
Evangelink marked this conversation as resolved.
{
Assert.AreNotEqual(ApartmentState.STA, apartmentState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task AssertFailIsHardFailure_ThrowsImmediately()
// Assert.Fail is a hard assertion — it throws immediately, even within a scope.
// The second Assert.Fail should not be reached.
testHostResult.AssertOutputMatchesRegex(
"""failed AssertFailIsHardFailure \(\d+ms\)[\s\S]+Assert\.Fail failed\. hard failure""");
"""failed AssertFailIsHardFailure \(\d+ms\)[\s\S]+Assertion failed\.[\r\n]+\s+hard failure""");
Comment thread
Evangelink marked this conversation as resolved.
testHostResult.AssertOutputDoesNotContain("second failure");
}

Expand Down
Loading
Loading