A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits. Unit testing finds problems early in the development cycle. This includes both bugs in the programmer's implementation and flaws or missing parts of the specification for the unit.
On the Create a new project page, type unit test into the search box. Select the project template for the test framework that you want to use, for example MSTest Test Project or NUnit Test Project, and then select Next. On the Configure your new project page, enter a name for your project, and then select Create.
A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.
I was also trying to get Debug or Trace or Console or TestContext to work in unit testing.
None of these methods would appear to work or show output in the output window:
Trace.WriteLine("test trace");
Debug.WriteLine("test debug");
TestContext.WriteLine("test context");
Console.WriteLine("test console");
(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
.
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.
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:
To use TestContext, create a member and property within your test class [...] The test framework automatically sets the property, which you can then use in unit tests.
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().
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.net/docs/capturing-output
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.
In VS 2019
View
-> Test Explorer
additional output
link as seen in the screenshot below.You can use:
all will log to the additional output window.
I think it is still actual.
You can use this NuGet package: Bitoxygen.Testing.Pane
Call the custom WriteLine method from this library. It creates a Testing pane inside the Output window and puts messages there always (during each test, it runs independently of DEBUG and TRACE flags).
To make tracing more easy I can recommend to create a 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));
}
}
It is indeed depending on the test runner as @jonzim mentioned.
For NUnit 3 I had to use NUnit.Framework.TestContext.Progress.WriteLine()
to get running output in the Output window of Visual Studio 2017.
NUnit describes how to: here
To my understanding this revolves around the added parallelization of test execution the test runners have received.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With