2010-06-28 52 views
5

我使用VS2010,我有以下方法調用:使MSTest尊重[Conditional()]屬性?

[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以外的編譯常量?

回答

6

ConditionalAttribute不影響方法是否編譯到應用程序中。它控制是否將調用方法編譯到應用程序中。

此示例中沒有對EnsureVerboseLog的呼叫。 MSTest只能看到屬性爲TestMethod的方法,並正確執行它。爲了防止MSTest的運行方法,你需要做以下

  1. 一個不把它編譯到您的應用程序(可能通過#如果公司)
  2. 不能與TestMethod的屬性
其標註
+0

MSTEST吮吸一個大的比MbUnit的/開羅,是嗎? – 2010-07-01 18:47:28

1

解決辦法是將Priority屬性設置爲-1以便您的方法。然後以「minpriority:0」作爲參數運行mstest

[TestMethod()] 
[Priority(-1)] 
public void Compute_Foo() 
{ 
    This method will not be executed 
} 
3

那麼,有沒有辦法依賴於編譯 不斷比#如果其他排除某些測試?

爲什麼忽視這個顯而易見的?它是可讀的,不正是想要的工作,等等

[TestMethod] 
#if !DEBUG 
[Ignore] 
#endif 
public void AnyTest() 
{ 
    // Will invoke for developer and not in test-server! 
} 

HTH ..

相關問題