可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Any call in my unit tests to either Debug.Write(line)
or Console.Write(Line)
simply gets skipped over while debugging and the output is never printed. Calls to these functions from within classes I'm using work fine.
I understand that unit testing is meant to be automated, but I still would like to be able to output messages from a unit test.
回答1:
Try using TestContext.WriteLine()
which outputs text in test results.
Example:
[TestClass]
public class UnitTest1
{
private TestContext testContextInstance;
/// <summary>
/// Gets or sets the test context which provides
/// information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod]
public void TestMethod1()
{
TestContext.WriteLine("Message...");
}
}
The "magic" is described in MSDN as "The test framework automatically sets the property, which you can then use in unit tests."
回答2:
Its a little late to the conversation.
I was also trying to get Debug or Trace or Console or TestContext to work in Unit Testing.
None of these methods would appeared work or show output in the output window.
Trace.WriteLine("test trace");
Debug.WriteLine("test debug");
TestContext.WriteLine("test context");
Console.WriteLine("test console");
VS2012 and Greater
(from comments) In Visual Studio 2012, there is no icon. Instead, there is a link in the test results called Output. If you click on the link, you see all of the WriteLine
.
Prior to VS2012
I then noticed in my Test Results window, after running the test, next to the little success green circle , there is another icon, i doubled clicked it. It was my test results, and it included all of the types of writelines above.
回答3:
In Visual Studio 2017, you can see the output from test explorer.
1) In your test method, Console.WriteLine("something");
2) Run the test.
3) In Test Explorer window, click the Passed Test Method.
4) And click the "Output" link.
And click "Output", you can see the result of Console.Writeline().
回答4:
It depends on your test runner... for instance, I'm using XUnit, so in case that's what you are using, follow these instructions:
https://xunit.github.io/docs/capturing-output.html
This method groups your output with each specific unit test.
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);
}
}
There's another method listed in the link I provided for writing to your Output window, but I prefer the previous.
回答5:
I think it is still actual.
You can use this NuGet package:
https://www.nuget.org/packages/Bitoxygen.Testing.Pane/
Call custom WriteLine method from this library.
It creates Testing pane inside Output window and puts messages there always (during each test run independently of DEBUG and TRACE flags).
To make trace more easy I can recommend to create base class:
[TestClass]
public abstract class BaseTest
{
#region Properties
public TestContext TestContext { get; set; }
public string Class
{
get { return this.TestContext.FullyQualifiedTestClassName; }
}
public string Method
{
get { return this.TestContext.TestName; }
}
#endregion
#region Methods
protected virtual void Trace(string message)
{
System.Diagnostics.Trace.WriteLine(message);
Output.Testing.Trace.WriteLine(message);
}
#endregion
}
[TestClass]
public class SomeTest : BaseTest
{
[TestMethod]
public void SomeTest1()
{
this.Trace(string.Format("Yeah: {0} and {1}", this.Class, this.Method));
}
}
回答6:
Try using:
Console.WriteLine()
The call to Debug.WriteLine
will only be made during when DEBUG is defined.
Other suggestions are to use: Trace.WriteLine
as well, but I haven't tried this.
There is also an option (not sure if VS2008 has it) but you can still Use Debug.WriteLine
when you run the test with Test With Debugger
option in the IDE
回答7:
Are you sure you're running your unit tests in Debug? Debug.WriteLine won't be caled in Release builds.
Two options to try are:
Trace.WriteLine(), which is built inot release builds as well as debug
Undefine DEBUG in your build settings for the unit test
回答8:
I get no output when my Test/Test Settings/Default Processor Architecture setting and the assemblies that my test project references are not the same. Otherwise Trace.Writeline() works fine.
回答9:
I'm using xunit so this is what I use: Debugger.Log(0, "1", input);
PS: you can use Debugger.Break();
to so you can see you log in out
回答10:
Trace.WriteLine
should work provided you select the correct output (The dropdown labeled with "Show output from" found in the Output window)
回答11:
Solved with the following example:
public void CheckConsoleOutput()
{
Console.WriteLine("Hi Hi World");
Trace.WriteLine("Trace Trace the World");
Debug.WriteLine("Debug Debug WOrld");
Assert.IsTrue(true);
}
After running this test, under 'Test Passed', there is the option to view the output, which will bring up the output window.
回答12:
Console.WriteLine won't work. Only Debug.WriteLine() or Trace.WriteLine() will work, in debug mode.
I do the following: include using System.Diagnostics in the test module. Then, use Debug.WriteLine for my output, right click on the test, choose Debug Selected Tests. The result output will now appear in the Output window below. I use Visual Studio 2017 vs 15.8.1, with the default unit test framework VS provides.
回答13:
Just ran into this. The cause/solution is a different variant of the above so I'll post.
My issue was that I was not getting an output because I was writing the result set from an async Linq call to console in a loop in an async context:
var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345");
p.ForEachAsync(payment => Console.WriteLine(payment.Amount));
and so the test was not writing to the console before the console object was cleaned up by the runtime (when running only one test).
Solution was to convert the result set to a list first so i could use the non async version of forEach():
var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345").ToList();
p.ForEachAsync(payment =>Console.WriteLine(payment.Amount));