I am currently working with NUnit to write test cases for an ASP.NET application.
I have created a sample test project and have added NUnit latest version (3.6.1) and NUnitTestAdapter 2.1.1.
If I run the application by clicking on menu Test → Windows → Text Explorer, I am getting a blank screen in the Output Window. Why?
My code is:
[TestFixture]
public class SampleTest
{
[Test]
public void StringCheck()
{
string str = "Hello";
Assert.That(str, Is.EqualTo("Hello"));
}
[Test]
public void EmptyCheck()
{
string str = "siva";
Assert.That(str, Is.EqualTo(string.Empty));
}
[Test]
public void NumberCheck()
{
int i = 0;
Assert.That(i, Is.EqualTo(0));
}
}
You are using the adapter for the NUnit 2.x series, rather than the NUnit 3 adapter. NUnit versions 2 and 3 differ substantially, and there are two separate Visual Studio adapters to run the tests.
This is the one you'll need to run NUnit 3.6.1 tests:
NUnit 3 Test Adapter
I found the solution. The problem was with NUnit version (3.6.1). Now I am updated with NUnit 2.6.4. It is working for me.
Related
I have an NUnit test in VS2019 using .Net Core 3.1 with a TestCaseSource that supplies a list of 3-Tuples.
It works as expected and the cases I wish to test are discovered and run.
However, the Test Explorer shows the same Test method as a test without source. The result is the "test" is skipped and the results for the group show as inconclusive. Here are the code and Test Explorer image:
public static IEnumerable<(int, int, int)[]> TestInput
{
get
{
yield return new[] {
(0, 2, 3),
(0, 1, 7),
...
[Test]
[TestCaseSource(nameof(TestInput))]
public void CalcTotalTime_Given_known_valid_input_Then_returns_expected_result((int,int,int)[] input)
{
...
Since this is just a learning project the code under test is in the test project, so there is only one project and it targets .NET Core 3.1.
The installed NuGet packages are:
Microsoft.NET.Test.Sdk v16.7.1
NUnit v3.12.0
NUnit3TestAdapter v3.17.0
I was able to reproduce it and it seems like a bug to me with the NUnit3TestAdapter.
Similar issues were found in the past:
https://github.com/nunit/nunit3-vs-adapter/issues/559
Depending on what you're trying to achieve, you can consider writing your unit test like this:
[TestCase(0,2,3)]
[TestCase(1,2,3)]
public void CalcTotalTime_Given_known_valid_input_Then_returns_expected_result(int a, int b, int c)
{
}
But as mentioned in the comment, the behavior is not identical to the original code.
I'm running Nunit 3.5 on VS2015, Resharper Ultimate 10,
created this TestFixture
[TestFixture]
public class TestInfluxDbConnector
{
[Test]
public void TestPong()
{
// Arrange
InfluxDbProxy influxDb = new InfluxDbProxy();
// Act
Task<bool> res = influxDb.PingAsync();
// Assert
Assert.IsTrue(res.Result);
}
[Test]
public void CreateDatabaseAsync()
{
// Arrange
InfluxDbProxy influxDb = new InfluxDbProxy();
// Act
var databseAsync = influxDb.CreateDatabseAsync("Test");
// Assert
Assert.IsTrue(databseAsync.Result);
}
}
Why when I'm debugging a single Test all tests are running? (I want to debug / run only a single test)
I had the same issue when I had updated nunit 2.6.4 to nunit 3.5.0.
Try do this:
Disable Nunit Test Adapter in "extensions and updates".
Install Nunit3TestAdapter. Link:
https://visualstudiogallery.msdn.microsoft.com/0da0f6bd-9bb6-4ae3-87a8-537788622f2d
In Visual Studio use "Tests"->"Windows"->"Test Explorer" and run
tests only from this window.
Only this way is working for me at the moment. Probably nunit 3.5 is new for Resharper and they cannot work fine at the moment.
I have some test like this:
[Test, Combinatorial]
public void SomeTest(
[Values(false, true)] bool flag,
[Values(2, 5)] int someValue))
{
var entity = new SomeClass();
entity.Flag = flag;
entity.SomeValue = someValue;
var context = entity.GetContext();
Assert.AreEqual(context.SomeValue, entity.SomeValue);
}
When I try to run test, it throws TargetParameterCountException. StackTrace:
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at NUnit.Core.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
at NUnit.Core.TestMethod.RunTestMethod(TestResult testResult)
at NUnit.Core.TestMethod.RunTestCase(TestResult testResult)
What is wrong? I use Nunit 3.4.1and VS 2012.
Simple tests work well.
Your code is fine and runs fine for me using the NUnit 3 Visual Studio Adapter. Based on the callstack, you are trying to run the code in an older NUnit 2 based adapter. It is either an older version of Resharper or an old version of the NUnit Visual Studio Extension from before it was updated to not run NUnit 3 tests.
Install the NUnit 3 Visual Studio adapter and give that a try. If you are using Resharper, you need to pay for an update.
Also, pro-tip, you don't need to include the values in the attribute for bool or enums, all values will be automatically injected. You also don't need the Test attribute.
Here is my simplified version of your example,
[Combinatorial]
public void SomeTest([Values] bool flag, [Values(2, 5)] int someValue)
{
TestContext.WriteLine($"{flag} - ${someValue}");
}
And the results in the Visual Studio Adapter,
In Visual Studio 2015 Community I have a sample ASP.NET 5 (vNext) project and a project with unit tests (xUnit.net). The version of DNX is 1.0.0-beta5. My goal is to add messages during the test run to the output pane.
Here I took a way to do this, so my unit test code looks like this:
using Xunit;
using Xunit.Abstractions;
namespace UnitTests
{
public class UnitTest1
{
ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void TestTestTest()
{
output.WriteLine("Test Message");
Assert.Equal(2, 2);
}
}
}
Visual Studio Test Explorer discovers this test (and that's OK), but all I have in the Output pane (from Tests) is:
------ Run test started ------
------ Test started: Project: UnitTests ------
Starting Microsoft.Framework.TestHost [C:\Users\*******\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta5\bin\dnx.exe --appbase "C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests" Microsoft.Framework.ApplicationHost --port 55837 Microsoft.Framework.TestHost --port 55893]
Connected to Microsoft.Framework.TestHost
Running tests in 'C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests\project.json'
========== Run test finished: 1 run (0:00:03,2267169) ==========
Also, there is not any link "Output" under the selected test run information, like here:
(Only "Test passed... Elapsed time ...")
What should I do to make this ITestOutputHelper work?
This solution works for me (Visual Studio 2017 and xUnit 2.2.0.3545).
Try adding the below configuration in the App.Config file. (I don't know why. It was just needed. If it doesn't exist, just add a new one in your test project.)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="xunit.diagnosticMessages" value="true"/>
</appSettings>
</configuration>
The output information will be written in Test output as expected like below.
[xUnit.net 00:00:00.0853907] Starting: xxxAssemblyName (parallel test collections = on, max threads = 8)
[xUnit.net 00:00:00.1689373] Example.xxxTestClassName [PASS]
[xUnit.net 00:00:00.1697265] Output:
[xUnit.net 00:00:00.1700698] xxxx your Test Message
[xUnit.net 00:00:00.1797303] Finished: xxxAssemblyName
As explained on the xUnit GitHub page, I used
private readonly ITestOutputHelper output;
And this worked to me.
I also ran into this the first time I used xUnit after using NUnit for a long time.
Surprisingly, xUnit does not support console output directly, but does offer some other ways to achieve the same thing.
https://xunit.github.io/docs/capturing-output.html
If you aren't really invested in xUnit I would say the simplest thing to do would just use NUnit or MSTest because both support simple console.writeline.
For the ITestOutputHelper implementation only, the following works:
As reported by Joe.wang, you must put the {add key="xunit.diagnosticMessages" value="true"} in the App.config of the unit test project.
As reported by stefano, you must add a local member of type ITestOutputHelper to the test class. And you must add it to the parameters of a constructor and assign the injected object to the local member.
You should rebuild after instrumenting with an output method.
You must have a failing test.
Your call to the output method should precede the/a failing test.
This works per test! Not for a test suite.
Got it to work with no configuration. Using .NET Core and VSCode, using Dependency Injection pattern, as described on XUnit.Net:
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);
}
}
And then run this command:
dotnet test --logger "console;verbosity=detailed"
Json configuration
As it seems that the above xml answer was not updated for a while, is obsolete and Matt answer in a comment is not the appropriate way to answer. Here is the minimal JSON configuration needed to display messages in output.
{
"$schema":"https://xunit.github.io/schema/current/xunit.runner.schema.json",
"diagnosticMessages": true
}
Just use Console.Write() and Console.WriteLine().
ReSharper hooks into this and you'll see the output in the test results.
I have the following code:
[TestFixture]
public class LexicalTests
{
[Test]
public void LexicalTest1()
{
TestContext.CurrentContext.TestDirectory;
}
}
CurrentContext throws an exception while attempting to get TestDirectory or WorkingDirectory property.
How can I solve this problem?
P.S.: On my home PC tests work perfectly (without strange exceptions).
It seems that some applications that offer the functionality to run NUnit unit tests have a problem with the TestContext class.
The test in class below should pass:
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class UnitTests
{
[Test]
public void CurrentContextTest()
{
Assert.IsNotNull(TestContext.CurrentContext);
Assert.IsNotNull(TestContext.CurrentContext.TestDirectory);
Assert.IsNotNull(TestContext.CurrentContext.WorkDirectory);
}
}
}
If the test doesn't pass then, as Dmitry wrote in his comment above, change the NUnit version in the ReSharper menu. From within Visual Studio, go to ReSharper -> Options -> Tools -> NUnit. Click the Specified NUnit installation radio button and ensure that a folder with nunit.core.dll, nunit.core.interfaces.dll and nunit.util.dll is specified. An error will be displayed if the listed files cannot be found.
Once the NUnit version has been changed, re-run the test and it should pass.