3

I'm having some troubles getting my unit tests to be setup to use an Excel .xlsx data source.

My App.config file:

<?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="TestData" connectionString="Dsn=Excel Files;dbq=TestData.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
  </connectionStrings>
  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="GetAllCellNamesTest" connectionString="TestData" dataTableName="GetAllCellNamesTest$" dataAccessMethod="Sequential"/>
</dataSources>

I have verified that it is finding TestData.xlsx, and there is a sheet named GetAllCellNamesTest.

In my unit test class, I have the following setup:

        [TestMethod()]
        [DeploymentItem("TestProject\\TestData.xlsx")]
        [DataSource("GetAllCellNamesTest")]
        public void GetAllCellNamesTest()
        {
            // ... test code

TestData.xlsx is being copied to the test results directory and all the unit tests which don't try to reference the data source are passing.

However, this one test is failing with the following message:

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.
Error details: ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Access database engine could not find the object 'GetAllCellNamesTest$'. Make sure the object exists and that you spell its name and the path name correctly. If 'GetAllCellNamesTest$' is not a local object, check your network connection or contact the server administrator.

I'm really not sure where in my setup is wrong, I followed this walkthrough on MSDN to get setup: Walkthrough: Using a Configuration File to Define a Data Source. Note that I did change the section version to 10.0.0.0 because I am using .net 4.0 (per the note at the bottom of the page).

edit: oh, and all files are located locally on my computer.

helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • This is one of the reasons I prefer NUnit and xUnit over MSTest. Working all in code, with [TestCase] or [Theory] is just way more convenient. – Mathias Sep 25 '11 at 21:47

3 Answers3

3

Have you tried using a full file path?

Is the file readonly?

Valamas
  • 24,169
  • 25
  • 107
  • 177
  • Ah, thanks. Using the full path worked. However, I would really like to use a relative path since the project is in a subversion repo. Do you know what the default directory to search in is? – helloworld922 Sep 25 '11 at 21:48
  • My guess would be the test is running out of bin directory. Assuming \bin\debug, maybe try an attribute of `..\..TestData.xlsx` – Valamas Sep 25 '11 at 23:47
  • Write out this in a console or quickly to file. `DirectoryInfo ApplicationRoot = new DirectoryInfo(Environment.CurrentDirectory); `In my test project I am using `DirectoryInfo ApplicationRoot = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent;` to find the project root. – Valamas Sep 25 '11 at 23:49
  • 2
    Ok, so by adding the `[DeploymentItem]`, it moved the file to the correct execution directory. I finally figured out that `defaultdir=.;` needs to be `defaultdir=.\;` (no idea why the extra slash at the end is needed, but it did make it work). – helloworld922 Sep 26 '11 at 00:03
2

You could also specify the files/directory you want to deploy as a part of your TestSettings. This will save you the effort of having to put the DeploymentItem attribute for every test method.

Yash Sinha
  • 21
  • 3
1

you use configuration after;

<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>
dispose
  • 115
  • 7