使用 NUnit 或 XUnit 时如何将参数传递给 dotnet 测试命令 [英] How to pass parameters to the dotnet test command while using NUnit or XUnit

查看:55
本文介绍了使用 NUnit 或 XUnit 时如何将参数传递给 dotnet 测试命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 和 .NET Core、Selenium 和 NUnit 开发一些端到端测试.现在我想写一个登录测试用例.我的测试是通过使用 dotnet test 命令从控制台启动的.

I'm developing some end-to-end tests using C# with .NET Core, Selenium and NUnit. Now i want to write a login testcase. My tests are started from console simply by using the dotnet test command.

我只想将用户名和密码传递给此命令并在我的测试中获取它们.我不能使用 NUnit-Console,因为它目前不支持 .NET Core.

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.

解决此问题的建议方法是什么?我宁愿不将设置存储在文件中,而是直接将它们输入到控制台中.

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.

推荐答案

如果您想避免使用 runsettings 文件,可以使用此解决方法.传递参数的推荐方法之一是通过环境变量.因此,在您的 C# nunit(或 xunit)文件中,您可以执行以下操作:

If you want to avoid a runsettings file, you can use this workaround. 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:

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

如果您不想明确设置您的环境变量,请记住您始终可以仅为您的会话进程临时设置它们.一种方法是创建一个简单的 cmd 文件

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 TestUser=%1
SET TestPassword=%2
SET TestUrl=%3
DOTNET TEST mytest.csproj

或者,如果您想从 Azure DevOps(fka VSTS 或 TFS)管道启动测试,您可以简单地使用 $(...) 表示法来内联变量,即使它们被标记为机密和/或来来自 Azure KeyVault.

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

这篇关于使用 NUnit 或 XUnit 时如何将参数传递给 dotnet 测试命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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