[英]How to get tests in unit test project to be run (when the method has parameters)
我有一個單元測試我想命名參數,例如:
[TestMethod]
public void Register(bool DoSeed = false, AccountViewModelForReg VM = null)
{
AutoMapperConfig.Configure(); //TODO move to global
AccountController C = new AccountController();
VM = new AccountViewModelForReg()
{
Username = "testuser1",
Password = AccountHelper.HashPassword("a"),
Email = "testuser1"+CommonEmailSuffix,
Email2 = "testuser1"+CommonEmailSuffix
};
var Result = C.Register(VM, true) as ViewResult;
Assert.AreEqual("Register", Result.ViewName);
}
這是我的EF播種器,我可以通過傳入參數來使用我的單元測試來播種。但是因為該方法需要參數,所以Visual Studio不會將其視為要運行的測試。 我怎么能繞過這個?
AFAIK,VS單元測試不支持參數化單元測試。
你唯一的方法是編寫另一個無參數的重載並從那里調用你的方法。 順便說一句:可選參數只不過是語法糖; 該方法仍然需要參數。
例:
public void Test_One(bool p1, string p2)
//...
public void Test_One()
{
Test_One(true, "defaultvalue");
}
您應該看一下另一個框架,例如NUnit,它允許您輕松地參數化您的測試(即使是范圍,參數的自動組合等):請參閱http://nunit.org/index.php?p=parameterizedTests&r= 2.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.