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...
The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and . NET Framework 4.5.
To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.
One way to do it: add " --logger=trx" to the "dotnet test" command and then use the build step "Process xUnit test result report" from the "xUnit plugin". Use the option "MSTest-Version N/A (default) Pattern" and set pattern to "*/. trx". This is the answer!
The dotnet test command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure of each test.
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.
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