在 C# 测试装置中从 Nunit3 获取参数 [英] Fetch Params from Nunit3 in C# test fixture

查看:60
本文介绍了在 C# 测试装置中从 Nunit3 获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Nunit3 版本中提供的 params 参数来传递多个参数.

I am using the params argument provided in the Nunit3 version to pass multiple parameters.

但是,我无法使用 C# 测试装置获取它们.我已经搜索过但无法得到正确的结果.

However, I am unable to fetch them with the C# test fixture. I have searched but unable to get a correct result.

谁能告诉我如何在 c# 中获取这些 param 参数.

Can someone provide me with the pointers on how to fetch those param arguments in c#.

任何帮助将不胜感激.提前致谢.

Any help will be appreciated. Thanks in advance.

推荐答案

首先,确保您同时使用 NUnit 控制台 3.4.1 和 NUnit Framework 3.4.1.

First, make sure that you are using both NUnit console 3.4.1 and NUnit Framework 3.4.1.

您的 --params:Code=XXX --params:Date=2011-05-16 命令行选项看起来正确.也可以用分号组合多个参数,--params:Code=XXX;Date=2011-05-16

Your command line option of --params:Code=XXX --params:Date=2011-05-16 looks correct. You can also combine multiple parameters with a semicolon, --params:Code=XXX;Date=2011-05-16

要访问单元测试中的参数,请在测试中使用 TestContext.Parameters.Get("Code").还有一个 string Get(string key, string default) 和一个 T Get(string key, T default) 执行 Convert.ChangeType>.

To access the parameters in your unit tests, use TestContext.Parameters.Get("Code") in your tests. There is also a string Get(string key, string default) and a T Get(string key, T default) which does a Convert.ChangeType.

它还没有很好的文档记录,所以请参阅实现该功能的拉取请求 了解更多信息.

It isn't well documented yet, so see the pull request that implemented the feature for more information.

这是一个示例测试,

[Test]
public void TestCommandLineParameters()
{
    var code = TestContext.Parameters.Get("Code", "<unknown>");
    var date = TestContext.Parameters.Get("Date", DateTime.MinValue);

    TestContext.WriteLine($"Fetched test parameters {code} and {date}");
}

我使用命令行和 NUnit 3.4.1 运行,

Which I run with the command line and NUnit 3.4.1,

nunit3-console.exe --params:Code=XXX --params:Date=2011-05-16 .\nunit-v3.dll

在输出中,我看到

=> nunit.v3.TestParamsTest.TestCommandLineParameters
Fetched test parameters XXX and 2011-05-16 12:00:00 AM

这篇关于在 C# 测试装置中从 Nunit3 获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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