NUnit测试被忽略 [英] NUnit tests being ignored

查看:70
本文介绍了NUnit测试被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试夹具类,并且出于我自己之外的原因,NUnit决定围绕该类而不是该类来运行所有测试类

I have the following test fixture class and for reasons beyond me NUnit decides to run all of the test classes around this one but not this one

using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;   
using MyProject;
using NUnit.Framework;

namespace MyProject.Test
{
    [TestFixture]
    public class MyProjectTests
    {
        private const string Description = "This is a test Description generated through UNIT Tests";

        private ManualWorkflowSchedulerService scheduler;
        private WorkflowRuntime workflowRuntime;

        [SetUp]
        public void Init()
        {
            // set up workflow scheduler and runtime
            this.workflowRuntime = new WorkflowRuntime();
            this.scheduler = new ManualWorkflowSchedulerService(true); // run synchronously
            this.workflowRuntime.AddService(this.scheduler);
            this.workflowRuntime.StartRuntime();

            // create Test Case Sources
            object[] insertScenarios = 
                {
                    new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, true, string.Empty },
                    new object[] { typeof(RaiseScenario), this.workflowRuntime, Description, true, false, "New Reason" }
                };
        }

        /// <summary>
        /// The insert tests.
        /// </summary>
        /// <param name="runtime">
        /// The runtime.
        /// </param>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <param name="outstandingWorkDocUploaded">
        /// The Doc One Uploaded.
        /// </param>
        /// <param name="DocTwoUploaded">
        /// The Doc Two Uploaded.
        /// </param>
        /// <param name="shortageReason">
        /// The shortage Reason.
        /// </param>
        [Test, TestCaseSource("insertScenarios")]
        public void TestInsert(
            WorkflowRuntime runtime,
            string description,
            bool DocOneUploaded,
            bool DocTwoUploaded,
            string Reason)
        {
            var message = Business.InsertBoatHandoverOutsideCrew(runtime, description, DocOneUploaded, DocTwoUploaded, Reason);
            Assert.AreNotEqual(0, message.Id);
        }

    }
}

测试项目的结构分为其组成部分,即解决方案的每个子项目在测试项目中都有其自己的目录.对于用.Net 3.5编码的所有其他项目来说,这并不是问题,但是现在忽略了该项目的测试.

The structure of the test project is split into it's constituent parts i.e. each sub project of the solution has it's own directory within the test project. This has not been a problem for all other projects al coded in .Net 3.5 but this project's tests are now being ignored.

推荐答案

仍然看不到为什么NUnit应该忽略您的测试装置(基于发布的代码段).代码段中缺少某些内容吗?

Still don't see why your test fixture should be ignored by NUnit (based on the code snippet posted). Is the code snippet missing something?

如维克托所指出的,

sourceName参数代表用于提供测试用例.它具有以下特征:可能是字段,属性或方法.它可以是实例,也可以是静态的成员.它必须返回IEnumerable或实现的类型IEnumerable.枚举器返回的单个项目必须为与属性所在的方法的签名兼容出现.

The sourceName argument represents the name of the source used to provide test cases. It has the following characteristics: It may be a field, property or method. It may be either an instance or a static member. It must return an IEnumerable or a type that implements IEnumerable. The individual items returned by the enumerator must be compatible with the signature of the method on which the attribute appears.

不过,使用上面列出的代码片段,您应该将标记为Invalid not Ignored的特定测试(在Fwk 4.0上使用NUnit v2.5.10).

However with the code snippet listed above, you should get the specific test marked as Invalid not Ignored (using NUnit v2.5.10 on Fwk 4.0).

namespace AJack
{
    [TestFixture]
    public class ParameterizedTestsDemo
    {
        private object[][] _inputs;

        public ParameterizedTestsDemo()
        {
            Console.Out.WriteLine("Instantiating test class instance");
            _inputs = new[]{ new object[]{1,2,3}, 
                                 new object[]{4,5,6} }; 
        }

        [TestFixtureSetUp]
        public void BeforeAllTests()
        {
            Console.Out.WriteLine("In TestFixtureSetup");
            object[] localVarDoesNotWork = {   new object[]{1,2,3}, 
                                    new object[]{4,5,6} };
            /*this will NOT work too
            _inputs = new[]{ new object[]{1,2,3}, 
                                 new object[]{4,5,6} }; */
        }

        [TestCaseSource("localVarDoesNotWork")]
        public  void WillNotRun(int x, int y, int z)
        {
            Console.Out.WriteLine("Inputs {0}, {1}, {2}", x,y,z);
        }
        [TestCaseSource("PropertiesFieldsAndMethodsWork")]
        public void TryThisInstead(int x, int y, int z)
        {
            Console.Out.WriteLine("Inputs {0}, {1}, {2}", x, y, z);
        }
        private object[] PropertiesFieldsAndMethodsWork
        {
            get {
                Console.Out.WriteLine("Getting test input params");

                return _inputs;
            }
        }
    }
}

如果您在Console.Out.WriteLines上设置跟踪点并附加调试器,则会看到在加载程序集(构建测试树)时,命中的跟踪点是

If you set tracepoints on the Console.Out.WriteLines and attach a debugger, you'd see When the assembly is loaded (the test tree is constructed), the tracepoints hit are

Test Class constructor
Retrieve test case inputs from property/field/method

运行测试时,

Test Class constructor
InTestFixtureSetup

因此,要点是,您必须在测试类ctor中分配实例字段才能起作用.您不能使用Setup方法,因为在参数化测试中不会调用它们.输入已解决.另外,当它无法解析输入时,您应该会看到一个红色的异常,例如

So the point being, you'd have to assign the instance fields in the test class ctor for this to work. you can't use Setup methods because they are not called when the parameterized test inputs are resolved. Also when it can't resolve the inputs, you should see a red with exception like

AJack.ParameterizedTestsDemo.WillNotRun:
System.Exception : Unable to locate AJack.ParameterizedTestsDemo.localVarDoesNotWork

这篇关于NUnit测试被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆