I have the test class below in a .NET Core 1.1 Unit Test project (not an xUnit Test project) in Visual Studio 2017. How do I pass command line arguments to TestMethod
?
[TestClass]
public class TestClass
{
[TestMethod]
public void TestMethod()
{
var args = Environment.GetCommandLineArgs();
var json = JsonConvert.SerializeObject(args);
throw new Exception(json);
}
}
Lots of places on the internet make it sound like you can just put a --
in front of the arguments you want to pass in, but I can't get it to work.
These are the commands I've tried:
dotnet test TestProject.csproj -- hello=world
dotnet test TestProject.csproj -- --hello world
dotnet test TestProject.csproj -- -hello world
But all of them output this message every time. Note how neither hello
nor world
are present:
["C:\Users\____\.nuget\packages\microsoft.testplatform.testhost\15.0.0\lib\netstandard1.5\testhost.dll","--port","55032","--parentprocessid","24440"]
The first string is just the name of the running assembly -- pretty standard for the first command line argument. I don't know where the --port
or --parentprocessid
arguments are coming from.
Also, these variations make dotnet test
choke with One or more runsettings provided contain invalid token
(sic):
dotnet test TestProject.csproj -- -hello=world
dotnet test TestProject.csproj -- --hello=world
Edit: It looks like GetCommandLineArgs()
isn't that great of an option, even if it did work here.
Also, the answer to this question on social.msdn.microsoft.com from 2006 says:
The way those classes are instantiated so VS can do unit testing against them is entirely different than normal execution. The executable is not run like normal, and neither you nor VS can provide arguments to it. The classes are instantiated indepdently of program execution by using the binary as a class library. (sic)
I wonder if this still applies to dotnet test
?
In other news, this guy on SO was doubtful that command line arguments could be passed to DLLs at all, but he's wrong:
[Prototype for the <entrypoint> function:] void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
[Rundll] calls the <entrypoint> function, passing the command line tail which is the <optional arguments>.