locked
Configure SQL connection string for DataSource attribute in unit testing RRS feed

  • Question

  • Hi,

    I am referring to this article for creating data-driven unit test cases using a SQL connection string app.config file.

    https://docs.microsoft.com/en-us/visualstudio/test/walkthrough-using-a-configuration-file-to-define-a-data-source?view=vs-2017

    Code:

    [TestClass]
    public class IntegrationTest
    {
    public TestContext TestContext { get; set; }
    
    	[TestMethod]
    	[DataSource("MySQLDataSource")]
    	public void TestRequestSummary()
    	{
    		string customerId = TestContext.DataRow["MessageId"].ToString();
    		Assert.IsNotNull(customerId);
    	}
    }

    My App.config file

    <configuration>
      <configSections>
        <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </configSections>
      <connectionStrings>    
        <add name="MySQL" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MYPortal;Integrated Security=True;" providerName="System.Data.SqlClient"/>
      </connectionStrings>
      <microsoft.visualstudio.testtools>
        <dataSources>
          <add name="MySQLDataSource" connectionString="MySQL" dataTableName="ServiceLog" dataAccessMethod="Sequential"/>
        </dataSources>
      </microsoft.visualstudio.testtools>
     </configuration>

    When I am trying to debug the test case it failed with the following error:

    Result2 StackTrace: at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestDataSource.GetData(ITestMethod testMethodInfo, ITestContext testContext)
    Result2 Message: The type initializer for 'Microsoft.VisualStudio.TestTools.UnitTesting.TestConfiguration' threw an exception.

    Could anyone help me to solve this? I am using VS 2017 Professional

    Tuesday, January 15, 2019 6:17 AM

All replies

  • Hi sagar-d,

    Thanks for your posting.

    Could you share some details about the framework your project uses and the detailed references about test (name and  versions) you use. So that we can reproduce this issue for you.

    Looking forward to hearing from you.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 16, 2019 9:10 AM
  • Hi friend,

    As for this issue, here is a workaround :(I use vs2017 and sql2012 and sql2017, all works)

    1.Right click on the project in solution explorer and choose "manage nuget packages" , uninstall the MSTest.TestAdapter and MSTest.TestFramework.

    2.After that, right-click references=>add reference "Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll" (version:10.0.0.0)

    3.And replace your app.config file with file below, then the data source can work.

    <?xml version="1.0" encoding="utf-8" ?> 

    <configuration> 

        <configSections> 

            <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 

        </configSections> 

        <connectionStrings> 

            <add name="MySQL" connectionString="Data Source=.\sqlexpress;Initial Catalog=MyPortal;Integrated Security=True" providerName="System.Data.SqlClient" /> 

        </connectionStrings> 

        <microsoft.visualstudio.testtools> 

            <dataSources> 

                <add name="MySQLDataSource" connectionString="MySQL" dataTableName="ServiceLog" dataAccessMethod="Sequential"/> 

            </dataSources> 

        </microsoft.visualstudio.testtools> 

    </configuration>

     

    Note1: The version 10.0.0.0 must match the reference we add. And to connect successfully to database, please make sure your server name is "xxx\SQLEXPRESS" , database name is MyPortal, table name is ServiceLog.

    Note2: If you add the "Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll" (version:10.1.0.0)  don’t forget to replace Version=10.0.0.0 with Version=10.1.0.0 in section node.

    Note3: If your server name is xxx\sqlexpress, then data source in connectionstring should be “.\sqlexpress”.And if your server is “. ”, just make “Data source=.;Initial…...”

    Hope it helps, feel free to contact us if there has any update! 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 17, 2019 7:09 AM
  • Hi friend,

    For my answer, is there anything confused here?

    If my answer is helpful, please mark it as answer that I have proposed the solution. For that, more and more members could know this issue and get a favor quickly.

    Thank you for your understanding.

    Br

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 21, 2019 4:03 AM
  • Hi sagar_d,

    Not get your response for several days, would you please share us the latest information in your side?

    Like Lance's suggestion, the high VS version really changed certain assembly version. So you could change the version and test it in your side.

    Sincerely,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 22, 2019 9:00 AM
  • Hello, Lance,

    I tried your solution but it did not work, any other workarounds? I still get the error: 

    "The unit test adapter failed to connect to the data source or to read the data."

    Friday, March 8, 2019 10:33 AM
  • Hi friend,

    1.Please check the sql structure below, I try to create same structure like you(even the name should be same):

    2.Make sure you create a App.config under your project, not solution

    3.Edit the App.config like what I suggest in last reply

    4.Check the server name of your Microsoft Sql Server Management Studio 2017, if the server name is ".", change the "Data Source=.;" in App.config, or change to "Data Source=.\sqlexpress;" if the server name is xxx\sqlexpress. (I suggest you use windows authentication as the authentication when connecting to sql server.)

    5.Add the script below to test it again.

    public TestContext TestContext { get; set; }
    
            [DataSource("MySQLDataSource")]
            [TestMethod]
            public void TestMethod1()
            {
                string customerId = TestContext.DataRow["MessageId"].ToString();
                //Assert.IsNotNull(customerId);
                Console.WriteLine(customerId);
            }

    I totally understand your meaning. But any mistake in this progress can result in such error message, which makes it hard to trouble shooting.Do hope you can understand.

    In addition: Today i've create a new test project to test the workaround, and I can't reproduce the issue in my machine with vs2017 15.9.7 and sqlserver2017 express. If the issue persists, please share some details about your sql database, the structure and the server name. And we will check it directly.

    Best Regards

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 11, 2019 7:10 AM