Question

I have a test class and a class like below.

public class Foo
{
    private IDbOjbect _db;

    public Foo(string conn)
    {
        _db = new DbOjbect(conn);
    }

    internal Foo(IDbObject db)
    {
        _db= db;
    }

    internal bool TestOne()
    {
        if (_db.CurrentDbType == DBType.Oracle)
        {
            //do something
            return true;
        }
        return false;
    }

    internal bool TestTwo()
    {
        if (_db.CurrentDbType == DBType.SqlServer || 
            _db.CurrentDbType == DBType.SqlServerCE)
        {
            //do something
            return true
        }
        return false;
    }

    internal bool TestThree()
    {
        if (_db.CurrentDbType == DBType.MySql || 
            _db.CurrentDbType == DBType.PostgreSQL || 
            _db.CurrentDbType == DBType.SQLite)
        {
            //do something
            return true
        }
        return false;
    }

    public void RunProcesses()
    {
         TestOne();
         TestTwo();
         TestThree();
    }
}

[TestFixture("sqlserver")]
[TestFixture("sqlserverce")]
[TestFixture("oracle")]
[TestFixture("mysql")]
[TestFixture("sqlite")]
[TestFixture("postgre")]
public class Tests
{
   private string _conn;
   private Foo f;

   public Tests(string conn)
   {
       _conn = conn;
   }

    [SetUp]
    public void SetUp()
    {
        db = new Mock<IDbObject>();
        switch (_conn)
        {
            case "sqlserver":
                db.Setup(x => x.CurrentDbType).Returns(DBType.SqlServer);
                break;
            case "sqlserverce":
                db.Setup(x => x.CurrentDbType).Returns(DBType.SqlServerCE);
                break;
            case "mysql":
                db.Setup(x => x.CurrentDbType).Returns(DBType.MySql);
                break;
            case "postgre":
                db.Setup(x => x.CurrentDbType).Returns(DBType.PostgreSQL);
                break;
            case "sqlite":
                db.Setup(x => x.CurrentDbType).Returns(DBType.SQLite);
                break;
            case "oracle":
                db.Setup(x => x.CurrentDbType).Returns(DBType.Oracle);
                break;
        }

        f = new Foo(db.Object);
    }

   [Test]
   public void TestOne()
   {
       Assert.IsTrue(f.TestOne());
   }

   [Test]
   public void TestTwo()
   {
       Assert.IsTrue(f.TestTwo());
   }

   [Test]
   public void TestThree()
   {
       Assert.IsTrue(f.TestThree());
   }
}

When the _conn is oracle, I want to be run the TestOne method. When the _conn is sqlserver or sqlserverce, I want to be run the TestThree method. When the _conn is mysql,sqlite or postgre, I want to be run the TestTwo method. How can I do this ? Is there an attribute of nunit for this ?

Was it helpful?

Solution

That really isn't the intended use of TestFixture... If you don't want to use an if, why not just be explicit in your tests? You can use a TestCase for values you expect to pass a certain test.

[TestFixture]
public class Tests {

    [TestCase("test1")]
    public void FooTest_One(String value) {
        Foo f = new Foo(value);
        Assert.IsTrue(f.TestOne());
        Assert.IsFalse(f.TestTwo());
    }

    [TestCase("test2")]
    public void FooTest_Two(String value) {
        Foo f = new Foo(value);
        Assert.IsTrue(f.TestTwo());
        Assert.IsFalse(f.TestOne());
    }
}

Assuming you want to test more than two values, you can also add additional TestCase's for values that correspond to the expected behavior:

[TestCase("test1")]
[TestCase("test1.1")]
public void FooTest_One(String value) ...

[TestCase("test2")]
[TestCase("test2.1")]
public void FooTest_Two(String value) ...

This gives you the added benefit of testing for the cases that are supposed to fail. I'm not sure if it matches your real situation, but it is always important to test for failure in addition to expected passing values.


EDIT If you truly need dynamic behavior for the Foo class based on the DB type, your best bet would be to create an abstract class to represent the intended behavior. Using that, your DB-specific implementation would be called at runtime. Here's an abbreviated example:

public abstract class Foo {
    protected IDbOjbect _db;

    private DBType _type;
    public DBType Type {
        get { return _type; }
    }

    public Foo(DBType type) {
        _type = type;
    }

    internal abstract bool RunTest();

    public void Connect(IDbObject db) {
        _db = db;
    }

    public static Foo Create(String type) {
        switch (type) {
            case "oracle": return new FooImpl_Oracle();
        }

        return null;
    }
}

public sealed class FooImpl_Oracle : Foo {
    internal FooImpl_Oracle() : base(DBType.Oracle) {
    }

    internal bool RunTest() {
        //do something
        return true;
    }
}

[TestFixture("oracle")]
public class Tests {
    private Foo f;

    public Tests(string conn) {
        f = Foo.Create(conn);
    }

    [SetUp]
    public void SetUp() {
        Mock<IDbObject> db = new Mock<IDbObject>();
        db.Setup(x => x.CurrentDbType).Returns(f.Type);
        f.Connect(db);
    }

    [Test]
    public void TestOne() {
        Assert.IsTrue(f.RunTest());
    }
}

This will preserve the meaning of TestFixture, which is to run all tests in the fixture using a specific configuration option. It may not be an exact fit for your real situation, but hopefully these two ideas give you some direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top