krafo
krafo

Reputation: 183

static classes instances + integration tests

I have created an nunit test project with a number of integration tests in 5 different TestFixtures. I have a class called ConfigSettings which has the [SetupFixture] attribute and a method with [SetUp] attribute which basically connects to a database to retrieve settings. the settings retrieved should be used throughout the tests. The 5 different TestFixtures all inherit from this class ConfigSettings.

I'm noticing that the SetupFixture [setup] method is run for every test, so i used a flag 'HasRun' to avoid this. However when TestFixture1 is ready and the runner goes to TestFixture2 the HasRun will be 0 again since a new instance will be created. How can I have the SetupFixture attribute class run only once at the beginning of the TestSuite? A solution would be to make the HasRun property and all other properties as static, however if i then open a new instance of NUnit, the properties will have the same value of the first instance. any ideas please?

The issue is basically that a second instance of the static class would still retrieve the property value of the first instance, and if i overwrite them with new settings the first instance will use the values of the second instance. is there any workaround for this behaviour?

This is a sample code of what i'm trying to do:

Below is the SetupFixture class which should run at the beginning of the project before any other testfixture:-

[SetUpFixture]
public class ConfigSettings
{
    private static string strConnectionString = @"connectionStringHere";

    public string userName { get; set; }
    public string userId { get; set; }
    public int clientId { get; set; }
    public int isLive { get; set; }
    public int HasRun { get; set; }

    [SetUp]
    public void GetSettings()
    {
        if (HasRun != 1)
        {
            Console.WriteLine("executing setup fixture..");
            HasRun = 1;
            using (var db = new autodb(strConnectionString))
            {
                var currentSession = (from session in db.CurrentSessions
                                      where session.TestSuiteID == 1
                                      orderby session.TestStarted descending
                                      select session).FirstOrDefault();

                userId = currentSession.UserId.ToString();
                clientId = currentSession.ClientID;
                isLive = currentSession.IsLive;

                etc...
            }
        }
    }
}

Now from each TestFixture i'm inheriting the ConfigSettings Class and access it's properties for example:-

[TestFixture]
public class TestFixture1: ConfigSettings
{
    [Test]
    public void Test1()
    {
        Console.WriteLine("executing test 1...");
        Console.WriteLine(userId);
    }

    [Test]
    public void Test2()
    {
        Console.WriteLine("executing test 2...");
        Console.WriteLine(clientId);
    }
}

Thanks.

Upvotes: 2

Views: 466

Answers (1)

Mzf
Mzf

Reputation: 5260

As you already know the SetUp run only once and SetupFixture Run every tine before a test.

so to answer your question . How can I have the SetupFixture attribute class run only once at the beginning of the TestSuite? the easiest thing to do is to use SetUp instead of SetupFixture

Upvotes: 0

Related Questions