Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ReSharper, how to show debug output during a long-running unit test?

I'm using xUnit with the ReSharper test runner and the xUnitContrib resharper plugin.

When I have a long-running test, I'd like to be able to output some progress indicator to the Unit Test Output window.

I've tried Debug.WriteLines, Trace.WriteLine and Console.WriteLine. All of which have the same behavior - nothing shows in the output window until the test has completed.

For example:

[Fact] public void Test() {     Debug.WriteLine("A");     Trace.WriteLine("B");     Console.WriteLine("C");      Thread.Sleep(10000); } 

The test shows no output until the 10 seconds have elapsed and the test completes. How do I get output along the way?

UPDATE 1

I tried also with MSTest and NUnit. NUnit is the only one that shows output along the way.

MSTest and XUnit don't return any output until the test completes. The weird part is that while the XUnit and NUnit test output looks like this:

A B C 

The MSTest output looks like this:

C   Debug Trace:  A B 

Given all of these variations, I think the answer is that it is up to the test runner implementation to decide how and when to output. Does anyone know if it is possible to configure the XUnit test runner?

UPDATE 2

I think this must be a deficiency in xUnitContrib. Posted to their CodePlex issue tracker.

like image 500
Matt Johnson-Pint Avatar asked Feb 26 '13 15:02

Matt Johnson-Pint


People also ask

How do I run unit tests with ReSharper?

Right click on the project or solution in the VS solution-explorer and choose 'Run Unit Tests' Or go to the Resharper menu, choose Unit-Testing and choose one of the options from there.

How do I debug a single test in Visual Studio?

To debug a single test: Click on a test method name, then press Ctrl+R, Ctrl+T. (Or go to Test / Debug / Tests in Current Context.)

How do I open Unit Test Explorer ReSharper?

ReSharper adds the Unit Test Explorer window to Visual Studio (ReSharper | Unit Tests | Unit Tests or ReSharper | Windows | Unit Tests, or Ctrl+Alt+U ). Using this window, you can explore and run or debug unit tests of all supported frameworks in the entire solution.


2 Answers

If you used xUnit.net 1.x, you may have previously been writing output to Console, Debug, or Trace. When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources. Users who are porting code from v1.x to v2.x should use one of the two new methods instead.

Have a look here for example of how to perform logging with xUnit.net v2:

http://xunit.github.io/docs/capturing-output.html

This is the example:

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);     } } 
like image 132
Răzvan Flavius Panda Avatar answered Oct 09 '22 10:10

Răzvan Flavius Panda


For NUnit this works:

Console.SetOut(TestContext.Progress); 

** The late answer is because I had the same problem, and I just solved it. may help others

like image 40
Yitzchak Avatar answered Oct 09 '22 10:10

Yitzchak