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
77 changes: 65 additions & 12 deletions playground/TestPlatform.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void Main(string[] args)
// copy of TestPlatform that is similar to what we ship.
//
// The copying might trigger only on re-build, if you see outdated dependencies, Rebuild this project instead of just Build.
//
//
// Use this as playground for your debugging of end-to-end scenarios, it will automatically attach vstest.console and teshost
// sub-processes. It won't stop at entry-point automatically, don't forget to set your breakpoints, or remove VSTEST_DEBUG_NOBP
// from the environment variables of this project.
Expand All @@ -51,6 +51,7 @@ static void Main(string[] args)
<RunSettings>
<RunConfiguration>
<InIsolation>true</InIsolation>
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>
</RunSettings>
";
Expand All @@ -62,6 +63,58 @@ static void Main(string[] args)
r.RunTestsWithCustomTestHost(sources, sourceSettings, options, new TestRunHandler(), new DebuggerTestHostLauncher());
}

public class PlaygroundTestDiscoveryHandler : ITestDiscoveryEventsHandler, ITestDiscoveryEventsHandler2
Comment thread
Evangelink marked this conversation as resolved.
{
private int _testCasesCount;

public void HandleDiscoveredTests(IEnumerable<TestCase> discoveredTestCases)
{
Console.WriteLine($"[DISCOVERY.PROGRESS]");
Console.WriteLine(WriteTests(discoveredTestCases));
_testCasesCount += discoveredTestCases.Count();
}

public void HandleDiscoveryComplete(long totalTests, IEnumerable<TestCase> lastChunk, bool isAborted)
{
Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {isAborted}, tests count: {totalTests}");
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
}

public void HandleDiscoveryComplete(DiscoveryCompleteEventArgs discoveryCompleteEventArgs, IEnumerable<TestCase> lastChunk)
{
Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {discoveryCompleteEventArgs.IsAborted}, tests count: {discoveryCompleteEventArgs.TotalCount}, discovered count: {_testCasesCount}");
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
Console.WriteLine("Fully discovered:");
Console.WriteLine(WriteSources(discoveryCompleteEventArgs.FullyDiscoveredSources));
Console.WriteLine("Partially discovered:");
Console.WriteLine(WriteSources(discoveryCompleteEventArgs.PartiallyDiscoveredSources));
Console.WriteLine("Not discovered:");
Console.WriteLine(WriteSources(discoveryCompleteEventArgs.NotDiscoveredSources));
}

public void HandleLogMessage(TestMessageLevel level, string message)
{
Console.WriteLine($"[DISCOVERY.{level.ToString().ToUpper()}] {message}");
}

public void HandleRawMessage(string rawMessage)
{
Console.WriteLine($"[DISCOVERY.MESSAGE] {rawMessage}");
}

private static string WriteTests(IEnumerable<TestCase> testCases)
=> testCases?.Any() == true
? "\t" + string.Join("\n\t", testCases.Select(r => r.DisplayName))
: "\t<empty>";

private static string WriteSources(IEnumerable<string> sources)
=> sources?.Any() == true
? "\t" + string.Join("\n\t", sources)
: "\t<empty>";
}

public class TestRunHandler : ITestRunEventsHandler
{

Expand All @@ -76,33 +129,33 @@ public void HandleLogMessage(TestMessageLevel level, string message)

public void HandleRawMessage(string rawMessage)
{
Console.WriteLine($"[MESSAGE]: { rawMessage}");
Console.WriteLine($"[RUN.MESSAGE]: {rawMessage}");
}

public void HandleTestRunComplete(TestRunCompleteEventArgs testRunCompleteArgs, TestRunChangedEventArgs lastChunkArgs, ICollection<AttachmentSet> runContextAttachments, ICollection<string> executorUris)
{
Console.WriteLine($"[COMPLETE]: err: { testRunCompleteArgs.Error }, lastChunk: {WriteTests(lastChunkArgs?.NewTestResults)}");
Console.WriteLine($"[RUN.COMPLETE]: err: {testRunCompleteArgs.Error}, lastChunk:");
Console.WriteLine(WriteTests(lastChunkArgs?.NewTestResults));
}

public void HandleTestRunStatsChange(TestRunChangedEventArgs testRunChangedArgs)
{
Console.WriteLine($"[PROGRESS - NEW RESULTS]: {WriteTests(testRunChangedArgs.NewTestResults)}");
Console.WriteLine($"[RUN.PROGRESS]");
Console.WriteLine(WriteTests(testRunChangedArgs.NewTestResults));
}

public int LaunchProcessWithDebuggerAttached(TestProcessStartInfo testProcessStartInfo)
{
throw new NotImplementedException();
}

private string WriteTests(IEnumerable<TestResult> testResults)
{
return WriteTests(testResults?.Select(t => t.TestCase));
}
private static string WriteTests(IEnumerable<TestResult> testResults)
=> WriteTests(testResults?.Select(t => t.TestCase));

private string WriteTests(IEnumerable<TestCase> testCases)
{
return testCases == null ? null : "\t" + string.Join("\n\t", testCases.Select(r => r.DisplayName));
}
private static string WriteTests(IEnumerable<TestCase> testCases)
=> testCases?.Any() == true
? "\t" + string.Join("\n\t", testCases.Select(r => r.DisplayName))
: "\t<empty>";
}

internal class DebuggerTestHostLauncher : ITestHostLauncher2
Expand Down
23 changes: 23 additions & 0 deletions src/Microsoft.TestPlatform.Common/Utilities/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;

using SystemDebug = System.Diagnostics.Debug;

namespace Microsoft.TestPlatform;

[SuppressMessage("ApiDesign", "RS0030:Do not used banned APIs", Justification = "Replacement API to allow nullable hints for compiler")]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could remove this because I haven't yet banned the other symbol but keeping it would simplify merge for later changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Keep it.

internal static class Debug
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am always forcing this one but FYI in recent versions the system Debug.Assert methods already support it.

{
/// <inheritdoc cref="SystemDebug.Assert(bool)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b)
=> SystemDebug.Assert(b);

/// <inheritdoc cref="SystemDebug.Assert(bool, string)"/>
[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool b, string message)
=> SystemDebug.Assert(b, message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,20 +572,21 @@ private void OnDiscoveryMessageReceived(ITestDiscoveryEventsHandler2 discoveryEv
discoveryEventsHandler.HandleDiscoveredTests(testCases);
break;
case MessageType.DiscoveryComplete:
var discoveryCompletePayload =
_dataSerializer.DeserializePayload<DiscoveryCompletePayload>(data);
var discoveryCompleteEventArgs = new DiscoveryCompleteEventArgs(
discoveryCompletePayload.TotalTests,
discoveryCompletePayload.IsAborted,
discoveryCompletePayload.FullyDiscoveredSources,
discoveryCompletePayload.PartiallyDiscoveredSources,
discoveryCompletePayload.NotDiscoveredSources,
discoveryCompletePayload.DiscoveredExtensions);

discoveryCompleteEventArgs.Metrics = discoveryCompletePayload.Metrics;
var payload = _dataSerializer.DeserializePayload<DiscoveryCompletePayload>(data);
var discoveryCompleteEventArgs = new DiscoveryCompleteEventArgs
{
TotalCount = payload.TotalTests,
IsAborted = payload.IsAborted,
FullyDiscoveredSources = payload.FullyDiscoveredSources,
PartiallyDiscoveredSources = payload.PartiallyDiscoveredSources,
NotDiscoveredSources = payload.NotDiscoveredSources,
DiscoveredExtensions = payload.DiscoveredExtensions,
};

discoveryCompleteEventArgs.Metrics = payload.Metrics;
discoveryEventsHandler.HandleDiscoveryComplete(
discoveryCompleteEventArgs,
discoveryCompletePayload.LastDiscoveredTests);
payload.LastDiscoveredTests);
SetOperationComplete();
break;
case MessageType.TestMessage:
Expand Down
Loading