1

我有以下测试夹具类,出于我以外的原因,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

4 回答 4

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();

}
于 2012-08-22T12:20:26.767 回答
1

仍然不明白为什么 NUnit 应该忽略您的测试夹具(基于发布的代码片段)。代码片段是否缺少某些内容?

正如维克托所指出的,

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
于 2012-08-23T06:13:15.533 回答
0

我从未见过 void 需要参数的测试用例,你打算这样做吗?我认为这就是为什么您在此类中的测试无法运行的原因。

[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.
    }
于 2012-08-22T12:01:18.600 回答
0

您可以为该案例应用 if 条件,并且此 if 条件适用于 [TestFixtureSetUp] 属性,因为您可以使用 Assert.Ignore("")。

于 2012-08-22T12:52:26.510 回答