Microsoft单元测试-在测试配置设置中找不到数据源 [英] Microsoft Unit Tests - Data source cannot be found in the test configuration settings
问题描述
我正在尝试在C#中构建数据驱动的单元测试的Test类.我想使用3个数据库:一个来自SQL,一个来自Access,一个来自Excel. 这是我的app.config文件:
I am trying to build Test class of data-driven unit tests in C#. I want to use 3 databases: one from SQL, one from Access and one from Excel. This is 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"/>
</configSections>
<connectionStrings>
<add name="MyJetConn"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=H:\SQA\CoolMath\CoolMath\Database1.accdb;
Persist Security Info=False;"
providerName="System.Data.OleDb" />
<add name="MyExcelConn"
connectionString="Dsn=Excel Files;
dbq=H:\SQA\CoolMath\CoolMath\CoolMathExcelDataTable.xlsx;
defaultdir=.;
driverid=1046;
maxbuffersize=2048;
pagetimeout=5"
providerName="System.Data.Odbc" />
<add name="MSSQLConn"
connectionString="Data Source=H:\SQA\CoolMath\CoolMath\SQLExpress;
Initial Catalog=MSSQLDB;
Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<microsoft.visualstudio.testtools>
<dataSources>
<add name="MyJetDataSource"
connectionString="MyJetConn"
dataTableName="CoolMathAcessDataTable"
dataAccessMethod="Sequential"/>
<add name="MyExcelDataSource"
connectionString="MyExcelConn"
dataTableName="Sheet1$"
dataAccessMethod="Sequential"/>
<add name="MSSQLDataSource"
connectionString="MSSQLConn"
dataTableName="dbo.CoolMathDataTable"
dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
</configuration>
当我尝试运行测试时,它们全部失败,并显示以下消息:
When I try to run the tests, they all fail with the message:
在测试配置设置中找不到数据源".
"Data source cannot be found in the test configuration settings".
我看不到我在做什么错,也许这是数据库的位置? (它们都与代码项目和XML文件位于同一个库中.)
I can't see what am I doing wrong, perhaps it is the location of the databases? (they all in the same library as the code project and the XML file).
在单元测试上方,我放置了以下属性:
Above the Unit Tests I've place the following attributes:
[TestMethod()] [DeploymentItem("CoolMath\\CoolMathExcelDataTable.xlsx")]
[DataSource("MyExcelDataSource")]
推荐答案
在Visual Studio中,数据源的相对路径已经移动. 我正在使用XML文件,但是如果xlsx文件有相同的问题,我不会感到惊讶.我必须使用以下内容来使文件正常工作
With Visual Studio the relative paths for DataSources have moved. I'm using XML files, but I would not be surprised if the xlsx files have the same problem. I had to use the following to get the files to work
[DeploymentItem("TestData\\Search.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
@"|DataDirectory|\\..\TestData\Search.xml",
"Row",
DataAccessMethod.Sequential)]
请注意第2行中的路径,它以\ .. \开头
这是因为相对路径从解决方案更改为项目(反之亦然). Test_Data是项目顶部的文件夹.
[DeploymentItem("TestData\\Search.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
@"|DataDirectory|\\..\TestData\Search.xml",
"Row",
DataAccessMethod.Sequential)]
Note the path in line 2, it is starts with a \..\
This is because relative paths changed from Solution to Project (or visa versa). Test_Data is a folder at the top of the project.
这篇关于Microsoft单元测试-在测试配置设置中找不到数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!