You can extend MSTest to allow this.
How to get tests in unit test project to be run (when the method has parameters)
-
02-06-2022 - |
Question
I have a unit test I want to named parameters in, e.g:
[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?
La solution
Autres conseils
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