Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:51:48+00:00 2026-06-12T20:51:48+00:00

I have a test class and a class like below. public class Foo {

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:51:49+00:00Added an answer on June 12, 2026 at 8:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test like below: public class TestSizeAnnotation { public static void main(String[]
I have wrote below code. import java.io.*; class Test { public static void main(String
I have an example class defined like below: public class FooBar { void method1(Foo
I have a structure like below: public class BaseClass { public string SendError(string Message){
I have custom class like the below; Public MyClass Public Property MyText() As String
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I have a class that defines its own enum like this: public class Test
If I have: class Test { private Vector2 v; public Vector2 Velocity { get
I have an interface like below public interface FooDAO { public void callA(String x);
I have a model called 'UserRoleHolder' like below. @Entity public class UserRoleHolder extends Model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.