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,
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 have a c# unit test project (.NET 4.8).
My unit test are using a .testsetting file that I want to remove.
In my unit test I use TestContext too:
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
LoadSettings(testContext.DeploymentDirectory);
}
First I removed the .testsettings file, then I modified the unit test removing the TestContext:
[ClassInitialize]
public static void MyClassInitialize()
{
LoadSettings(Directory.GetCurrentDirectory());
}
My problem is when I run the unit test I got this error:
MyClassInitialize has wrong signature. The method must be static,
public, does not return a value and should take a single parameter of
type TestContext.
Looking at this issue, I changed the version of "Microsoft.VisualStudio.QualityTools.UnittestFramework" from 10.0.0.0 to 10.1.0.0 with no success.
Before run the tests I unchecked the .testsettings from "Test" menu and then I deleted it.
Currently use Visual Studio 2019 version 16.3.2.
I still got the same error. Do I need to reference another dll or another version? Do I need to install something?
UPDATE:
Here how I added the reference:
Here the row added by Visual Studio in the .csproj file:
Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework,Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,processorArchitecture=MSIL"
And It is how look the reference properties in Visual Studio:
I still got the error, but the strange thing is If I restart Visual Studio in the reference property I continue to see the old version (10.0.0.0)!!!
I tried also with Visual Studio 2017 version 15.9.16. Same result!
As the error message says, a method decorated with the ClassInitializeAttribute must take a TestContext parameter.
You can just ignore it if you don't ever indend to use a .testsettings file, but you cannot remove the parameter.
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.
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 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.