使 MSTest 尊重 [Conditional()] 属性?
我正在使用 VS2010,我有以下方法调用:
[Conditional("DEBUG")]
public void VerboseLogging() { }
public void DoSomething() {
VerboseLogging();
Foo();
Bar();
}
然后我对 DoSomething
方法进行单元测试,检查它是否发出正确的日志记录。
[Conditional("DEBUG"), TestMethod()]
public void EnsureVerboseLog() {
DoSomething();
VerifyVerboseLoggingCalled(); // <-- fail in release builds since VerboseLogging() calls get eliminated.
}
看来 MSTest 只看到 TestMethod
并执行它(生成失败的测试),即使我已经用 Conditional("DEBUG")
标记它并在发布模式下编译它。
那么,有没有办法根据 #if
之外的编译常量排除某些测试?
I'm using VS2010, I have the following method call:
[Conditional("DEBUG")]
public void VerboseLogging() { }
public void DoSomething() {
VerboseLogging();
Foo();
Bar();
}
Then I have a unit test for the DoSomething
method which checks that it emits proper logging.
[Conditional("DEBUG"), TestMethod()]
public void EnsureVerboseLog() {
DoSomething();
VerifyVerboseLoggingCalled(); // <-- fail in release builds since VerboseLogging() calls get eliminated.
}
It seems that MSTest only sees TestMethod
and executes it (generating failed test) even though I've marked it with Conditional("DEBUG")
and compile it in release mode.
So, is there a way to exclude certain tests depending on compilation constant other than #if
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用#if - 它是可读的。
哈..
I'd use the #if - it's readable.
HTH..
ConditionalAttribute
不影响方法是否编译到应用程序中。它控制是否将对方法的调用编译到应用程序中。此示例中没有调用
EnsureVerboseLog
。 MSTest 只是在集合中看到带有TestMethod
属性的方法并正确执行它。为了防止 MSTest 运行该方法,您需要执行以下操作之一The
ConditionalAttribute
does not affect whether or not a method is compiled into an application. It controls whether or not calls to the method are compiled into the application.There is no call to
EnsureVerboseLog
in this sample. MSTest just sees a method in the assemlby with theTestMethod
attribute and correctly executes it. In order to prevent MSTest from running the method you will need to do one of the following如果您希望能够有条件地运行不同的测试,您可能需要使用
[TestCategory]
属性:然后使用
filter
参数 以包含或排除类别:If you want to be able to conditionally run different tests, you will probably want to use the
[TestCategory]
attribute:And then use the
filter
parameter to include or exclude the categories:解决方法是将方法的
Priority
属性设置为 -1。然后以“minpriority:0”作为参数运行mstest
。A work around is to set the
Priority
attribute to -1 to your method. Then runmstest
with "minpriority:0" as an argument.