I have a C# test project in visual studio. This project's output path is set to "..\bin\", which is a solution level bin directory. I have a test in my project like this:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestClass
{
private TestContext context { get; set; }
[ClassInitialize]
public static void ClassInitialize(TestContext c)
{
//generate some test data xml
XDocument.Save("|DataDirectory|\\TestProjectData\\data.xml");
}
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestProjectData\\data.xml", "testcase", DataAccessMethod.Random)]
public void TestMethod1()
{
Console.WriteLine("test case run");
}
}
I have successfully run this test, and I receive the test results as a datasource should - I see the test result per row number, etc. However, I have been unable to execute the test case again successfully. I started getting the error:
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.
When I attempt to debug the test, I set breakpoints in the TestMethod and ClassInitialize. However, it appears the ClassInitialize method is not being called anymore. What would cause a ClassInitialize method to only optionally be called? My understanding is that it would always be called before any test method was called, and I also saw this work once successfully before beginning to fail.
Since this worked once successfully, I have tried things like rebuilding, cleaning solution, restarting VS, restarting computer, etc. in hopes that some file created during the first successful run would be deleted. However, the test continues to fail after only one execution.
I have looked at questions such as
- Forcing ClassInitialize to execute before testmethod data is read
- Data driven tests generated in ClassInitialize: no longer working in Visual Studio 2012
There is a common thread around this topic when I google the issue - The suggestion usually always revolves around using MSTestHacks. However, I would like to know what specifically causes this issue. Is there some app.config, visual studio setting, etc. that would allow this to work without requiring a third-party library. The fact that this does work with a library indicates to me that there's an answer to this issue. Perhaps it's just not easy to find via googling.