让MSTest的尊重[条件()]属性? [英] Make MSTest respect [Conditional()] attribute?

查看:142
本文介绍了让MSTest的尊重[条件()]属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2010,我有以下的方法调用:

I'm using VS2010, I have the following method call:

[Conditional("DEBUG")]
public void VerboseLogging() { }

public void DoSomething() {
    VerboseLogging();
    Foo();
    Bar();
}



然后我对的DoSomething <单元测试/ code>方法,它会检查它发出适当的记录。

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.
}



似乎MSTest的只能看到 TestMethod的并执行它(产生失败的测试),即使我与条件(调试)并在释放模式编译。

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?

推荐答案

ConditionalAttribute 不影响与否的方法编译到应用程序。它控制对方法调用是否被编译到应用程序。

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.

有就是这个样本中 EnsureVerboseLog 没有呼叫。 MSTest的只是看到与 TestMethod的属性assemlby的方法和正确地执行它。为了防止MSTest的运行方法,你需要做以下

There is no call to EnsureVerboseLog in this sample. MSTest just sees a method in the assemlby with the TestMethod attribute and correctly executes it. In order to prevent MSTest from running the method you will need to do one of the following


  1. 未编译到应用程序(可能是一个通过#如果的)

  2. 不与TestMethod的属性

这篇关于让MSTest的尊重[条件()]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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