Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify test method parameters with TestDriven.NET?

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...

like image 636
Thomas Levesque Avatar asked Sep 05 '09 02:09

Thomas Levesque


2 Answers

Use the TestCase attribute.

[TestCase("User1", "")]
[TestCase("", "Pass123")]
[TestCase("xxxxxx", "xxxxxx")]
public void TestLogin(string userName, string password)
{
    // ...
}
like image 127
Naeem Sarfraz Avatar answered Sep 23 '22 14:09

Naeem Sarfraz


Unit Tests should normally not take any parameters. You create the necessary data within the test itself.

  • The expected value
  • You call your method you want to test passing the necessary arguments
  • You compare the result with the expected value and the returned value from your tested method

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.

like image 32
Juri Avatar answered Sep 23 '22 14:09

Juri