none
How to attach data source at class initialize in Unit Test in C#? RRS feed

  • Question

  • 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?

    Thursday, June 6, 2013 4:34 PM

Answers

  • Hello,

    Thank you for your post.

    I have created a test project with a unit test. I can define DataSource attribute and access the data through tc.DataRow[“columnname”] in ClassInitialize method. I think that you don’t need to mark the TestContext as static.

    But when I run the unit test, an error occurs:

    Invalid use of DataSource attribute. Method MyClassInitialize is not marked as TestMethod.

    I think that we only can use DataSource attribute for test methods.

    I suggest that you still use DataSource at TestMethod level.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 7, 2013 9:13 AM
    Moderator
  • Hello,

    Glad to receive your reply.

    Based on my reply above, we can’t use DataSource attribute for ClassInitialize method. In this case, you can’t read a row from the CSV file and perform a set of operations for all test methods through DataSource attribute.

    As my personal point of view, you can try to write your own C# code to access/read the data source and then read a column in the ClassInitialize method and then perform some operations.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 10, 2013 2:15 AM
    Moderator

All replies

  • Hello,

    Thank you for your post.

    I have created a test project with a unit test. I can define DataSource attribute and access the data through tc.DataRow[“columnname”] in ClassInitialize method. I think that you don’t need to mark the TestContext as static.

    But when I run the unit test, an error occurs:

    Invalid use of DataSource attribute. Method MyClassInitialize is not marked as TestMethod.

    I think that we only can use DataSource attribute for test methods.

    I suggest that you still use DataSource at TestMethod level.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 7, 2013 9:13 AM
    Moderator
  • Hello,

    Thanks for the reply Amanda. Yep exactly if i don't mark the TestContext as static i get different issue - 

    tc.DataRow["ColumnName"] - the Data Row property here is null for me. Sample Code - 

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
                "HeaderData.csv",
                "HeaderData#csv",
                DataAccessMethod.Sequential)]

    [ClassInitialize]

    public static void ClassIntiialize(TestContext tc)

    {

    tc.DataRow["Locale"] as string; - this line throws error that - data row is null

    }

    Also i want to use the data source attribute at Class Level and the reason is after reading a single row from CSV file i want to perform a set of operation which will be common for all testMethod. Now as i can't do this i need to do the same operation for each test method. And tests can increase to more than 100. So you see here this is not right way to do? Is there any other approach where i can meet my requirements?

    Thanks.

    Friday, June 7, 2013 3:43 PM
  • Hello,

    Glad to receive your reply.

    Based on my reply above, we can’t use DataSource attribute for ClassInitialize method. In this case, you can’t read a row from the CSV file and perform a set of operations for all test methods through DataSource attribute.

    As my personal point of view, you can try to write your own C# code to access/read the data source and then read a column in the ClassInitialize method and then perform some operations.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 10, 2013 2:15 AM
    Moderator
  • HI Amanda,

    Thanks again for the reply

    So according to you, i should write my own C# code to read the data source and then read the column in ClassInitialize method, but how will i be able to use the advantage of data driven testing, where tests run automatically for each row in data files.

    Please correct me if i am wrong here.

    Thanks

    Javed

    Wednesday, June 12, 2013 1:53 PM
  • Hello,

    If you write your own code to read data from a data source and then perform some operations in ClassInitialize method, all test methods will perform the same operations before they run.

    If what you want to do in each test method is different from what is done in ClassInitialize method, and what you want to do is still related to the data source, I think that you still can add DataSource attribute for each test method and do the data driven test. You just can’t use DataSource attribute for ClassInitialize method.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 13, 2013 1:36 AM
    Moderator