gpt4 book ai didi

c# - VS 团队测试 : . 以 Excel 作为数据源的网络单元测试:适配器失败

转载 作者:太空狗 更新时间:2023-10-29 22:23:40 25 4
gpt4 key购买 nike

我正在尝试使用 Excel 作为数据源进行单元测试。我收到以下异常。我们如何纠正它?

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"


  [TestMethod]
[Owner("Lijo ")]
[TestProperty("TestCategory", "Developer"),
DataSource("Microsoft.ACE.OLEDB.12.0",
"Data Source=C:/Sheets/DataSheet.xlsx;Extended Properties=Excel 12.0;",
"[Sheet1$]",
DataAccessMethod.Sequential)]
public void ChangePasswordTest()
{

int a = Convert.ToInt32(TestContext.DataRow[0]); //(int)Column.UserId
int b = Convert.ToInt32(TestContext.DataRow[1]);
int expectedResult = Convert.ToInt32(TestContext.DataRow[2]);

MyClass myObj = new MyClass(1, "P@ssw0rd");
int actualResult = myObj.GetAdditionResult(a, b);
Assert.AreEqual<int>(expectedResult, actualResult, "The addition result is incorrect.");

}

读物:

  1. Unit Testing Error - The unit test adapter failed to connect to the data source or to read the data

  2. Data driven unit tests problem

  3. How to create Startup and Cleanup script for Visual Studio Test Project?

  4. How Does MSTEST/Visual Studio 2008 Team Test Decide Test Method Execution Order?

  5. Visual Studio 2010 Ultimate - Data Generation Plan Setting Incorrect Data Type for Column

  6. How should I unit-test a simple CRUD-class?

最佳答案

我今天完成了同样的任务。在经历了一些头痛之后,我无需 app.config 就可以解决问题:

[TestMethod]
[DataSource("System.Data.OleDB",
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Sheets\DataSheet.xlsx; Extended Properties='Excel 12.0;HDR=yes';",
"Sheet1$",
DataAccessMethod.Sequential
)]
public void ChangePasswordTest()
{
//Arrange

//Act

//Assert

}

如果您在测试项目中使用 excel 文件作为资源,则必须将文件的复制到输出目录属性设置为Copy alwaysCopy if newer。并将 DeploymentItem 属性添加到您的测试中:

[TestMethod]
[DataSource("System.Data.OleDB",
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=.\DataSheet.xlsx; Extended Properties='Excel 12.0;HDR=yes';",
"Sheet1$",
DataAccessMethod.Sequential
)]
[DeploymentItem(".\DataSheet.xlsx")]
public void ChangePasswordTest()
{
//Arrange

//Act

//Assert

}

关于c# - VS 团队测试 : . 以 Excel 作为数据源的网络单元测试:适配器失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com