Nippysaurus
Nippysaurus

Reputation: 20378

NUnit executes with alternate constructor

I have a class which has some unit tests, but when I am running tests I would like the class to be created with a different constructor. Something like this:

[TestFixture]
public class MyClass
{
    public MyClass() { /* set up for production */ }

    [TestFixtureConstructor]
    public MyClass() { /* set up for testing */ }

    [Test]
    public void FirstTest()
    {
        // assert some values
    }
}

Is this even possible?

I have considered using a static factory method for creating the class in production (with a private constructor), and a public constructor for the testing framework.

Does anyone have any other solutions?

Upvotes: 2

Views: 1378

Answers (3)

Graviton
Graviton

Reputation: 83254

If you really really want this you can take a look at TestFixtureSetUp.

Here's the introduction:

This attribute is used inside a TestFixture to provide a single set of functions that are performed once prior to executing any of the tests in the fixture. A TestFixture can have only one TestFixtureSetUp method. If more than one is defined the TestFixture will compile successfully but its tests will not run.

Example:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [TestFixtureSetUp] public void Init()
    { /* ... */ }

    [TestFixtureTearDown] public void Dispose()
    { /* ... */ }

    [Test] public void Add()
    { /* ... */ }
  }
}

But you should use this with care, or else it defeats the purpose of unit test.

Upvotes: 2

TrueWill
TrueWill

Reputation: 25523

To give a quick example of silky's correct approach:

public class MyClass
{
    // ...
}

// In a different assembly:

[TestFixture]
public class TestMyClass
{
    [SetUp]
    public void SetUp()
    {
        _myClass = new MyClass();
    }

    [Test]
    public void FooReturnsTrue()
    {
        Assert.That(_myClass.Foo(), Is.True);
    }

    // more tests

    private MyClass _myClass;
}

Upvotes: 3

Noon Silk
Noon Silk

Reputation: 55072

You don't do this.

You do not have your tests written inside the class that you use in real code; you write your tests external to the classes. I believe most testing suites have the concept of 'Teardown' and the opposite; to set up the test environment for a given execution.

Upvotes: 9

Related Questions