I've fount creating unit tests a super easy way to try out code in Visual Studio over the years. I was able to get the same type of unit tests to work with .NET core 1.1 in VS 2017 by creating a Visual C#/.NET Core/Unit Test Project (.NET Core)
project, then adding xunit
and xunit.runner.visualstudio
nuget packages.
The only problem is that I no longer see the 'Output' link when the test is run to see what I wrote to the console. I can use Trace.WriteLine
from the System.Diagnostics
package instead and see the output in the Output
window of Visual Studio, but seeing the output associated with each test run is easier and more useful.
Is there a way to get that to display with my setup or is there a different setup I could use?
Edit
@ilya's solution works, but I have to re-write all my tests, removing [TestClass]
and replacing [TestMethod]
with [Fact]
, and I can no longer right-click and run a single test method, doing that seems to run all test methods in the class...
Recommended xUnit way to capture output is to use ITestOutputHelper
:
public class BaseTestClass
{
protected ITestOutputHelper Output { get; }
public BaseTestClass(ITestOutputHelper output)
{
Output = output;
}
public void WriteLine(string str)
{
Output.WriteLine(str ?? Environment.NewLine);
}
}
public class MyTestClass : BaseTestClass
{
public MyTestClass(ITestOutputHelper output) : base(output) {}
[Fact]
public void MyTest()
{
WriteLine("foo");
}
}
Check the docs: Capturing Output .
If you used
xUnit.net 1.x
, you may have previously been writing output toConsole
,Debug
, orTrace
. WhenxUnit.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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.