2010-11-09 100 views
2

是否可以使用VS2010測試工具編寫參數化測試Silverlight?使用VS2010測試工具進行參數化測試

在常規NUnit測試,這將使用的TestCase屬性來完成...

[Test] 
[TestCase("myParam1")] 
[TestCase("myParam2")] 
[TestCase("myParam3")] 
public void TestSomethingWithParameters(string myParam) 
{ 
    // ...some tests using myParam 
} 

這可能使用VS2010的測試工具?

回答

0

您可以使用測試方法和參數創建基類作爲虛擬屬性。 從這個類繼承時,只需要覆蓋具有所需值的屬性。 請參閱示例代碼:

public class Operation 
{ 
    public static int Add(int x, int y) 
    { 
     return x + y; 
    } 
} 

[TestClass] 
public class AddTests : WorkItemTest 
{ 
    protected virtual int First{get { return 0; }} 
    protected virtual int Second{get { return 0; }} 

    [TestInitialize] 
    public virtual void Init() 
    { 
     //Init code 
    } 

    [TestCleanup] 
    public virtual void Clean() 
    { 
     //Clean code 
    } 

    [TestMethod] 
    [Description("x+y = y+x")] 
    public virtual void Test_operation_commutativity() 
    { 
     Assert.AreEqual(Operation.Add(Second, First), Operation.Add(First, Second)); 
    } 
} 

[TestClass] 
public class AddPositiveTest : AddTests 
{ 
    protected override int First { get { return 1; } } 
    protected override int Second { get { return 2; } } 
} 

[TestClass] 
public class AddNegativeTest : AddTests 
{ 
    protected override int First { get { return -1; } } 
    protected override int Second { get { return -2; } } 
}