testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Upgraded to MSTest v2, The type initializer for 'Microsoft.VisualStudio.TestTools.UnitTesting.TestConfiguration' threw an exception

Open stephen-nba opened this issue 9 years ago • 9 comments

Question

Has anything changed around the DataSource attribute in MSTest v2? Were there any upgrade steps I need to perform?

Description

Have upgraded to MS Test v2 (v 1.1.13). Running my unit test gives:

Message: The type initializer for 'Microsoft.VisualStudio.TestTools.UnitTesting.TestConfiguration' threw an exception.

Result StackTrace:	
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestDataSource.GetConnectionProperties(DataSourceAttribute dataSourceAttribute, String& providerNameInvariant, String& connectionString, String& tableName, DataAccessMethod& dataAccessMethod)
   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestDataSource.RunDataDrivenTest(TestContext testContext, ITestMethod testMethodInfo, ITestMethod testMethod, TestMethodAttribute executor)

Steps to reproduce

I have the following attributes on my test:

    [TestMethod]
    [DataSource("NoteData")]
    [DeploymentItem(@".\TestFiles\ExcelFiles\TestData.xls")]
    [DeploymentItem(@".\TestFiles\Note", "NoteAck")]
    [DeploymentItem(@".\TestFiles\Note", @"TestFiles\NoteAck")]

In my app.config I have


 <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="NoteData" connectionString="TestDataConnection" dataTableName="IssueNoteAck$" dataAccessMethod="Sequential" />
    </dataSources>
  </microsoft.visualstudio.testtools>

<connectionStrings>    
    <add name="TestDataConnection" connectionString="Driver={Microsoft Excel Driver (*.xls)}; dbq=TestData.xls; defaultdir=.; driverid=790; maxbuffersize=2048; pagetimeout=5; readonly=true;" providerName="System.Data.Odbc" />
  </connectionStrings>

Expected behavior

Test would pass as previously

Actual behavior

As above with exception and stack trace

Environment

Using VS 2017 15.0.0-RTW+26228.4
Win 7
  <package id="MSTest.TestAdapter" version="1.1.13" targetFramework="net462" />
  <package id="MSTest.TestFramework" version="1.1.13" targetFramework="net462" />
.Net Framework v4.6.2

stephen-nba avatar Mar 29 '17 00:03 stephen-nba

@stephen-nba : Thanks for reporting this. This definitely looks like a bug. Does this work if you provide the connection details inline in the DataSource attribute itself instead of in the configuration file?

AbhitejJohn avatar Mar 29 '17 05:03 AbhitejJohn

Wondering if there's been any movement on this? Our tests have quite a few DataSources defined in the same way, so our move to MSTest v2 is on hold for now...

Smenus avatar Sep 01 '17 12:09 Smenus

Actually, I seem to have found the fix! The issue was the old configSections declaration at the top of the app.config. I found that changing the previous one from <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> to <section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions"/> and switching the <microsoft.visualstudio.testtools> around the dataSources to <microsoft.visualstudio.qualitytools> makes everything work again!

Smenus avatar Sep 01 '17 13:09 Smenus

Great you were able to unblock yourself @smenus. Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral would have to be changed but microsoft.visualstudio.testtools should work too. This does look like a different config from the original poster though. Can you share your entire config section please?

AbhitejJohn avatar Sep 02 '17 14:09 AbhitejJohn

Of course!

We went from this

<?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="UnitTestConnection" connectionString="AConnectionString" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="TestDataSource" connectionString="UnitTestConnection" dataTableName="UnitTestTable" dataAccessMethod="Sequential" />
    </dataSources>
  </microsoft.visualstudio.testtools>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

to this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions" />
  </configSections>
  <connectionStrings>
    <add name="UnitTestConnection" connectionString="AConnectionString" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <microsoft.visualstudio.qualitytools>
    <dataSources>
      <add name="TestDataSource" connectionString="UnitTestConnection" dataTableName="UnitTestTable" dataAccessMethod="Sequential" />
    </dataSources>
  </microsoft.visualstudio.qualitytools>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

I actually can't remember where I found microsoft.visualstudio.qualitytools, I think it was somewhere in this repo actually! Edit: forgot to mention that this was with 1.1.8.

Smenus avatar Sep 02 '17 17:09 Smenus

microsoft.visualstudio.testtools is working perfectly fine with MSTestV2. i.e. final app.config may look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions" />
  </configSections>
  <connectionStrings>
    <add name="MyJetConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=testdatasource.mdb; Persist Security Info=False;" providerName="System.Data.OleDb" />
  </connectionStrings>
  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="MyJetDataSource" connectionString="MyJetConn" dataTableName="MyDataTable" dataAccessMethod="Sequential"/>
    </dataSources>
  </microsoft.visualstudio.testtools>
</configuration>

jayaranigarg avatar Dec 27 '17 14:12 jayaranigarg

@jayaranigarg @Smenus

Could you guys please have a look at my app.config because I am still getting the error, tried replacing testtools with qualitytools as well. Still its not working

shilpamalhotra-git avatar Jul 10 '20 16:07 shilpamalhotra-git

Actually, I seem to have found the fix! The issue was the old configSections declaration at the top of the app.config. I found that changing the previous one from <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> to <section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions"/> and switching the <microsoft.visualstudio.testtools> around the dataSources to <microsoft.visualstudio.qualitytools> makes everything work again!

Thank you Smenus, these codes also worked for me!

ybykpmk avatar Feb 11 '23 10:02 ybykpmk

I am using MSTest.TestAdapter and MSTest.TestFramework 2.2.3, Microsoft.NET.Test.Sdk 17.6.1 and TargetFramework net472. Visual Studio version is 17.8.7 (Microsoft Studio Enterprise 2022). My app.config is as below and get the same error - The type initializer for 'Microsoft.VisualStudio.TestTools.UnitTesting.TestConfiguration' threw an exception.

Could someone let me know what could be wrong here ? I tried to upgrade the versions of MSTest.TestFramework and MSTest.TestAdapter. But unfortunately it did not help.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="microsoft.visualstudio.qualitytools"
 type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions"/>
</configSections>  
  <connectionStrings>
    <add name="ABC.TestAutomation.MyApp.TestData.Abc"
connectionString="|DataDirectory|\Data\Testdata.xml" providerName="Microsoft.VisualStudio.TestTools.DataSource.XML"/>
  </connectionStrings>
  <microsoft.visualstudio.qualitytools>
    <datasources>
      <add name="TC123456"
           connectionString="ABC.TestAutomation.MyApp.TestData.Abc"
           dataTableName="TC123456"
           dataAccessMethod="Sequential" />
    </datasources>
  </microsoft.visualstudio.qualitytools>    
</configuration>

Test looks like below

[TestMethod, TestProperty("TestCaseId", "31715"), TestCategory("Smoke"), TestCategory("Selenium")]
[DataSource("TC123456")]
public void TestDatasourceThroughAppConfig()
{
    var title = TestContext.DataRow["Title"];
    //var title = "title";
    Assert.IsNotNull(title, "Title should not be null");
}

raghere avatar Nov 25 '24 13:11 raghere