一本献给 NUnit 专家:升级到 2.x 并通过现有测试工具使用新功能

发布于 2024-09-11 01:40:59 字数 1186 浏览 17 评论 0原文

我最近升级到 NUnit 2.5(是的,我知道),我想知道我是否可以用这个来听取别人的意见。

我有很多类似这样的测试:

[Test]
public void TestTheSpecialDict() {

    int start = 0;
    int end = 44;

    Dictionary<int, string> = SomeFunction(start, end);
    Assert.That(dict1.Count > 0);

    // Alright now, some funky values
    start = -1;
    end = -34;
    Dictionary<int, string> dict2 = SomeFunction(start, end);

    Assert.That(dict2.Count == 0);

}

因此,这个特定的测试可确保当范围无效时 SomeFunction 返回一个空字典,而不是 null。

现在我在 2.x 中发现了 [TestCase(...)] 属性。我的天啊!所以我希望我的测试看起来像这样:

[TestCase(0, 44)]
[TestCase(-1, -34)]
public void TestTheSpecialDict(int start, int end) {
    /* ... */
}

太棒了。当然,这里的问题是第二个断言在第一个测试用例中失败,第一个断言在第二个测试用例中失败。这显然是预期的行为,因为 startend 具有方法范围并适用于这两个操作。毫不奇怪。

解决这个问题不是问题:

  • 将测试用例分解为两种不同的方法。重复代码,实际上,TestCase 在这一点上有点多余。
  • 添加额外的测试用例参数(如顺序值)并根据该参数调节 Assert 调用。考虑到我要做的测试数量,还有很多工作要做。
  • 还有一些我还没想到的事情。

有没有“更干净”的方法?我查看了 NUnit.Framework,但我想我看不到任何可以让我流利完成此操作的内容。所以我想知道是否有人拥有 NUnit 1.x 的这种“遗留”UT 结构,以及他们如何在利用新功能的同时进行迁移。

I've recently upgraded to NUnit 2.5 (yeah I know) and I was wondering if I could pick someone's brain with this.

I have lots of tests that look like this:

[Test]
public void TestTheSpecialDict() {

    int start = 0;
    int end = 44;

    Dictionary<int, string> = SomeFunction(start, end);
    Assert.That(dict1.Count > 0);

    // Alright now, some funky values
    start = -1;
    end = -34;
    Dictionary<int, string> dict2 = SomeFunction(start, end);

    Assert.That(dict2.Count == 0);

}

So this specific test ensures that SomeFunction returns an empty dictionary when the range is invalid, rather than null, for example.

Now I've discovered the [TestCase(...)] attribute in 2.x. OMG! So I want my test to look like this:

[TestCase(0, 44)]
[TestCase(-1, -34)]
public void TestTheSpecialDict(int start, int end) {
    /* ... */
}

Awesome. The problem here of course is that the second Assert fails in the first testcase, and the first assert fails in the second. This is obviously the expected behavior since start and end have method scope and apply to both operations. No surprise.

Fixing this isn't a problem:

  • Break out the test cases to two different methods. Duplication of code and really, TestCase would be kind of superfluous at this point.
  • Add an additional test case parameter (like a sequential value) and conditioning the Assert calls based on that. Lots of work considering the amount of tests I have.
  • Something else I haven't though of.

Is there a "cleaner" way? I've looked through NUnit.Framework and I can't see anything that would let me do this fluently I guess. So I'm wondering if there's someone out there who had this type of "legacy" UT structure with NUnit 1.x and how they migrated up while taking advantage of the new features.

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

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

发布评论

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

评论(2

太傻旳人生 2024-09-18 01:40:59

我个人认为这些 TestCase 属性对于测试接受一些参数并返回一些值的函数很有用。因此,在属性中,您只需给它参数和期望值,它就可以正常工作。在你的情况下,你可以有你的字典的确切计数或一个布尔值(如果它是正数)。我不相信它是否会增加你的案例的可读性......

I personally think these TestCase attributes are useful for testing functions that take some parameters and return some value. So in the attributes you can just give it arguments and expected value and it works fine. In your case you could have there either the exact count for your dictionary or a boolean value if its positive. I am not convinced if it would increase the readability in your case though....

慢慢从新开始 2024-09-18 01:40:59

请原谅我没有使用 Visual Studio 来测试这个。但也许使用 Assert.That 并传入一个评估参数。像这样的事情:

[TestCase(0, 44, Is.GreaterThan(0))]
[TestCase(-1, -34, Is.Empty]
public void TestTheSpecialDict(int start, int end, Func<Dictionary<int, string>, bool> condition) {
    /* ... */

    Assert.That(dict.Count, condition);

}

再次对它不准确表示歉意,特别是参数类型可能只是一个猜测,但基本思想应该成立。

Forgive me a bit for not having Visual Studio to test this. but perhaps using Assert.That and passing in an evaluation parameter. Something like this:

[TestCase(0, 44, Is.GreaterThan(0))]
[TestCase(-1, -34, Is.Empty]
public void TestTheSpecialDict(int start, int end, Func<Dictionary<int, string>, bool> condition) {
    /* ... */

    Assert.That(dict.Count, condition);

}

Again apologize about it not being exact, especially that parameter type might be of is just a guess at it, but the basic idea should hold.

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