Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xunit add info to output

Does anyone know how to add additional info to the output console when running xUnit tests?

I'm using testdriven.net, but I don't think that is where my answer lies.

I am using a IUseFixture (actually IClassFixture from 2.0) to maintain data between tests. When a test fails I want to output some of that contextual data along with the failure and the stack trace that you usually get.

Does anyone know of a hook I can use?

like image 846
user3442348 Avatar asked Feb 26 '15 18:02

user3442348


People also ask

What is Fact attribute in xUnit?

xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.

Which is better xUnit or NUnit?

MSTest is concerned, the biggest difference between xUnit and the other two test frameworks (NUnit and MSTest) is that xUnit is much more extensible when compared to NUnit and MSTest.

How do I create a test report in xUnit?

One way to do it: add " --logger=trx" to the "dotnet test" command and then use the build step "Process xUnit test result report" from the "xUnit plugin". Use the option "MSTest-Version N/A (default) Pattern" and set pattern to "*/. trx". This is the answer!

Does xUnit have setup?

It is common for unit test classes to share setup and cleanup code (often called "test context"). xUnit.net offers several methods for sharing this setup and cleanup code, depending on the scope of things to be shared, as well as the expense associated with the setup and cleanup code.


1 Answers

You can use ITestOutputHelper to write any output to the test result view. Just let xUnit inject it into your constructor.

using Xunit;
using Xunit.Abstractions;
namespace xUnitTestOutput
{
    public class OutputTests
    {
        private readonly ITestOutputHelper _output;
        public OutputTests(ITestOutputHelper output)
        {
            _output = output;
        }
        [Fact]
        public void FirstOutputTest()
        {
            _output.WriteLine("This is output from the test!");
        }
    }
}
like image 71
n-develop Avatar answered Oct 11 '22 19:10

n-develop