2

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.

IEatBagels
  • 823
  • 2
  • 8
  • 23
Deviland
  • 3,324
  • 7
  • 32
  • 53

4 Answers4

3

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

}
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • can't have this in a static member, that's why it was in the init() so I could get around this. – Deviland Aug 22 '12 at 12:24
  • true but wouldn't that mean that I was using this within an initializer. I have never tried this (in a test) but what about a constructor to initialize insertScenarios? (sorry had to correct my bad English sometimes I type faster than I think lol) – Deviland Aug 22 '12 at 12:28
  • The test case source has to be a field, a property or a method. I don't think it matters how you initialize it or what are internal implementation details. For example, if it's a method, it can return anything, computed anywhere. – Wiktor Zychla Aug 22 '12 at 12:32
  • Yes I'm slowly catching up with you but now back to more basic errors object not set to an instance of an object (still being ignored) – Deviland Aug 22 '12 at 12:36
  • Can't help you with null reference exceptions and other specific stuff, you have to carefully debug it. It's beyond the scope of your original problem. – Wiktor Zychla Aug 22 '12 at 14:52
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
Gishu
  • 134,492
  • 47
  • 225
  • 308
0

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.
    }
Jonas W
  • 3,200
  • 1
  • 31
  • 44
  • This is called "parametrized tests", a test method takes arguments and the `TestCaseSource` attribute is to specify the method which is supposed to provide arguments. http://nunit.org/index.php?p=testCaseSource&r=2.5 – Wiktor Zychla Aug 22 '12 at 12:18
  • This is what I am using http://www.nunit.org/index.php?p=testCaseSource&r=2.5 I have multiple scenarios for the input and wish to have a set up where I write one fire many – Deviland Aug 22 '12 at 12:19
  • no worries that's why we (especially me right now) are all here ;) – Deviland Aug 22 '12 at 12:25
0

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("").