【发布时间】:2017-03-24 05:38:00
【问题描述】:
我在 Visual Studio 2017 的 .NET Core 1.1 单元测试项目(不是 xUnit 测试项目)中有以下测试类。如何将命令行参数传递给 TestMethod?
[TestClass]
public class TestClass
{
[TestMethod]
public void TestMethod()
{
var args = Environment.GetCommandLineArgs();
var json = JsonConvert.SerializeObject(args);
throw new Exception(json);
}
}
互联网上的很多地方听起来好像您可以在要传递的参数前面加上 --,但我无法让它工作。
这些是我尝试过的命令:
dotnet test TestProject.csproj -- hello=worlddotnet test TestProject.csproj -- --hello worlddotnet test TestProject.csproj -- -hello world
但是他们每次都输出这个消息。注意hello 和world 都不存在:
["C:\Users\____\.nuget\packages\microsoft.testplatform.testhost\15.0.0\lib\netstandard1.5\testhost.dll","--port","55032","- -parentprocessid","24440"]
第一个字符串只是正在运行的程序集的名称——pretty standard for the first command line argument。我不知道--port 或--parentprocessid 参数来自哪里。
此外,这些变化使dotnet test 与One or more runsettings provided contain invalid token 窒息(原文如此):
dotnet test TestProject.csproj -- -hello=worlddotnet test TestProject.csproj -- --hello=world
编辑:它看起来像 GetCommandLineArgs() isn't that great of an option,即使它确实在这里工作。
另外,the answer to this question on social.msdn.microsoft.com from 2006 说:
实例化这些类以便 VS 对它们进行单元测试的方式与正常执行完全不同。可执行文件无法正常运行,您和 VS 都无法为其提供参数。通过将二进制文件用作类库,这些类的实例化与程序执行无关。 (原文如此)
我想知道这是否仍然适用于dotnet test?
在其他新闻中,this guy on SO 怀疑命令行参数是否可以传递给 DLL,但he's wrong:
[
函数的原型:] void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow); [Rundll] 调用
函数,传递命令行 tail,即 。
【问题讨论】:
-
暗示它闻起来像
dotnet test不适合运行集成测试,当您需要将机密/凭据保留在代码之外时,这是我试图让它做的事情
标签: c# automated-tests integration-testing .net-core