0

我有一个单元测试,我想在其中命名参数,例如:

[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 Seeder 的情况,我可以使用我的单元测试来播种,只需传入参数。但由于该方法需要参数,Visual Studio 不会将其视为要运行的测试。我怎样才能解决这个问题?

4

2 回答 2

1

AFAIK, VS unit test don't support parameterized unit tests.
Your only way is to write another, parameterless overload and call your method from there. BTW: The optional parameters are nothing but syntactical sugar; the method still requires the parameters.

Example:

public void Test_One(bool p1, string p2)
//...
public void Test_One()
{
   Test_One(true, "defaultvalue");
}

You should take a look at another framework, e.g. NUnit, which allows you to easily parametrize your tests (even with ranges, automatic combination of parameters etc.): See http://nunit.org/index.php?p=parameterizedTests&r=2.5

于 2013-07-03T06:46:26.463 回答
1

您可以扩展 MSTest 以允许这样做。

http://blogs.msdn.com/b/vstsqualitytools/archive/2009/09/04/extending-the-visual-studio-unit-test-type-part-2.aspx

于 2013-07-03T06:53:16.057 回答