I'm writing unit tests with NUnit and the TestDriven.NET plugin. I'd like to provide parameters to a test method like this :
[TestFixture]
public class MyTests
{
[Test]
public void TestLogin(string userName, string password)
{
// ...
}
...
}
As you can see, these parameters are private data, so I don't want to hard-code them or put them in a file. Actually I don't want to write them anywhere, I want to be prompted each time I run the test.
When I try to run this test, I get the following message in the output window :
TestCase 'MyProject.MyTests.TestLogin' not executed: No arguments were provided
So my question is, how do I provide these parameters ? I expected TestDriven.NET to display a prompt so that I can enter the values, but it didn't...
Sorry if my question seems stupid, the answer is probably very simple, but I couldn't find anything useful on Google...
EDIT: I just found a way to do it, but it's a dirty trick...
[Test, TestCaseSource("PromptCredentials")]
public void TestLogin(string userName, string password)
{
// ...
}
static object[] PromptCredentials
{
get
{
string userName = Interaction.InputBox("Enter user name", "Test parameters", "", -1, -1);
string password = Interaction.InputBox("Enter password", "Test parameters", "", -1, -1);
return new object[]
{
new object[] { userName, password }
};
}
}
I'm still interested in a better solution...
Use the TestCase attribute.
[TestCase("User1", "")]
[TestCase("", "Pass123")]
[TestCase("xxxxxx", "xxxxxx")]
public void TestLogin(string userName, string password)
{
// ...
}
Unit Tests should normally not take any parameters. You create the necessary data within the test itself.
MS Unit tests don't allow the passing of parameters to tests. Instead you need to create Datadriven Unit tests. Try the link, it may help you.
As I mentioned. I wouldn't declare passing arguments to unit tests itself good practice.
Update: I was young :). Consider Sarfraz's answer instead on how to pass parameters to NUnit tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With