将参数传递给 dotnet 测试项目? [英] Passing arguments to dotnet test project?

查看:27
本文介绍了将参数传递给 dotnet 测试项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2017 的 .NET Core 1.1 单元测试项目(不是 xUnit 测试项目)中有下面的测试类.如何将命令行参数传递给 TestMethod?

[测试类]公共类测试类{[测试方法]公共无效测试方法(){var args = Environment.GetCommandLineArgs();var json = JsonConvert.SerializeObject(args);抛出新的异常(json);}}

互联网上的很多地方听起来好像您可以在要传入的参数前放置一个 -- ,但我无法让它工作.>

这些是我尝试过的命令:

  • dotnet test TestProject.csproj -- hello=world
  • dotnet test TestProject.csproj -- --hello world
  • dotnet test TestProject.csproj -- -hello world

但是他们每次都输出这个消息.注意 helloworld 都不存在:

<块引用>

[C:\Users\____\.nuget\packages\microsoft.testplatform.testhost\15.0.0\lib\netstandard1.5\testhost.dll"、--port"、55032","--parentprocessid","24440"]

第一个字符串只是正在运行的程序集的名称 --

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>.

解决方案

This is one of the places I encountered while searching for this answer, so it's only fair I share my workaround here.

At this moment, there is no way to pass parameters to any .net test project. You could use the mstest testsettings/runsettings file, but that requires creating seperate files. In my case, I needed a servername and a password for my Selenium Coded UI tests, and I did not want to store them.

One of the recommended ways of passing parameters, is through environment variables. So in your C# xunit file, you can do something like:

// in mytest.cs
var user = Environment.GetEnvironmentVariable("TestUser");
var password = Environment.GetEnvironmentVariable("TestPassword");
var url = Environment.GetEnvironmentVariable("TestUrl");

If you do not want to definitively set your environment variables, remember you can always set them temporarily for just your session process. One way of doing this, is by creating a simple cmd file

#launchtests.cmd
SETLOCAL
SET TestUser='pete001'
SET TestPassword='secret'
SET TestUrl='http://testserver.local/login'
DOTNET TEST mytest.csproj

And now the fun part. You can parameterize every aspect of this. So you can change it to:

#run wity launchtests.cmd pete001 secret 'http://testserver.local/login'
SETLOCAL
SET %1
SET %2
SET %3
DOTNET TEST mytest.csproj

Or if you want to launch the test from an Azure DevOps (fka VSTS or TFS) pipeline, you can simply use the $(...) notation to inline variables, even if they're marked secret and/or come from Azure KeyVault.

#In AzureDevops, variables not marked as secret are already added to the environment
SET TestPassword=$(TestPassword)
dotnet test $(Build.SourcesDirectory)\MyCompany.MyProduct.UITests\MyTest.csproj --configuration $(BuildConfiguration) --collect "Code Coverage" --logger trx --results-directory $(Agent.TempDirectory)

这篇关于将参数传递给 dotnet 测试项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆