我该怎么做才能使断言不再阻止自动化测试? [英] What do I have to do so that assertions won't block automated tests anymore?

查看:40
本文介绍了我该怎么做才能使断言不再阻止自动化测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在几个虚拟机上使用 hudson/jenkins 对我们的 C# 项目运行自动化 NUnit 测试,这些虚拟机在某些服务器上几乎无人看管地运行.测试涉及启动几个交换数据的进程,其中一个是 NUnit 本身,另一个是由单元测试创​​建的.

We run automated NUnit tests on our C# projects using hudson/jenkins on several virtual machines which run mostly unattended on some server. The tests involve starting several processes that exchange data, one of which is NUnit itself, the others created by the unit test.

有时,其中一位开发人员会签入触发断言(Debug.Assert())的内容.然后会弹出一个消息框,询问用户要做什么.通常这些发生在单元测试创​​建的外部"进程之一中.他们将阻止该进程,而其他进程放弃,因为它们无法通信.但是,由于系统的性质,接下来的测试也将全部失败,只要该进程被阻止等待某人点击该消息框.

Sometimes, one of the developer checks in something that triggers an assertion (Debug.Assert()). This then pops up a message box, asking the user what to do. Usually those happen in one of the "external" processes created by the unit tests. They will block that process while the other processes give up, because they can't communicate. However, due to the nature of the system, the next tests will all fail, too, as long as that one process is blocked waiting for someone to click away that message box.

有人告诉我,您可以更改 .NET 程序的设置,这样断言就不会弹出消息框.理想情况下,该过程只需将一些内容写入 stdout 或 stderr,供 Jenkins 记录.

I've been told that you can change the settings for a .NET program so that an assertion won't pop up a message box. Ideally, the process would just write something to stdout or stderr, for Jenkins to record.

那么我需要做什么来关闭那些交互式断言对话框呢?

So what do I have to do to turn off those interactive assertion dialogs?

推荐答案

你需要实现System.Diagnostics.TraceListener,它不会在Fail时弹出对话框(即你可以向单元测试框架报告错误)并添加这个监听器使用 Listeners.Clear/Add 的默认值之一

You need to implement System.Diagnostics.TraceListener that will not pop up dialog on Fail (i.e. you can report error to unit test framework) and add this listener instead of default one by using Listeners.Clear/Add

public class MyListenerThatDoesNotShowDialogOnFail: System.Diagnostics.TraceListener
{....
    public override void Fail(string message, string detailMessage)
    {// do soemthing UnitTest friendly here
    }

}

System.Diagnostics.Debug.Listeners.Clear();
System.Diagnostics.Debug.Listeners.Add(new MyListenerThatDoesNotShowDialogOnFail());

此代码应该在您的单元测试设置部分.这样,常规调试构建将显示断言对话框,但在运行单元测试时,它会为测试做一些有意义的事情(如 Assert.Fail).请注意,您应该考虑在测试的拆卸方法中恢复原始侦听器.

This code should be in your Unit test setup portion. This way regular debug build will show assert dialogs, but while running Unit tests it will do something sensible for the test (like Assert.Fail). Note that you should consider restoring original listeners in test's teardown methods.

这篇关于我该怎么做才能使断言不再阻止自动化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆