Jash
Jash

Reputation: 962

How to attach data source at class initialize in Unit Test in C#

i am writing Data driven unit tests in C#. I want to attach data source at class level so that all the test will run for set of inputs. Some how the problem is with the TestContext property which i cannot access as the ClassInitialize method is static. Below is the code -

    [ClassInitialize]
            [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
                "TrackingData.csv",
                "TrackingData#csv",
                DataAccessMethod.Sequential)]
            public static void ClassInit(TestContext tc)
    {
       // Inside this i am trying to access the static TestContext property but its not working
    }

public static TestContext TestContext {get; set;}

The error which i am getting is 'you cannot have a static TestContext Property.

Could some one please help me out here?

Upvotes: 0

Views: 1946

Answers (1)

vc 74
vc 74

Reputation: 38179

TestContext has to be an instance property, it cannot be static. I'm afraid you have no other option than duplicating the attribute for each TestMethod.

One thing you could do, is to move the description of your data source to the config file as explained here.

Upvotes: 1

Related Questions