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 ?
That really isn't the intended use of
TestFixture
... If you don't want to use anif
, why not just be explicit in your tests? You can use aTestCase
for values you expect to pass a certain test.Assuming you want to test more than two values, you can also add additional
TestCase
's for values that correspond to the expected behavior: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: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.