-1

我想爲某些編碼UI測試使用VS2010測試框架。我已經安裝了以下基本測試類:C#中虛擬基類方法/成員的屬性繼承

[CodedUITest] 
public abstract class TestObjectBase 
{ 
    public TestContext TestContext { get; set; } 

    public TestObjectBase() 
    { 
    } 

    [TestInitialize] 
    public virtual void Initialize() 
    { 
     Database.ResetArchiveDatabase(); 
     Database.ResetSettingsDatabase(); 
     Database.ResetTransactionsDatabase(); 
    } 

    [TestCleanup] 
    public virtual void Dispose() 
    { 
    } 
} 

所以我想的問題是以下幾點:[CodedUITest][TestInitialize][TestCleanup]屬性沒有AttributeUsage=Inherited成員。

會從TestObjectBase繼承的子類的任何實例有這些屬性傳播到自身(在[CodedUITest]及其成員的情況下(在[TestInitialize][TestCleanup])的情況下?

+1

什麼阻止你只是嘗試它? – 2012-03-22 20:02:42

+0

有趣。這是我的同事在我發佈帖子後說的。那麼,我可以。我也可能會留下這個問題,因爲在我看來這是一個很好的問題。 – bleepzter 2012-03-22 20:11:32

+0

@bleepzter,如果你找到答案,你應該自己回答這個問題,而不僅僅是「讓它留下」。 – svick 2012-03-22 20:51:54

回答

0

的默認值the AttributeUsageAttribute.Inherited propertytrue,所以我會假設,如果你不使用AttributeUsageAttribute都同樣適用。而我的測試似乎與以同意。

但作爲payo正確地指出,AttributeUsageAttribute.Inherited更多的是推薦的。這是因爲有有幾種獲取屬性的方法(或其成員):

  1. 呼叫GetCustomAttributes(inherit: false)上的類型。這樣,即使指定了Inherited = true,也不會繼承基類型的屬性。

  2. 請致電GetCustomAttributes(inherit: true)。這是實際上尊重AttributeUsageAttribute.Inherited價值的唯一方法。

  3. 瀏覽繼承層次結構並在每種類型上調用GetCustomAttributes(inherit: false)。這樣,即使指定了Inherited = false,您也可以從基本類型中獲得所有屬性。

大多數工具可能會與#2,因爲這是最明智的選擇,但肯定不是唯一的。