How to diagnose “TestFixtureSetUp Failed”

前端 未结 11 1122
感动是毒
感动是毒 2021-02-06 21:46

We use TeamCity as our CI server, and I\'ve just started seeing \"TestFixtureSetUp Failed\" in the test failure window.

Any idea how I go about debugging th

相关标签:
11条回答
  • 2021-02-06 22:12

    I had this symptom caused by an error during field initialization. If your initialize your fields in the [SetUp] method, you should see a better error message.

    [TestFixture]
    internal class CommandParserTest
    {
        // obscure error message
        private CommandParser parser = new CommandParser(...);
        ...
    }
    
    [TestFixture]
    internal class CommandParserTest
    {
        private CommandParser parser;
    
        [SetUp]
        public void BeforeTest()
        {
            // better error message
            parser = new CommandParser(...);
        }
        ...
    }
    
    0 讨论(0)
  • 2021-02-06 22:14

    In case it can help someone: You can catch the exception and write it in the console on the TearDown

    Something like :

    [SetUpFixture]
    public class BaseTest
    {
        private Exception caughtException = null;
    
        [SetUp]
        public void RunBeforeAnyTests()
        {
            try
            {
                throw new Exception("On purpose");
            }
            catch (Exception ex)
            {
                caughtException = ex;               
            }
        }
    
        [TearDown]
        public void RunAfterAnyTests()
        {
            if (caughtException != null)
            {
                Console.WriteLine(string.Format("TestFixtureSetUp failed in {0} - {1}", this.GetType(), caughtException.Message));
            }           
        }
    
    }
    

    And the result will be :

    TestFixtureSetUp failed in IntegratedTests.Services.BaseTest - On purpose

    0 讨论(0)
  • 2021-02-06 22:19

    It is a bit of a flaw in the implementation of TestFixtureSetUp (and TestFixtureTearDown) that any exceptions are not well reported. I wrote the first implementation of them and I never got it to work the way it was supposed to. At the time the concepts in the NUnit code were tightly coupled to the idea that actions were directly related to a single test. So the reporting of everything was related to a test result. There wasn't really a space for reporting something that happened at the suite level without a huge re-write (it isn't a refactoring when you change a sheep into an escalator).

    Because of that bit of history it's hard to find out what really happened in a TestFixtureSetUp. There isn't a good place to attach the error. The TestFixtureSetUp call is a side effect of running a test instead of being directly related to it.

    @TrueWill has the right idea. Check the logs and then modify the test to add more logging if necessary. You might want to put at try/catch inside the TestFixtureSetup and log a lot in the catch block. I just thought I could add some background to it (in other words it's kind of my fault).

    0 讨论(0)
  • 2021-02-06 22:19

    I was able to see that I was not creating my test database correctly by doing a quick switch to VS Unit Testing. In my Case it was able to return a better response to the reason why it failed. I usually use NUnit. "Unable to create instance of class X. Error: System.Data.SqlClient.SqlException: A file activation error occurred. The physical file name '\DbTest.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.. "

    0 讨论(0)
  • 2021-02-06 22:22

    I was getting the same error while running any test with SpecFlow using Visual NUnit. When I tried doing the same from the Unit Test Explorer(provided by Resharper), it gave a little more helpful message: Binding methods with more than 10 parameters are not supported. I realized I can't have a SpecFlow method with more than 10 params, had to remove the test.

    0 讨论(0)
  • 2021-02-06 22:22

    Run the unit test in debug mode. You may find a runtime error in the the setup.

    0 讨论(0)
提交回复
热议问题