【问题标题】:is there a way to write a generic unit test for multiple null arg validations for a single method?有没有办法为单个方法的多个 null arg 验证编写通用单元测试?
【发布时间】:2018-12-18 17:01:44
【问题描述】:

我有一个带有 4 个字符串参数的方法。这些参数中的每一个都存在一个空参数验证保护。有没有办法编写一个 single xUnit null 验证单元测试来通用测试所有 4 个参数?

【问题讨论】:

标签: unit-testing nunit xunit xunit.net


【解决方案1】:

坦率地说,这似乎不是一件很有用的事情。 :-) 但是...

在 NUnit 中,您可以执行以下操作

[TestCase(null, "b", "c", "d")]
[TestCase("a", null, "c", "d")]
[TestCase("a", "b", null, "d")]
[TestCase("a", "b", "c", null)]
public void NullArgDetected(string a, string b, string c, string d)
{
    // call method and assert that it throws
}

当然,您必须更改参数的类型以匹配您正在调用的方法。如果 C# 不允许该类型作为 Attribute 构造函数参数,则需要使用 [TestCaseSource] 而不是 [TestCase]。

就个人而言,如果我有 10 个参数,我可能会这样做。对于四个,我只需编写四个单独的测试方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多