使用TestCaseSource时,为什么nUnit测试会被忽略?

时间:2022-02-13 15:38:49

I am having a lot of difficulty getting the nUnit TestCaseSource attribute to work correctly in nUnit 2.6.4.14350.

我很难让nUnit TestCaseSource属性在nUnit 2.6.4.14350中正常工作。

When running the unit tests through VS2010, it just says the tests are being ignored, without any additional information as to why. The Create test just appears greyed out on the test results viewer.

当通过VS2010运行单元测试时,它只是说测试被忽略,没有任何关于原因的附加信息。 Create测试在测试结果查看器中显示为灰色。

In my case the class being tested is the TestCaseSource itself. This is what I've got:

在我的例子中,被测试的类是TestCaseSource本身。这就是我所拥有的:

public class TestClass
{
    static TestClass()     
    {
        PopulateIds();
    }

    public IEnumerable<object> CreateTestCases
    {
        get 
        {
            return _ids.Select(id => new TestCaseData(id).SetName("createTest_" + id));
        }
    }

    private static string[] _ids;

    private static void PopulateIds()
    {
        _ids = new string[] { "id123", // ... }
    }

    [TestFixtureSetUp]
    public void Init()
    {
        // this completes successfully
    }

    [Test, TestCaseSource("CreateTestCases")]
    public void Create(string Id)
    {
        // breakpoint here is never hit as test is ignored
    }
}

Clues:

  • CreateBondTestCases getter is getting hit. (before [TestFixtureSetUp] is called).
  • CreateBondTestCases getter受到了冲击。 (在调用[TestFixtureSetUp]之前)。

  • [TestCaseSource(typeof(TestClass), ...)] doesn't change anything.
  • [TestCaseSource(typeof(TestClass),...)]不会改变任何东西。

  • public TestClass() doesn't change anything.
  • public TestClass()不会改变任何东西。

  • public IEnumberable<TestCaseSource> CreateTestCases or public IEnumberable CreateTestCases doesn't change anything.
  • public IEnumberable CreateTestCases或public IEnumberable CreateTestCases不会更改任何内容。

  • [TestCaseSource(typeof(TestClass), "asdfasdf")] results in Failed: System.Reflection.TargetParameterCountException : Parameter count mismatch.
  • [TestCaseSource(typeof(TestClass),“asdfasdf”)]导致失败:System.Reflection.TargetParameterCountException:参数计数不匹配。

I saw this question but still can't get it to work. I have also tried to get it to work as in this answer, with the same results. I guess I must be doing something pretty stupid here but I am too infuriated to see what it is. Please can somebody enlighten me?

我看到了这个问题,但仍无法让它发挥作用。我也试着让它像在这个答案中一样工作,结果相同。我想我必须在这里做一些非常愚蠢的事情,但我太激怒了,看看它是什么。请有人赐教我吗?

1 个解决方案

#1


0  

It's likely that your issue is being caused by your test runner not supporting the TestCaseSource attribute, particularly if it is being reported as Create. NUnit renames tests that use TestCaseSource, to combine the argument names into the name of the test. For your test, it should be reported as createTest_id123 (the name of the test + the value of the parameters).

您的问题很可能是由您的测试运行器不支持TestCaseSource属性引起的,特别是如果它被报告为Create。 NUnit重命名使用TestCaseSource的测试,将参数名称组合到测试名称中。对于您的测试,应将其报告为createTest_id123(测试名称+参数值)。

Check your assembly using the NUnit GUI to see if the tests work as expected when run from there. If they do, then this will confirm that your TestRunner is the issue. With VS2012+, you can use the NUnit Test Adapter, installed via Nuget to run your tests. Other addins like Resharper (depending on the version) should support the attribute, but I'm not sure what versions will work with VS2010.

使用NUnit GUI检查程序集,以查看从那里运行时测试是否按预期工作。如果他们这样做,那么这将确认您的TestRunner是问题。使用VS2012 +,您可以使用通过Nuget安装的NUnit测试适配器来运行测试。像Resharper这样的其他插件(取决于版本)应该支持该属性,但我不确定哪些版本适用于VS2010。

#1


0  

It's likely that your issue is being caused by your test runner not supporting the TestCaseSource attribute, particularly if it is being reported as Create. NUnit renames tests that use TestCaseSource, to combine the argument names into the name of the test. For your test, it should be reported as createTest_id123 (the name of the test + the value of the parameters).

您的问题很可能是由您的测试运行器不支持TestCaseSource属性引起的,特别是如果它被报告为Create。 NUnit重命名使用TestCaseSource的测试,将参数名称组合到测试名称中。对于您的测试,应将其报告为createTest_id123(测试名称+参数值)。

Check your assembly using the NUnit GUI to see if the tests work as expected when run from there. If they do, then this will confirm that your TestRunner is the issue. With VS2012+, you can use the NUnit Test Adapter, installed via Nuget to run your tests. Other addins like Resharper (depending on the version) should support the attribute, but I'm not sure what versions will work with VS2010.

使用NUnit GUI检查程序集,以查看从那里运行时测试是否按预期工作。如果他们这样做,那么这将确认您的TestRunner是问题。使用VS2012 +,您可以使用通过Nuget安装的NUnit测试适配器来运行测试。像Resharper这样的其他插件(取决于版本)应该支持该属性,但我不确定哪些版本适用于VS2010。