I have a pipeline running some C# Xunit tests. I run a dotnet test
command where I specify --logger:trx
. Then at the end of the agent job I have a Publish Test Results
step.
When the pipeline is done, if I go to the "Tests" tab where it breaks down each test, for any failed test I can drill down to the default debug tab and see the error message and the stack trace. There's nothing listed under attachments though.
If I go to the Test Plans / Runs
section for the test, I can download the trx file that gets produced. The trx file has the full output for each test so I'm not sure why it isn't listed as an attachment for each test. Is there something special I should be doing?
When you use the PublishTestResults@2
task, it creates a Test Run with Test Results. The trx file is an attachment for the Test Run. The view you are referring to is the for the attachments associated with the individual Test Result.
To include attachments on the Test Result you would need to add the attachment for the test using TestContext.AddResultFile(..)
. For example:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyTests {
[TestClass]
public class MyTestFixture
{
public TestContext TestContext { get; set; }
[TestMethod]
public void CanAddAttachment()
{
TestContext.AddFile( "path/to/file.txt" );
}
}
}
Update: I noticed you've tagged xunit
. XUnit unfortunately does not include attachment support. Presently, NUnit and MSTest do. As of February 2024, the PublishTestResult@2
task has support for JUnit attachments.
To add additional logging into your xUnit tests, you can use the IOutputTestHelper. Any information you add will appear on the Debug tab of the Test Result:
using Xunit;
using Xunit.Abstractions;
public class MyTestClass
{
private readonly ITestOutputHelper output;
public MyTestClass(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void MyTest()
{
var temp = "my class!";
output.WriteLine("This is output from {0}", temp);
}
}