NUnit with TestCaseSource discovers extra unsourced Test in VS2019 - c#

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.

Related

FsCheck integration with NUnit in C#

TL;DR: I'm not able to successfully use FsCheck with NUnit in C#: either:
it tells me on stdout that the test failed but the test still appears green
it tells me that it doesn't find any test to run
or I don't understand how to apply in C# the doc I read
I think a dummy but complete example would help...
(More details)
1st step: test remains green
I installed the Nuget package FsCheck.NUnit (2.10.4), and naively tried:
[NUnit.Framework.Test]
public void SomeTest()
{
// Note the subtle bug: I Reverse only once, because I want the test to fail
Func<int[],bool> revRevIsOrig = xs => xs.Reverse().SequenceEqual( xs );
Prop.ForAll(revRevIsOrig).QuickCheck();
}
When I run it as I would run any NUnit test, it ends up green, even though I see on stdout that
Falsifiable, after 3 tests (1 shrink) (StdGen (2129798881,296376481)):
Original:
[|-1; 0|]
Shrunk:
[|1; 0|]
2nd step: test inconclusive
So I went on, found some doc, and noticed that I should use Property instead of Test. So I changed my code to
[FsCheck.NUnit.Property] // <-- the line that changed
public void SomeTest()
{
Func<int[],bool> revRevIsOrig = xs => xs.Reverse().SequenceEqual( xs );
Prop.ForAll(revRevIsOrig).QuickCheck();
}
I launched the test from Visual, and it ended up with the status inconclusive. And some log was telling me:
Cannot run tests: No suitable tests found in 'xxx.exe'. Either assembly contains no tests or proper test driver has not been found
3rd step: I notice I didn't understood the doc
When I re-read the doc, I notice that it says that my test method can take argument and should return a property. I'm obviously not doing it since I return nothing.
The sad thing is that I don't understand what I'm actually supposed to do (and I'm not familiar enough with F# to understand the example below...)... (I've blindly tried some random stuff that looked they could make sense, but I never ended up with a red test)
I would really appreciate any pointer to help me get this test red!
Try using the QuickCheckThrowOnFailure function
the QuickThrowOnFailure ensures that if the test fails, an exception with the necessary information is raised so the runner know the test failed.
[Test]
public void SomeTest() {
// Note the subtle bug: I Reverse only once, because I want the test to fail
Func<int[],bool> revRevIsOrig = xs => xs.Reverse().SequenceEqual( xs );
Prop.ForAll(revRevIsOrig).QuickCheckThrowOnFailure();
}
The other way to use FsCheck with NUnit (from FsCheck github examples) is
[FsCheck.NUnit.Property(Verbose = true)]
public void SomeTest()
{
Func<int[],bool> revRevIsOrig = xs => xs.Reverse().SequenceEqual( xs );
return Prop.ForAll(revRevIsOrig);
}
But this way doesn't report you why your test fails. So the Nkosi's answer now is better but I think it will be fixed somedays.
Besides other mentioned options you can turn this test to a Property Based Test by using the Property attribute and correct return type (I've included namespaces for clarity).
Note the .ToProperty call to turn it into a Property.
[FsCheck.NUnit.Property]
public FsCheck.Property RevRev_IsOriginal(int[] xs)
{
Func<bool> revRevIsOrig = () => xs.Reverse().Reverse().SequenceEqual(xs);
return revRevIsOrig.ToProperty();
}
The framework will generate testdata via the int[] xs method parameter.
This requires nuget packages fscheck and fscheck.nunit (besides NUnit3TestAdapter and Microsoft.NET.Test.Sdk in my .NET Core 3.1 testproject).

What's the Microsoft unit-testing alternative to the InlineData or TestCase attributes?

Unit test frameworks other than Microsoft have options to add input parameters and expected results using attributes.
For example,
NUnit has
[TestCase(12,4,3)]
and xUnit has
[InlineData(5, 1, 3, 9)]
What's the Microsoft way to accomplish this?
You need to add Nuget packages MSTest.TestFramework and MSTest.TestAdapter (for discovery of tests) and remove the reference of Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll which is added by default. You are good to go to add input parameters:
[TestMethod]
[DataRow(10)]
[DataRow(20)]
[DataRow(30)]
public void TestMethod1(int inputValue)
{
Assert.AreEqual(10, inputValue);
}

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,

Why am I missing the "Create Unit Tests..." context menu item in VS 2013?

I'm trying to implement some Unit Testing in my MVC Web API ASP.NET project.
I am using VS 2013 (Ultimate, Update 2), and want to use Moq.
I have created a Test project within the solution.
In following along with this blog post, I'm thinking that I should be able to right-click on "GetCountOfPlatypusItemRecords" here:
public class PlatypusItemsController : ApiController
{
private readonly IPlatypusItemRepository _PlatypusItemRepository;
public PlatypusItemsController(IPlatypusItemRepository PlatypusItemRepository)
{
if (PlatypusItemRepository == null)
{
throw new ArgumentNullException("PlatypusItemRepository");
}
_PlatypusItemRepository = PlatypusItemRepository;
}
[Route("api/PlatypusItems/Count")]
public int GetCountOfPlatypusItemRecords()
{
return _PlatypusItemRepository.GetCount();
}
. . .
...and see a "Create Unit Tests..." menu item; but I don't (after "Organize Usings," I see "Generate Sequence Diagram," not "Create Unit Tests..."). Why is "Create Unit Tests..." not available for me?
Note: I am referencing Moq and nunit.framework in both the main project and the test project, and added the corresponding "using NUnit.Framework" and "using Moq"
From the looks of it, the Unit Test Generator is no longer a part of VS 2013. Check out this extension though, which apparently replicates some of those features.
Having said that, I'd advise against generating your unit tests. Most of the time, meaningful tests are the kind where you really have to think through what you are testing, and IMO the generator guides you in the wrong direction. Also, I should point out that Moq is independent of both the testing framework and the way you've written your tests, so you don't really need to generate the tests in order to try out Moq.

Categories

Resources