简体   繁体   English

使用 NUnit 或 XUnit 时如何将参数传递给 dotnet 测试命令

[英]How to pass parameters to the dotnet test command while using NUnit or XUnit

I'm developing some end-to-end tests using C# with .NET Core, Selenium and NUnit.我正在使用 C# 和 .NET Core、Selenium 和 NUnit 开发一些端到端测试。 Now i want to write a login testcase.现在我想写一个登录测试用例。 My tests are started from console simply by using the dotnet test command.我的测试是通过使用dotnet test命令从控制台启动的。

I simply want to pass username and password to this command and get them in my tests.我只是想将用户名和密码传递给这个命令并在我的测试中获取它们。 I can not use NUnit-Console since it doesn't support .NET Core at the moment .我不能使用 NUnit-Console,因为它目前不支持 .NET Core

Whats the suggested way to solve this problem?解决此问题的建议方法是什么? I would prefer to not store the settings in a file but to directly input them into the console.我宁愿不将设置存储在文件中,而是直接将它们输入到控制台中。

If you want to avoid a runsettings file, you can use this workaround.如果要避免使用 runsettings 文件,可以使用此解决方法。 One of the recommended ways of passing parameters, is through environment variables.传递参数的推荐方法之一是通过环境变量。 So in your C# nunit (or xunit) file, you can do something like:因此,在您的 C# nunit(或 xunit)文件中,您可以执行以下操作:

// 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一种方法是创建一个简单的 cmd 文件

#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 TestUser=%1
SET TestPassword=%2
SET TestUrl=%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.或者,如果您想从 Azure DevOps(fka VSTS 或 TFS)管道启动测试,您可以简单地使用 $(...) 表示法来内联变量,即使它们被标记为机密和/或来自 Azure KeyVault .

#In Azure DevOps, 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)

运行 Azure DevOps 命令任务

Unfortunately, the only way to pass settings from dotnet test into NUnit is to use a .runsettings file.不幸的是,将设置从dotnet test传递到 NUnit 的唯一方法是使用.runsettings文件。 There's no way for NUnit to create custom command line arguments for the dotnet test tool - although we'd love there to be! NUnit 无法为dotnet test工具创建自定义命令行参数 - 尽管我们很乐意这样做!

Take a look at the sample .runsettings file here .此处查看示例.runsettings文件。 The specific bit you'll need:您需要的特定位:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <!-- Parameters used by tests at runtime -->
  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
    <Parameter name="webAppUserName" value="Admin" />
    <Parameter name="webAppPassword" value="Password" />
  </TestRunParameters>
</RunSettings>

You should just be able to then pass this file into dotnet test with the -s flag.然后您应该能够使用-s标志将此文件传递到dotnet test

dotnet test myProj.csproj -s mySettings.runsettings

This documentation suggests that it should now be possible to pass in arguments on the command line, instead of within a runsettings file.该文档表明现在应该可以在命令行上传递参数,而不是在 runsettings 文件中。

https://github.com/Microsoft/vstest-docs/blob/master/docs/RunSettingsArguments.md https://github.com/Microsoft/vstest-docs/blob/master/docs/RunSettingsArguments.md

dotnet test -- MSTest.MapInconclusiveToFailed=True MSTest.DeploymentEnabled=False

Note the space after -- .注意--后面的空格。

Edit 1编辑 1

What worked for me was a combination of adding a runsettings file, and then overriding the param I wanted to, using this syntax:对我有用的是添加一个 runsettings 文件,然后使用以下语法覆盖我想要的参数的组合:

dotnet test -- TestRunParameters.Parameter(name=\\"myParam\\", value=\\"value\\")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM