[翻譯]NUnit---Property and Random Attributes(十四)

 

小記:因爲工做瑣碎,沒得心情翻譯並且也在看《CLR vis C#》,因此斷更了差很少5個月,如今繼續翻譯,保證會翻譯完成,不會有始無終。

    另:NUnit已經更新到2.6.3版本,雖然正在開發2.9.6(聽說會大版本更新爲NUnit3.0),可是2.6.2的不少東西不會變,只不過會支持.NET4.5的某些新東西,因此筆者會在翻譯2.6.2以後會保證也會翻譯更新的給你們。也許翻譯的不是很好或者不許確,僅給你們提供參考。安全

    有錯誤的地方,歡迎斧正。dom

廢話很少說,上蒸菜:函數

PropertyAttribute (NUnit 2.4)

  Property 特性使用鍵值對的形勢,爲任何測試用例或事例(fixture之後翻譯爲事例)提供了統一設置屬性的方法。測試

  在下面的示例中,事例類MathTests賦予Location屬性值爲723,測試用例AdditionTest 賦予Severity 屬性值 "Critical"ui

Example:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture, Property("Location",723)]
  public class MathTests
  {
    [Test, Property("Severity", "Critical")]
    public void AdditionTest()
    { /* ... */ }
  }
}

使用注意事項:spa

  NUnit自身並不使用Property特性,但會在XML輸出文件和GUI測試屬性對話框中顯示。翻譯

  同時,可使用擴展來訪問指定屬性的值。也能夠在測試中使用反射來訪問屬性值。code

 

Custom Property Attributes

  用戶能夠繼承PropertyAttribute來自定義特性,NUnit能夠自動識別。Property特性提供了一個protected修飾的構造函數,這個構造函數會使用這個屬性值。NUnit自身的一些屬性其實是Property特性的變異。orm

  下面是示例會建立一個Severity屬性。出了會有一個簡單語法和類型安全以後,其他和通常屬性同樣。一個測試報告系統可能會利用這個屬性來提供專業報告。blog

public enum SeverityLevel
{
    Critical,
    Major,
    Normal,
    Minor
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class SeverityAttribute : PropertyAttribute
{
    public SeverityAttribute( SeverityLevel level )
        : base( level ); 
}

...

[Test, Severity( SeverityLevel.Critical)]
public void MyTest()
{ /*...*/ }

從NUnit2.5開始,一個屬性類特性能夠包含多個鍵值對。這個功能沒有對外公開,可是在派生屬性類中可使用,NUnit自身的某些特性使用了這個功能,如:RequiresThreadAttribute。

 

RandomAttribute (NUnit 2.5)

  Random特性用於爲一個參數化方法的一個參數提供一個隨機組合值範圍。NUnit會將每一個參數的數據組合爲一些了測試用例,因此若是爲參數提供數據則必須爲因此參數提供數據。

  通常默認NNunit的組合方法會將參數提供的數據進行因此可能的組合值。但能夠在具體的測試方法使用指定的特性進行修改。

  Random特性支持如下幾種構造:

  

public Random( int count );
public Random( double min, double max, int count );
public Random( int min, int max, int count );

Example

  如下的測試會執行15次,參數x有三個值,參數d會選擇從-1.0到1.0的5個值,造成15次組合值

[Test]
public void MyTest(
    [Values(1,2,3)] int x,
    [Random(-1.0, 1.0, 5)] double d)
{
    ...
}

 

同理:

  • ValuesAttribute
  • RangeAttribute
  • SequentialAttribute
  • CombinatorialAttribute
  • PairwiseAttribute
相關文章
相關標籤/搜索