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
16 changes: 16 additions & 0 deletions src/Adapter/MSTest.TestAdapter/PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ as `Playwright`, produce a warning and are not applied by MSTest. Use the extens
native Microsoft.Testing.Platform configuration when available. VSTest loggers,
data collectors, and unsupported `RunConfiguration` entries retain their own warnings.

## Result file attachments

When running on Microsoft.Testing.Platform, `TestContext.AddResultFile` registers
an existing file as a test attachment. MSTest preserves successfully copied
attachments in visible `MSTest-<random-id>` folders under the configured results
directory so the published paths remain readable after the test host exits.
Original files are not moved or deleted by this copying step.

These copies are final test results: apply your results-directory retention policy
to remove them when they are no longer needed. TRX reporting can create an
additional copy in its own attachment layout; follow the report's references
rather than selecting a file by name from the entire results directory.
Retry tooling can also publish additional copies from the final attempt.
If an attachment cannot be copied, MSTest reports its original path so consumers
can surface their usual missing-file or copy-failure diagnostics.

## Documentation

For installation and configuration guidance, see <https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-getting-started>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal sealed class MSTestTestFramework : ITestFramework, IDataProducer, IDisp
private readonly CountdownEvent _incomingRequestCounter = new(1);
private int _nextResultFileStagingDirectory;
private int _resultFilesStagingDirectoryCreated;
private int _hasDurableResultFiles;
private bool? _isTrxEnabled;
private bool _isDisposed;
private SessionUid? _sessionUid;
Expand All @@ -59,9 +60,10 @@ public MSTestTestFramework(MSTestExtension extension, Func<IEnumerable<Assembly>
_configuration = new(serviceProvider.GetConfiguration());
_loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
_gracefulStopCapability = (MSTestGracefulStopTestExecutionCapability)capabilities.GetCapability<IGracefulStopTestExecutionCapability>()!;
// Retained roots need more entropy than a temporary cache without excessive legacy path growth.
_resultFilesStagingDirectory = Path.Combine(
serviceProvider.GetConfiguration().GetTestResultDirectory(),
$".mstest-{Guid.NewGuid():N}"[..16]);
Path.GetFullPath(serviceProvider.GetConfiguration().GetTestResultDirectory()),
$"MSTest-{Guid.NewGuid().ToString("N")[..20]}");
((Microsoft.Testing.Platform.Services.ServiceProvider)serviceProvider)
.AddService(new ResultFilesStagingDirectoryCleanup(CleanupResultFilesStagingDirectory));
PlatformServiceProvider.Instance.AdapterTraceLogger = new MTPTraceLogger(_loggerFactory.CreateLogger("mstest-trace"));
Expand Down Expand Up @@ -286,6 +288,7 @@ private void StageResultFiles(TestResult result)
GetPathForFileSystemAccess(stagedPath));

durableResultFiles.Add(stagedPath);
Volatile.Write(ref _hasDurableResultFiles, 1);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Expand Down Expand Up @@ -355,7 +358,9 @@ private static bool ContainsRelativePathSegments(string path)

private void CleanupResultFilesStagingDirectory()
{
if (Volatile.Read(ref _resultFilesStagingDirectoryCreated) == 0)
// Published copies must outlive the host; only unsuccessful staging attempts can be removed.
if (Volatile.Read(ref _resultFilesStagingDirectoryCreated) == 0
|| Volatile.Read(ref _hasDurableResultFiles) != 0)
{
return;
}
Expand Down
Loading
Loading