问题
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.
回答1:
Have you tried using a full file path?
Is the file readonly?
回答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.
回答3:
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>
来源:https://stackoverflow.com/questions/7548761/data-driven-unit-tests-problem