简体   繁体   中英

How to get tests in unit test project to be run (when the method has parameters)

I have a unit test I want to named parameters in, eg:

[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);
}

This is so from my EF Seeder, I can use my unit tests to seed, by just passing parameters in. But because the method takes parameters, Visual Studio doesn't count it as a test to be run. How can I get around this?

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, eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM