locked
DataSourceAttribute: Specifying a connection string RRS feed

  • Question

  • I am failing in finding a way how to pass a connection string defined in the app.config file in the C# 4.5 Unit Test project into the DataSourceAttribute (to enable data-driven unit tests).

    The project contains a ABC.sdf file.
    App.config looks like this (A.B.C.D.Test is the default namespace of the unit test project:

    <?xml version="1.0" encoding="utf-8" ?><configuration>    <configSections>    </configSections>    <connectionStrings>        <add name="A.B.C.D.Test.Properties.Settings.UnitTestDataConnectionString"             connectionString="Data Source=&quot;UnitTestData.sdf&quot;"             providerName="Microsoft.SqlServerCe.Client.4.0" />    </connectionStrings></configuration>

    How to correctly write down the [DataSource(connectionString, tableName)] attribute of a test method? All my attempts failed.

    Thanks.

    Wednesday, March 27, 2013 3:57 PM

Answers

All replies

  • Hello,

    Thank you for your post.

    You can reference the following MSDN document to use an app.config file to define the data source for unit test and then pass the connection string in DataSourceAttribute. There are detailed steps and file contnt.

    See: http://msdn.microsoft.com/en-us/library/ms243192(v=vs.110).aspx

    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.

    • Marked as answer by Amanda Zhu Tuesday, April 2, 2013 8:26 AM
    Thursday, March 28, 2013 3:25 AM
  • Hello,

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please feel free to reopen it or submit a new question.

    Thanks for your understanding.

    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.

    Tuesday, April 2, 2013 8:26 AM
  • Here they are expecting the connection string alone from app.config and the table name still they are keeping in the DataSource paramater.

    Is that a possible scenario, like the connection string values are configurable in app.config file and the table name is specified in the Datasource property?

    [DataSource(connectionString, tableName)]

    <connectionStrings>

    <add name="UnitTestDataConnectionString"             connectionString="Data Source="(loacldb); Inital Catalog:MyTestDB; Integrated Security=True"             providerName="System.Data.SqlClient" />    </connectionStrings>

    [DataSource("UnitTestDataConnectionString","testTable")]

    how can we configure the datasource like this?

    Wednesday, March 29, 2017 10:04 AM