1

Hi everyone I have a problem with generate test cases for TestCaseSource. I wrote this code for tests:

using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

namespace HeapSort.Tests
{
    [TestFixture]
    public class Tests
    {
        [Test, TestCaseSource(typeof(TestsGenerator),"TestCases")]
        public void IsEqualCollections(int[] received, int[] expected)
        {
            CollectionAssert.AreEqual(received, expected);
        }
    }

    public class TestsGenerator
    {
        public static IEnumerable<TestCaseData> TestCases
        {
            get
            {
                for (var i = 0; i < 25; i++)
                {
                    int[] t1 = GenerateCollection(i), t2 = t1.ToArray();
                    HeapSort.Sort(t1);
                    Array.Sort(t2);

                    yield return new TestCaseData(t1, t2);
                }
            }
        }

        private static int[] GenerateCollection(int seed)
        {
            var rnd = new Random(seed+DateTime.Now.Millisecond);
            int size = rnd.Next(100, 10000); 
            int[] array = new int[size];
                for (var i = 0; i < size; i++)
                    array[i] = rnd.Next(-100, 100);
            return array;

//            return Enumerable
//                .Repeat(100, rnd.Next(100, 10000))
//                .Select(i => rnd.Next(-100, 100))
//                .ToArray();
        }
    }
}

Where is the problem? Rather than get 25 tests, I get from 1 to 8. And often at the starting point of testing it shows that the tests are 7/8 and in the end there is only one test case.

How can I solve this problem?

UPD1: What's interesting is when I run tests through the console I handle all 25 tests how do I achieve the same results through the GUI!?

P.S. sorry for my bad english.

Perhaps I should mention that I'm working under Ubuntu in Rider

Aiswe
  • 25
  • 6
  • A few unrelated tips: use `nameof(TestsGenerator.TestCases)`, rather than the "TestCases" string. Move the `Random` variable into that property (just use the default constructor), and pass that variable as a parameter to the `GenerateCollection` method. – Richardissimo Apr 29 '18 at 22:04
  • Also, the separation between the Test and the TestCaseSource doesn't seem quite correct. From what I can tell, you are trying to check that Heap Sort and Array Sort return collections with the same elements in the same order. So the TestCaseSource should just be yield returning the collection that it should be testing: `t1`, and the `t2 = t1.ToArray(); HeapSort.Sort(t1); Array.Sort(t2);` should be in the test, since that's what the test is testing, it's not part of the test case. – Richardissimo Apr 29 '18 at 22:06
  • @Richardissimo Thanks for the advice, I will use them, about the test, you are certainly right – Aiswe Apr 30 '18 at 09:45
  • If you are using complex objects in your TestCaseData always make sure that they are properly constructed. Nunit won't yell at you, it will just ignore failed test cases. – Kamil Stadryniak Feb 28 '19 at 08:23

2 Answers2

1

DateTime.Now is normally not very accurate. Your loop is generating many identical tests because they are all starting from the same seed. Why are you using a seed rather than simply letting Random work on its own?

Different runners will handle identical test in different ways. If you indicate what runner you are using to execute your tests, I can edit this answer with more information. However, in general, you most certainly don't want to generate a bunch of tests with the same data. They don't do anything for you!

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • I run tests from gui in JetBrains Rider – Aiswe Apr 29 '18 at 16:10
  • What's interesting is when I run tests through the console I handle all 25 tests how do I achieve the same results through the GUI – Aiswe Apr 29 '18 at 16:15
  • @Aiswe Since the console behaves correctly, my guess would be that this isn't an NUnit issue. Consider adding a `rider` tag to this question to get the right people to see it. – Richardissimo Apr 29 '18 at 22:11
  • In any case, I suggest changing your code so you don't generate tests with identical data. That does nothing for you! – Charlie Apr 30 '18 at 23:15
0

The reason is TestExplorer looks at the name of test case and groups ones with equal names. So you don't see all test cases results. As reason is TestExplorer you don't have the same problem running test using console. Look at this issue for details. According to the issue there are two solutions:

  1. create class for your test case and override ToString() method making it return unique values for each test case.
  2. use TestCaseData class and its method SetName to set unique name for each test.
Andrei
  • 749
  • 6
  • 7