Search code examples
c#.netunit-testingtestingnunit

dotnet test with variable string parameter


how can I use variable string parameter in dotnet test (Visual Studio 2022)

[Test]
public void MyTest(string MyString)
{
     Console.WriteLine(MyString);
}

and execute the test by command line with this variable string?

dotnet test "C:\MyPath\Test.dll" --filter "FullyQualifiedName=QualifyTest.ColorTest("This is my String")

Is it also possible to pass path names like "D:\This\is\my\path.txt" in the same way?

Should I use [TestCase] or [TestCaseSource] with included parameter instead and how? I use NUnit 3.0

Thank you.


Solution

  • Finally I useed environment variables to do this task:

    dotnet test "C:\MyPath\Test.dll" --filter "FullyQualifiedName=QualifyTest.ColorTest -e variable1="MyString" -e variable2="C:\a\path\to\file.txt"
    

    and in code I use:

    string var1 = Environment.GetEnvironmentVariable("variable1");
    string path1 = Environment.GetEnvironmentVariable("variable2");
    

    Ths works for me. Maybe there is a better solution.