Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running `dotnet test` show output of the `dotnet vstest` output sink

Tags:

.net

vstest

Edit: Below is the former question originally posted with the title

xunit show ITestOutputHelper output when run in the console

After investigation (see comments), the question is still very much relevant and in need of an answer but it seems that it should be addressed to the vstest framework and around output sinks rather than xunit.

The specific question now is: When something is written to the dotnet vstest output sink, how do I get it to display when running dotnet test from the command line. Specifically I want to be able to set up a watcher with dotnet watch to run tests continuously and I care more about output than results (I am using this less as a testing system and more as a development notebook). And I don't want to be limited to visual studio or vscode.

Original Question:

In my test class I have

    [Fact]
    public void Test() {

       output.WriteLine("WHATEVER");

    }
    readonly ITestOutputHelper output;

    public GroceryTests(ITestOutputHelper output) => this.output = output;

When I run this through the vscode in-editor integration I can see the output in the Output pane. When I run it via dotnet test I do not. How do I get this output to show up via console?

like image 409
George Mauer Avatar asked Apr 07 '20 18:04

George Mauer


People also ask

Does dotnet use Vstest?

The dotnet vstest command is superseded by dotnet test , which can now be used to run assemblies.

Does dotnet test run in parallel?

Luckily, dotnet test using xUnit V2 runs the tests inside of a project (or even inside a solution) in parallel by default. So at least the tests inside of a project will be started in parallel.

What is DotNet vstest command?

The dotnet vstest command runs the VSTest.Console command-line application to run automated unit tests. Run tests from the specified assemblies. Separate multiple test assembly names with spaces. Wildcards are supported. Runs the tests in blame mode. This option is helpful in isolating the problematic tests causing test host to crash.

How to output the test results from DotNet test?

Currently there is no way to output the test results from dotnet test. Our CI requires a file in order to determine if a job has failed or not plus it allows us to gather data from our builds. Would be useful for dotnet test to accept a output path for test results. @Greg15153 can you provide some more details?

How do I run a vstest test on published output?

Test published output with dotnet vstest. You can run tests on already published output by using the dotnet vstest command. This will work on xUnit, MSTest, and NUnit tests. Simply locate the DLL file that was part of your published output and run: Where <MyPublishedTests> is the name of your published test project.

Why does the DotNet Cli’s test fail on non-test projects?

The dotnet cli’s test command can be run on any msbuild project or solution, yet it fails when run on non-test projects and prints errors like: No test is available in [SomeApp].dll. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate and try again. Test Run Aborted.


2 Answers

Just to share what worked for me (I am no expert of vstest sink stuff, so probably can't answer deeper questions):

I have a simple test like this:

public class UnitTest1
{
    private readonly ITestOutputHelper _output;

    public UnitTest1(ITestOutputHelper output)
    {
        _output = output;
    }

    [Fact]
    public void Test1()
    {
        _output.WriteLine("My message.");
    }
}

So if I watch / run this test in console:

dotnet watch test --logger:"console;verbosity=detailed"

I see output like:

  V XUnitTestProject1.UnitTest1.Test1 [5ms]
  Standard Output Messages:
 My message.

I am using .NET Core SDK 3.1.201.

like image 76
weichch Avatar answered Oct 09 '22 01:10

weichch


@weichch's answer works for me even without any specific log sink usage. The command dotnet test -l "console;verbosity=detailed" is showing me all the Console.WriteLines of my tests just fine. And doesn't clutter much.

Note that you can add --filter="Name=<MethodName>" to the command to run tests with such name.

My tests all have attribute [NonParallelizable].
Using dotnet 5

The command is also mentioned in this answer at github.

like image 29
bypasswer Avatar answered Oct 09 '22 00:10

bypasswer