Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

c# - Why does data driven unit test fail in vs2012 when it worked fine in vs2010?

I have some data driven unit tests that were working just fine in Visual Studio 2010. These tests were implemented using the following pattern.

[TestMethod()]
[DeploymentItem("path_to_data_dir_relative_to_solution\my_data.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\my_data.xml", "Token", DataAccessMethod.Sequential)]
public void MyTestMethod()
{
    // Arrange
    const string EXPECTED_PARAM_NAME = "table";
    string data = TestContext.DataRow["Data"].ToString();
    var sut = new MyClassUnderTest();

    // Act
    sut.DoSomething(data);

    // Assert
    Assert.IsTrue(sut.DidSomething);
}

Here is my solution structure.

  • MySolutionFolder
    • MyTestProjectFolder
    • MyTestDataFolder
      • my_data.xml

When I run the same tests in Visual Studio 2012, they fail with the following error message.

Result 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: Object reference not set to an instance of an object.

Why are my unit tests suddenly failing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Visual Studio 2010, the DeploymentItem attribute is relative to the solution, but in Visual Studio 2012, it is relative to the project. Simply specify the DeploymentItem path relative to the project folder and the unit tests will start working again.

For more info see the following link.

http://social.msdn.microsoft.com/Forums/en-US/vsunittest/thread/4a8403a2-b495-4120-aad3-0d0becc7e45e/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...