|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using Xunit.Abstractions; |
| 4 | + |
| 5 | +namespace Bunit.TestAssets; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Wrap a test action or function in a try-catch block that captures a dump file if the test fails. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// This requires the <c>dotnet-dump</c> tool to be installed as a local dotnet tool. |
| 12 | +/// </remarks> |
| 13 | +public static class DumpCapture |
| 14 | +{ |
| 15 | + |
| 16 | + public static async Task OnFailureAsync( |
| 17 | + Action testAction, |
| 18 | + ITestOutputHelper outputHelper, |
| 19 | + [CallerMemberName] string testName = "", |
| 20 | + [CallerFilePath] string testFilePath = "") |
| 21 | + { |
| 22 | + try |
| 23 | + { |
| 24 | + testAction(); |
| 25 | + } |
| 26 | + catch |
| 27 | + { |
| 28 | + await CaptureDump(testName, testFilePath, outputHelper); |
| 29 | + throw; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static async Task OnFailureAsync( |
| 34 | + Func<Task> testAction, |
| 35 | + ITestOutputHelper outputHelper, |
| 36 | + [CallerMemberName] string testName = "", |
| 37 | + [CallerFilePath] string testFilePath = "") |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + await testAction(); |
| 42 | + } |
| 43 | + catch |
| 44 | + { |
| 45 | + await CaptureDump(testName, testFilePath, outputHelper); |
| 46 | + throw; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private static async Task CaptureDump(string testName, string testFilePath, ITestOutputHelper outputHelper) |
| 51 | + { |
| 52 | +#if NETSTANDARD2_1 |
| 53 | + var processId = Process.GetCurrentProcess().Id; |
| 54 | +#else |
| 55 | + var processId = Environment.ProcessId; |
| 56 | +#endif |
| 57 | + var dumpFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"{Path.GetFileNameWithoutExtension(testFilePath)}-{testName}-wait-failed-{Guid.NewGuid()}.dmp"); |
| 58 | + // Attempt to start the dotnet-dump process |
| 59 | + var startInfo = new ProcessStartInfo |
| 60 | + { |
| 61 | + FileName = "dotnet", |
| 62 | + Arguments = $"dotnet-dump collect -p {processId} -o {dumpFilePath}", |
| 63 | + RedirectStandardOutput = true, |
| 64 | + RedirectStandardError = true, |
| 65 | + UseShellExecute = false, |
| 66 | + CreateNoWindow = true |
| 67 | + }; |
| 68 | + using var process = Process.Start(startInfo); |
| 69 | + if (process is null) |
| 70 | + { |
| 71 | + outputHelper.WriteLine(" Failed to start dotnet-dump process."); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | +#if NETSTANDARD2_1 |
| 76 | + process.WaitForExit(); |
| 77 | +#else |
| 78 | + await process.WaitForExitAsync(); |
| 79 | +#endif |
| 80 | + var output = await process.StandardOutput.ReadToEndAsync(); |
| 81 | + var error = await process.StandardError.ReadToEndAsync(); |
| 82 | + outputHelper.WriteLine($"Dump status: {{process.ExitCode}}. Dump file: {dumpFilePath}"); |
| 83 | + if (!string.IsNullOrWhiteSpace(output)) |
| 84 | + { |
| 85 | + outputHelper.WriteLine($"Dump output: {output}"); |
| 86 | + } |
| 87 | + if (!string.IsNullOrWhiteSpace(error)) |
| 88 | + { |
| 89 | + outputHelper.WriteLine($"Dump error: {error}"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments