Nunit running all tests on TestFixture even when selecting single test - c#

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.

Related

NUnit with TestCaseSource discovers extra unsourced Test in VS2019

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.

Blank output for an ASP.NET NUnit test application

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.

Nunit Combinatorial throws TargetParameterCountException

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,

nunit TestContext throws NullReferenceException

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.

resharper unit test inheritance

Has anyone got a strategy for unit testing heiarchies in Resharper?
I typically use both TestDriven.Net and Resharper's test runner, with NUnit tests. TestDriven is awesome for everything but quickly finding a bad test out of a batch run (which could be thousands), which is where Resharper's runner comes in.
I typically use a pattern with an abstract base class (like the code below) of test cases overridden to get the right subclass, which works great in TestDriven, but Resharper just ignores them! I had thought as of v5.0 Resharper was using NUnit's code base, which means this should work but it doesn't.
Cheers,
Berryl
[TestFixture]
public class AdminAccountTests : AccountTests
{
protected override Account _GetAccount() { return new AdminAccount(_idScheme, _description); }
}
[TestFixture]
public class LeaveTimeAccountTests : AccountTests
{
protected override Account _GetAccount() { return new LeaveTimeAccount(_idScheme, _description); }
}
public abstract class AccountTests
{
protected abstract Account _GetAccount();
[SetUp]
public void SetUp()
{
_account = _GetAccount();
}
[Test]
public void OnCreation_Blah() {
Assert.That(_account.IdScheme, Is.EqualTo(_idScheme));
}
}
Make your abstract class a TestFixture. I do this same thing with R#.
EDIT: I just noticed that R# (I'm using 5.1 with NUnit 2.6) will mark a class as a test fixture if it has Tests in it, regardless of whether the subclass or the base class are attributed with TestFixture. So that may not solve your problem.
I have the same issue with MbUnit and Gallio with resharper 5.1.3000.12. If i try to launch the test through visual studio plugin, the test is ignored. With external gallio test runner, it works fine.
JetBrains ReSharper 5.1 C# Edition
Build 5.1.3000.12 on 2011-01-28T05:05:56
Plugins:
1. “Gallio Test Runner” v3.2.0.0 by Gallio
Visual Studio 9.0.30729.1.
Copyright © 2003–2011 JetBrains s.r.o.. All rights reserved.

Categories

Resources