2

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

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.

Community
  • 1
  • 1
Zee
  • 1,780
  • 3
  • 16
  • 27
  • `ClassInitialize` is only called once per run, you may want to use `TestInitialize` instead, which is called once per test instead of once per run. – Ron Beyer Sep 01 '15 at 15:37
  • There is no object used in the snippet that can generate that exception. Nor does it matter at all that the [ClassInitialize] method wouldn't run, that XML document is still there. Surely you can help us help you by using the debugger and posting a better snippet. – Hans Passant Sep 01 '15 at 15:39
  • @HansPassant The DataSource attribute cannot produce the exception specified? The exception text seems to directly indicate the issue is with the data source of the data-driven test. Also, when I debug, the ClassInitialize is never hit. Where exactly would I breakpoint? – Zee Sep 01 '15 at 15:42
  • @RonBeyer The test generation only needs to be run once per run. It contains data for the data-driven TestMethod. – Zee Sep 01 '15 at 15:42
  • Good, you know how to use the debugger. Then you can show us the exception's stack trace. – Hans Passant Sep 01 '15 at 15:44
  • The issue may be that the test runner is enumerating the properties of the methods in the class first, and creating the `DataSource` attribute (which I think is what is throwing your exception) before the ClassInitialize is run. Try removing the `DataSource` attribute and set a breakpoint inside the `ClassInitialize`, I'm not sure its the case but it sounds like it may be. – Ron Beyer Sep 01 '15 at 15:45
  • 1
    @RonBeyer When I comment out the DataSource attribute on the TestMethod, the ClassInitialize does execute again. When I then uncomment it, the ClassInitialize is skipped again. I agree, the failure is due to the DataSource attribute. Since the xml file is created at runtime during the ClassInitialize, the file should exist before the DataSource attribute has a chance to fail. – Zee Sep 01 '15 at 15:59
  • @HansPassant When debugging, this is the info I get: "------ Run test started ------ An exception occurred while invoking executor 'executor://mstestadapter/v1': Object reference not set to an instance of an object. ========== Run test finished: 0 run (0:00:01.040836) ==========" – Zee Sep 01 '15 at 16:02
  • 1
    That is not a stack trace. Practice using the debugger and the Debug >Exceptions dialog. – Hans Passant Sep 01 '15 at 16:06
  • "the file should exist before the DataSource attribute has a chance to fail." You're assuming that the runner will not try to load the file identified in the attribute _before_ `ClassInitialize` is executed, which may not be the case. If you want a static data source to test against, why are you creating it in `ClassInitialize`? Why not just create it offline and save it with your test project? – D Stanley Sep 01 '15 at 16:10
  • 1
    @DStanley Like I said in the question, this DID work once. The issue is that the test isn't repeatable as something during the first run changed state. Also, something MSTestHacks does in their library makes their runtime datasource of ienumerable work - I just don't know what it is I'm doing that is different. – Zee Sep 01 '15 at 16:13

0 Answers0