Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run parametrized test from console

I'd like to run test method with only one set of params from a testcase.

I'm using NUnit Console 3.4.1.

Example of code:

[Category("SmokeTests")]
[TestCase("1 param", "2 param", "3 param")]
[TestCase("aaa", "bbb", "ccc")]
public void TestMethod(string a, string b, string c)
{
    // do something
}

Command-line to be run:

nunit3-console.exe UiTests.dll --where "cat==SmokeTests and name==TestMethod(\"aaa\", \"bbb\", \"ccc\")" --result C:\temp\result.xml

Currently NUnit returns an error

Unexpected token '(' at position 50 in selection expression.

like image 230
Yuriy Rozhkov Avatar asked Nov 08 '22 19:11

Yuriy Rozhkov


1 Answers

You were very close to the answer.

Your query was

--where "cat==SmokeTests and name==TestMethod(\"aaa\", \"bbb\", \"ccc\")"

There are three things wrong with the query:

  1. You were right to think that the quotes need to be escaped with backslashes (\"), but due to the way the arguments are interpreted, the backslashes themselves need to be escaped too (\\\").

  2. To get around the

    Unexpected token '(' at position 50 in selection expression.

    you have to also wrap the name parameter in quotes (these quotes only need to be escaped once).

  3. When NUnit constructs its name for a method, it comma-separates the arguments, but without spaces. The name of the test you're trying to run in your example is

    TestMethod("aaa","bbb","ccc")
    

    not

    TestMethod("aaa", "bbb", "ccc")
    

The query that should work, then, is:

--where "cat==SmokeTests and name==\"TestMethod(\\\"aaa\\\",\\\"bbb\\\",\\\"ccc\\\")\""

The arguments passed into nunit3-console.exe are then:

  • --where
  • cat==SmokeTests and name=="TestMethod(\"aaa\",\"bbb\",\"ccc\")"

then NUnit can parse the name arugment, handling the escaped quotes, and running the test you desire.

Reference: Test Selection Language in the NUnit documentation.

like image 117
Wai Ha Lee Avatar answered Nov 15 '22 12:11

Wai Ha Lee