Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass test case parameters using nunit console

I am developing tests using Nunit and data driven testing approach. I have test method with 2 parameters: path to xlsx file and worksheet name. It works perfect in Visual Studio when I pass parameters in TestCase attribute, for example when I want to run 3 test cases have to write something like this:

[TestCase(@"pathToFile.xlsx", "TestCase1")]
[TestCase(@"pathToFile.xlsx", "TestCase2")]
[TestCase(@"pathToFile.xlsx", "TestCase3")]
public void performActionsByWorksheet(string excelFilePath, string worksheetName)
{    
    //test code
}

I would like to run my test cases and pass parameters using Nunit Console (not to write parameters in code).

Is it possible to achieve it?

like image 318
kotoj Avatar asked Sep 29 '16 11:09

kotoj


People also ask

How do I write a unit test case in NUnit?

Creating the first testSave this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created.

How do I use Testcasesource in NUnit?

TestCaseSourceAttribute is used on a parameterized test method to identify the source from which the required arguments will be provided. The attribute additionally identifies the method as a test method. The data is kept separate from the test itself and may be used by multiple test methods.

What is NUnit-console?

nunit-console is a simple but powerful front-end to NUnit, a testing framework for . NET. It will run all or some tests from the assemblies specified as arguments and display the results. Results can be written in either XML or plain text.


1 Answers

If you are using NUnit 3 you can use TestContext.Parameters property:

[Test]
public void performActionsByWorksheet()
{
    string excelFilePath = TestContext.Parameters["excelFilePath"];
    string worksheetName = TestContext.Parameters["worksheetName"];
    TestContext.WriteLine(excelFilePath);
    TestContext.WriteLine(worksheetName);
}

and --params command line argument:

nunit3-console.exe path/to/your/test.dll --params=excelFilePath=testPath;worksheetName=testName
like image 174
Vadim Pashkov Avatar answered Oct 09 '22 16:10

Vadim Pashkov