Inheriting SetupFixture base class from different projects (nunit)

I am trying to use a basic SetupFixtureClass to call in each of my projects within a solution.

I got my abstract class TestFixtureSetupBase which has no namespace and is in project a.

[SetUpFixture]
public abstract class TestFixtureSetupClass
{
     [FixtureSetup]
     public void init(){myRandomMethod()};

     public virtual void myRandomMethod(){};
}

      

and I got another class from project b that inherits from this class, for example:

[TestFixture]
public class OtherClassOfOtherProject : TestFixtureSetupClass
{
     public override void myRandomMethod(){...};

     [Test]
     public void randomTest(){...}
}

      

However, neither Setup nor myRandomMethod is called in this project.

What should I take care of getting the desired result? It seems that I am fulfilling the requirements mentioned in the nunit-documentation.

Edit / Update: What I am trying to do is: Create my environment once, in TestFixtureSetUp. This fails, I would like to get a nice exception. So I followed the example shown here sandshadow: qaru.site/questions/149036 / ... . So I would keep the exception and throw it when setting up each test that is run, since otherwise the exception won't show up at all (there will only be s.th. like "SetUpFixture failed" without any explanation).

+3


source to share


2 answers


When I run my code with NUnit 2.6.2, I get different results. The GUI player does not run the test and gives this error:

ConsoleApplication4.OtherClassOfOtherProject.randomTest: TestFixtureSetUp method not allowed for SetUpFixture

It makes sense. Your base class TestFixtureSetupClass

has an attribute SetUpFixture

, which means that "this class has methods marked with SetUp

or TearDown

that should run before / after any other tests in this namespace." This is not the place to include a method with an attribute TestFixtureSetUp

, which means "run this method before any tests in the fixture (class)"



Since I think you are just mixing attributes wrong, what do you want?

  • I want to be myRandomMethod

    called once before any test in my tests namespace:

    [SetUpFixture]
    public class TestFixtureSetupClass
    {
        [SetUp]
        public void init()
        {
            myRandomMethod();
        }
    
        public virtual void myRandomMethod() { }
    }
    
          

  • I want to be myRandomMethod

    called once before any test in the derived class for each derived class:

    public abstract class TestFixtureSetupClass
    {
        [TestFixtureSetUp]
        public void init()
        {
            myRandomMethod();
        }
    
        public virtual void myRandomMethod() { }
    }
    
          

Note the differences: class-level attributes, keyword abstract

and attributes on init

.

+3


source


I believe this is a duplicate of resharper unit test inheritance . The first comment indicates that this is a Resharper bug that was fixed in version 5.1.1.



0


source







All Articles