NUnit TestCaseSource 将值传递给工厂

发布于 2024-08-22 04:50:40 字数 1273 浏览 3 评论 0原文

我正在使用 NUnit 2.5.3 TestCaseSource 属性并创建一个工厂来生成我的测试。像这样的事情:

[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
    Item obj = new Item();
    obj.Description = text;
}

我的来源是这样的:

public static IEnumerable<TestCaseData> VariableString
{
    get
    {
        yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Empty_Text");
        yield return new TestCaseData(null).Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Null_Text");
        yield return new TestCaseData("  ").Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Whitespace_Text");
    }
}

我需要做的是向变量字符串添加最大长度检查,但这个最大长度是在被测类的合同中定义的。在我们的例子中,它是一个简单的公共结构:

   public struct ItemLengths
    {
        public const int Description = 255;
    }

我找不到任何将值传递给测试用例生成器的方法。我尝试过静态共享值,但这些值没有被拾取。我不想将内容保存到文件中,因为每次代码更改时我都需要重新生成该文件。

我想将以下行添加到我的测试用例中:

yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
    .Throws(typeof(PreconditionException));

概念上相当简单,但我发现无法做到。有什么建议吗?

I'm using the NUnit 2.5.3 TestCaseSource attribute and creating a factory to generate my tests. Something like this:

[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
    Item obj = new Item();
    obj.Description = text;
}

My source is this:

public static IEnumerable<TestCaseData> VariableString
{
    get
    {
        yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Empty_Text");
        yield return new TestCaseData(null).Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Null_Text");
        yield return new TestCaseData("  ").Throws(typeof(PreconditionException))
            .SetName("Does_Reject_Whitespace_Text");
    }
}

What I need to be able to do is to add a maximum length check to the Variable String, but this maximum length is defined in the contracts in the class under test. In our case its a simple public struct:

   public struct ItemLengths
    {
        public const int Description = 255;
    }

I can't find any way of passing a value to the test case generator. I've tried static shared values and these are not picked up. I don't want to save stuff to a file, as then I'd need to regenerate this file every time the code changed.

I want to add the following line to my testcase:

yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
    .Throws(typeof(PreconditionException));

Something fairly simple in concept, but something I'm finding impossible to do. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-08-29 04:50:40

将测试的参数更改为类而不是字符串。像这样:

public class StringTest {
公共字符串测试字符串;
公共 int 最大长度;
然后

构造此类作为参数传递给 TestCaseData 构造函数。这样您就可以传递字符串和您喜欢的任何其他参数。

另一种选择是让测试有两个参数:字符串和整数。

然后是 TestCaseData(“mystring”, 255)。您是否意识到他们可以有多个论点?

韦恩

Change the parameter of your test as class instead of a string. Like so:

public class StringTest {
public string testString;
public int maxLength;
}

Then construct this class to pass as an argument to TestCaseData constructor. That way you can pass the string and any other arguments you like.

Another option is to make the test have 2 arguments of string and int.

Then for the TestCaseData( "mystring", 255). Did you realize they can have multiple arguments?

Wayne

紫罗兰の梦幻 2024-08-29 04:50:40

我遇到了与您类似的问题,最终编写了一个小型 NUnit 插件和一个扩展 NUnit TestCaseSourceAttribute 的自定义属性。在我的特定情况下,我对将参数传递给工厂方法不感兴趣,但您可以轻松地使用相同的技术来实现您想要的。

这并不难,只需要我写三个小班之类的东西。您可以在以下位置阅读有关我的解决方案的更多信息: 使用 nunit 进行黑盒测试使用自定义测试用例源

附言。为了使用这种技术,你必须使用 NUnit 2.5(至少)祝你好运。

I faced a similar problem like yours and ended up writing a small NUnit addin and a custom attribute that extends the NUnit TestCaseSourceAttribute. In my particular case I wasn't interested in passing parameters to the factory method but you could easily use the same technique to achieve what you want.

It wasn't all that hard and only required me to write something like three small classes. You can read more about my solution at: blackbox testing with nunit using a custom testcasesource.

PS. In order to use this technique you have to use NUnit 2.5 (at least) Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文