quldude
quldude

Reputation: 539

SetUpFixture being executed multiple times

I have the following code:

File1.cs contains:

namespace TestsNM
{
    [SetUpFixture]
    public class MySetUpClass
    {
        [SetUp]
        public void RunBeforeAnyTests()
        {
            //....
        }

        [TearDown]
        public void RunAfterAnyTests()
        {
            //...
        }
    }
}

File2.cs contains:

namespace TestsNM
{
    [TestFixture]
    public class LoginTest : MySetUpClass
    {
        [Test]
        public void LoginInValid()
        {
            //...
        }

        [Test]
        public void LoginValid()
        {
            //...
        }
    }
}

File3.cs contains:

namespace TestsNM
{
    [TestFixture]
    public class AddTest : MySetUpClass
    {
        [Test]
        public void Execute1()
        {
            //...
        }

        [Test]
        public void Execute2()
        {
            //...
        }
    }
}

File4.cs contains:

namespace TestsNM
{
    public class Tests
    {
        [Suite]
        public static IEnumerable Suite
        {
            get
            {
                return GetSuite();
            }
        }

        static ArrayList GetSuite()
        {
            ArrayList suite = new ArrayList();

            suite.Add(new LoginTest());
            suite.Add(new AddTest());

            return suite;
        }
    }
}

In Main():

string[] my_args = { "/fixture=" + typeof(Tests), Assembly.GetExecutingAssembly().Location };
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

When i run this, MySetUpClass is being executed before and after every Test. How can i make that class run once only before and after all TestFixtures?

Upvotes: -1

Views: 199

Answers (2)

quldude
quldude

Reputation: 539

That didn't help. What i wanted was the following output: #run RunBeforeAnyTests() #run all Tests in TestFixture class-LoginTest. #run all Tests in TestFixture class-AddTest. #run RunAfterAnyTests() I acheived this by putting a [TestFixtureSetUp] and [TestFixtureTearDown] in class-Tests. public class Tests { [TestFixtureSetUp] public void Setup(); [TestFixtureTearDown] public void Teardown(); [Suite] public static IEnumerable Suite; }

Also took out the base class-MySetUpClass.

Upvotes: 0

Silvaethor
Silvaethor

Reputation: 65

You must change the tag [SetUp] to [TestFixtureSetUp] and [TearDown] to [TestFixtureTearDown].

Upvotes: 1

Related Questions