NUnit测试被忽略

时间:2012-08-22 11:50:28

标签: c# .net tdd nunit

我有以下测试夹具类,除了我以外的原因,NUnit决定围绕这一个运行所有测试类但不是这个

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编码的所有其他项目来说都不是问题,但是这个项目的测试现在被忽略了。

4 个答案:

答案 0 :(得分:3)

如果您从SetUp

中取出测试用例,这应该可行
// create Test Case Sources
public 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 init.
/// </summary>
[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();

}

答案 1 :(得分:1)

仍然不明白为什么NUnit应该忽略你的测试夹具(根据发布的代码片段)。代码片段缺少什么吗?

正如Wiktor所指出的那样,

  

sourceName参数表示用于的源的名称   提供测试用例。它具有以下特征:它可能是一个   领域,财产或方法。它可以是实例,也可以是静态的   会员。它必须返回IEnumerable或实现的类型   IEnumerable的。调查员返回的个别项目必须是   兼容属性所在方法的签名   出现。

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

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上设置跟踪点并附加调试器,您会看到加载程序集时(构建测试树),命中的跟踪点是

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

运行测试时,

Test Class constructor
InTestFixtureSetup

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

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

答案 2 :(得分:0)

我从来没有见过一个带有参数的测试用例,你打算这样做吗? 我认为这就是为什么你在这个课程中的测试不能运行。

[Test, TestCaseSource("insertScenarios")]
public void TestInsert()
{
    WorkflowRuntime runtime = //some value;
    string description = //some value; 
    bool DocOneUploaded = //some value;
    bool DocTwoUploaded = //some value;
    string Reason = //some value;

    var message = Business.InsertBoatHandoverOutsideCrew(runtime, description, DocOneUploaded, DocTwoUploaded, Reason);
    Assert.AreNotEqual(0, message.Id);
}

如果您确实希望将此值放在测试用例之外,请使用外部指定它们作为可在Init()示例中设置的可变值:

private WorkflowRuntime RunTime;

    [Setup]
    public void Init()
    {
    RunTime = new WorkflowRuntime();
    }

    [Test]
    public void TestInsert()
    {
    //RunTime can now be accessable here.
    }

答案 3 :(得分:0)

你可以申请if case for case,if if condition适用于[TestFixtureSetUp]属性,if if condition你可以使用Assert.Ignore(“”)。