如何诊断“TestFixtureSetUp失败”

我们使用TeamCity作为CI服务器,并且在testing失败窗口中刚开始看到"TestFixtureSetUp Failed"

任何想法如何去debugging这个问题? testing在我的工作站(VS2008中的R#testing运行器)上运行良好。

在TestFixtureSetUp(和TestFixtureTearDown)的实现中存在一些缺陷,即任何exception都没有被很好的报告。 我写了他们的第一个实现,我从来没有按照它应该的方式工作。 当时,NUnit代码中的概念与行为与单个testing直接相关的想法紧密相关。 所以,一切的报告都与testing结果有关。 在没有大量重写的情况下,没有真正的空间来报告在套件级别发生的事情(当您将绵羊换成自动扶梯时,这不是一个重构)。

由于这一点的历史,很难找出TestFixtureSetUp中发生了什么。 没有一个好的地方来附加错误。 TestFixtureSetUp调用是运行testing的一个副作用,而不是与它直接相关。

@TrueWill有正确的想法。 检查日志,然后修改testing以在必要时添加更多日志logging。 你可能想把try / catch放在TestFixtureSetup里,并在catch块中logging很多。 我只是想我可以添加一些背景(换句话说,这是我的错)。

我会先检查生成日志。

如果不是那么明显,你可以尝试在testing中包含Console.WriteLines – 我不是很积极,但是我认为这些都写到了Build Log中。 或者你可以login到一个文件(即使使用log4net如果你想花哨)。

如果您在CI服务器上安装了Visual Studio,则可以尝试从此处运行构build/testing。 如果这是一个连接问题,那可能会解决它。

但是,我已经看到了path问题,文件的相对path不再正确,或者使用了绝对path。 这些很难debugging,可能需要loggingpath,然后检查它们是否存在于构build服务器上。

我在创build一些集成testing时遇到了这个问题,这些testing有很长的运行时间,我不想复制。 我最终将所有的testing夹具设置逻辑封装在try / catch中。 然后,我添加一个SetUp方法,其唯一目的是查看灯具设置期间是否发生故障,并提供更好的日志logging。

 Exception testFixtureSetupException = null; [TestFixtureSetUp] public void FixtureSetup() { try { // DoTestFixtureSetup } catch (Exception ex) { testFixtureSetupException = ex; } } [SetUp] // NUnit doesn't support very useful logging of failures from a TestFixtureSetUp method. We'll do the logging here. public void CheckForTestFixturefailure() { if (testFixtureSetupException != null) { string msg = string.Format("There was a failure during test fixture setup, resulting in a {1} exception. You should check the state of the storage accounts in Azure before re-running the RenewStorageAccountE2ETests. {0}Exception Message: {3}{0}Stack Trace:{4}", Environment.NewLine, testFixtureSetupException.GetType(), accountNamePrefix, testFixtureSetupException.Message, testFixtureSetupException.StackTrace); Assert.Fail(msg); } } 

当使用Visual NUnit运行SpecFlow的任何testing时,我得到相同的错误。 当我尝试从unit testing资源pipe理器(由Resharper提供)做同样的事情时,它给了一些更有帮助的消息:不支持超过10个参数的绑定方法。 我意识到我不能有超过10个参数的SpecFlow方法,不得不删除testing。

通过快速切换到VSunit testing,我能够看到我没有正确创buildtesting数据库。 在我的情况下,它能够更好地回应它失败的原因。 我通常使用NUnit。 “无法创build类X的实例。错误:System.Data.SqlClient.SqlException:发生文件激活错误,物理文件名'\ DbTest.mdf'可能不正确,请诊断并更正其他错误,然后重试该操作。 CREATE DATABASE failed。某些列出的文件名无法创build,请检查相关的错误..“

debugging模式运行unit testing。 您可能会在安装程序中发现运行时错误。

如果您在Visual Studio中使用SpecFlow和C#,请在testing失败后查看自动生成的<whatever>.feature.cs文件。 在public partial class <whatever>Feature行上,你应该看到一个符号,当它被覆盖时会显示出NUnit fixture fixture失败的原因。 就我而言,我的TestHooks类中的一些BeforeFeature方法并不是静态的。 所有BeforeTestRunAfterTestRunBeforeFeatureAfterFeature方法都需要是静态的。

我有这个问题,这是由在类中添加一个私有的只读Dictionary引起的,就像添加一个private const string

我试图使Dictionary不变,但你不能在编译时做到这一点。 我解决了这个问题,把我的Dictionary放在返回它的方法中。

我有这个症状在现场初始化过程中出现错误。 如果你在[SetUp]方法中初始化你的字段,你应该看到更好的错误信息。

 [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(...); } ... } 

今天我很困扰 我做了以下来获得实际的错误。

(1)在另一个夹具中写入另一个testing,该夹具初始化有问题的testing夹具的实例,明确地调用诸如TestFixtureSetUp和SetUp的设置方法(如果有的话),然后执行目标testing方法。

(2)为上面的新代码添加exception处理代码,并将实际exceptionlogging/输出到某处。

万一它可以帮助别人:你可以捕捉exception,并将其写在TearDown的控制台上

就像是 :

 [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)); } } } 

结果将是:

在IntegratedTests.Services.BaseTest中TestFixtureSetUp失败 – 故意