简体   繁体   English

Nunit:TestCaseSource 不希望生成所有测试用例?

[英]Nunit: TestCaseSource does not expect to generate all test cases?

Hi everyone I have a problem with generate test cases for TestCaseSource.大家好,我在为 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.我没有得到 25 个测试,而是从 1 到 8 个。通常在测试的起点,它表明测试是 7/8,最后只有一个测试用例。

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!? UPD1 :有趣的是,当我通过控制台运行测试时,我处理了所有 25 个测试,如何通过 GUI 获得相同的结果!?

PS sorry for my bad english. PS抱歉我的英语不好。

Perhaps I should mention that I'm working under Ubuntu in Rider也许我应该提到我在 Rider 的 Ubuntu 下工作

DateTime.Now is normally not very accurate. DateTime.Now通常不是很准确。 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?为什么要使用种子而不是简单地让 Random 自行工作?

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!他们不为你做任何事!

The reason is TestExplorer looks at the name of test case and groups ones with equal names.原因是 TestExplorer 查看测试用例的名称并将名称相同的分组。 So you don't see all test cases results.所以你看不到所有的测试用例结果。 As reason is TestExplorer you don't have the same problem running test using console.原因是 TestExplorer,您在使用控制台运行测试时不会遇到同样的问题。 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.为您的测试用例创建类并覆盖ToString()方法,使其为每个测试用例返回唯一值。
  2. use TestCaseData class and its method SetName to set unique name for each test.使用TestCaseData类及其方法SetName为每个测试设置唯一的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM