not able to print output to console window while running xunit tests
Asked Answered
R

3

7
public class test2InAnotherProject
{
    private readonly ITestOutputHelper output;

    public test2InAnotherProject(ITestOutputHelper output)
    {
        this.output = output;
    }
    int Diff(int a, int b)
    {
        return (a - b);
    }
    int Div(int a, int b)
    {
        return (b / a);
    }

    [Fact]
    public void Test2()
    {
        int a = 2, b = 4;

        output.WriteLine("Test1: Project 2 in old library");
        int c = Diff(a, b);
        Assert.Equal(c, (a - b));
        output.WriteLine("Test1: Asssert done Project 2 in old library");
    }

    [Fact]
    public void Test3()
    {
        int a = 2, b = 4;

        output.WriteLine("Test2: Project 2 in old library");
        int c = Div(a, b);
        Assert.Equal(c, (float)((b / a)));
        output.WriteLine("Test2: Assert done Project 2 in old library");
    }
}

trying to print those lines when test is run through command prompt by using the command

dotnet test --no-build

Tried Console.Writeline, after which i tried with Output.WriteLine. Even when i run from Visual Studio am not able to get those lines printed in output window.

Raynor answered 20/3, 2019 at 10:7 Comment(0)
E
14

Indeed there is no output with Console.WriteLine. And the ITestOutputHelper output is not shown in the Output window. Instead, when you click on the test in the Test Explorer, then there is an Output link. Click on that link to see the output.

To show the test output on the command line, use dotnet test --logger "console;verbosity=detailed".

enter image description here

Erlin answered 27/6, 2019 at 13:20 Comment(1)
am using the xunit runner class and making another application to run a passed dll tests. dll of test2InAnotherProject is passed to xunit runner implementation which runs the dll in console window.So i would be looking to output to console window when the application is running.Raynor
P
1

Running dotnete test from within powershell console like package Manager console or powershell console ISE, you can get ALL the Console.WriteLine output for Xunit project.

In powershel ISE, run this script:

cls
cd 'path/to/project/test/folder'
dotnet test

Also any Console.WriteLine(..) in the source code is displayed in the PS console.

Pimpernel answered 29/5, 2020 at 23:35 Comment(0)
A
-1

Please note that you can use ITestOutputHelper and it should write output.

Please refer this documentation for more details.

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);
    }
}
Adamina answered 20/3, 2019 at 21:30 Comment(1)
according to my code. i have already using ITestOutputHelper still it didn't work.Raynor

© 2022 - 2024 — McMap. All rights reserved.