NUnit tests being ignored

五迷三道 提交于 2020-01-05 07:11:13

问题


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

    }
}

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.


回答1:


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?

As pointed out by Wiktor,

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.

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;
            }
        }
    }
}

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

When you run the tests,

Test Class constructor
InTestFixtureSetup

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



回答2:


This should work if you take the test cases out of 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();

}



回答3:


I've never seen a testcase where the void takes arguments, do you intent to do this? I think this is why your test in this class dosn't run.

[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);
}

If you really want this values outside your testcase, use specify them outside as a vaiable that you can set in the Init() Example :

private WorkflowRuntime RunTime;

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

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



回答4:


you can apply if condition for that case and this if condition apply on [TestFixtureSetUp] attribute in that if condition you can use Assert.Ignore("").



来源:https://stackoverflow.com/questions/12072462/nunit-tests-being-ignored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!