是否可以改变单元测试的调用方式?

发布于 2024-08-06 19:58:55 字数 193 浏览 7 评论 0原文

我的猜测是,单元测试的当前语义涉及实际调用该方法,即,如果我有一个方法 MyTest() 那么这就是被调用的方法。我的问题是:是否有可能以某种方式改变测试执行方式的管道(最好不重新编译测试运行程序),这样,不是直接调用该方法,而是通过我提供的包装器调用(即 MyWrapper(MyTest))?

谢谢。

My guess is that the current semantics of unit testing involve actually calling the method, i.e., if I have a method MyTest() then that's what gets called. My question is this: is it possible to somehow change the pipeline of the way tests are executed (preferably without recompiling the test runner) so that, say, instead of calling the method directly it's called via a wrapper I provide (i.e., MyWrapper(MyTest))?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-08-13 19:58:55

如果您使用 MbUnit,那么您可以通过定义自定义属性来自定义很多内容。

最简单的方法是创建 TestDecoratorAttribute 的子类并重写 SetUp、TearDown 或 Execute 方法,以使用您选择的附加逻辑来包装它们。

但是,如果您需要更精细的控制,则可以创建 TestDecoratorPatternAttribute 的子类,并使用逻辑重写 DecorateTest 方法以添加其他测试操作或测试实例操作。

例如,MbUnit [Repeat] 属性的工作原理是使用循环和一些额外的记录来包装测试的主体运行操作(运行测试的所有阶段)以重复运行测试。

以下是 RepeatAttribute 的代码: http://code.google.com/p/mb-unit/source/browse/trunk/v3/src/MbUnit/MbUnit/Framework/RepeatAttribute.cs

If you use MbUnit then there's lot of stuff you can customize by defining custom attributes.

The easiest way to do this is to create a subclass of TestDecoratorAttribute and override the SetUp, TearDown or Execute methods to wrap them with additional logic of your choice.

However if you need finer control, you can instead create a subclass of TestDecoratorPatternAttribute and override the DecorateTest method with logic to add additional test actions or test instance actions.

For example, the MbUnit [Repeat] attribute works by wrapping the test's body run action (which runs all phases of the test) with a loop and some additional bookkeeping to run the test repeatedly.

Here's the code for RepeatAttribute: http://code.google.com/p/mb-unit/source/browse/trunk/v3/src/MbUnit/MbUnit/Framework/RepeatAttribute.cs

断舍离 2024-08-13 19:58:55

这取决于单元测试框架如何提供拦截和扩展能力。

大多数框架(MSTest、NUnit 等)允许您定义保证在测试之前和之后运行的 Setup 和 Teardown 方法。

xUnit.NET 具有更高级的可扩展性机制,您可以在其中定义可用于装饰测试方法的自定义属性改变它们的调用方式。例如,有一个 TheoryAttribute 允许您定义参数化测试。

我不了解MBUnit,所以我不能说它是否支持这些场景。

It depends on how the unit testing framework provides interception and extensibility capabilities.

Most frameworks (MSTest, NUnit etc.) allow you to define Setup and Teardown methods that are guaranteed to run before and after the test.

xUnit.NET has more advanced extensibility mechanisms where you can define custom attributes you can use to decorate your test methods to change the way they are invoked. As an example, there's a TheoryAttribute that allows you to define Parameterized Tests.

I don't know MBUnit, so I can't say whether it supports these scenarios or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文