NUnit 测试 - 如何在您的应用程序中使用它

发布于 2024-08-17 07:45:56 字数 86 浏览 2 评论 0原文

谁能告诉我.NET 中的 NUnit 是什么以及如何使用它?我听说使用这个工具可以改进应用程序的设计。这是真的吗?我在哪里可以获得有关该主题的绝对初学者教程?

Can anyone tell me what is NUnit in .NET and how to use it? I have heard that using this tools improves design of an application. Is it true? Where can I get an absolute beginners tutorial on this topic?

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

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

发布评论

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

评论(3

红墙和绿瓦 2024-08-24 07:45:56

NUnit 是一个单元测试框架 - 它允许您编写单独的测试套件来测试框架的组件,而不是直接在应用程序中使用它。

单元测试是一种测试方法,您可以通过它来测试应用程序的各个小单元,而不是作为一个整体进行测试。

这有助于改进应用程序设计的原因是,它鼓励您以小单元编写应用程序,每个单元都与其他单元解耦,因此可以进行测试。这强化了“关注点分离”的概念;确保每种类型只负责一项任务。

此外,许多人使用“单元测试优先”的理念进行开发,即在编写实际代码之前为某些代码编写测试。这里的想法是以下顺序:

  • 编写测试。这是一个失败的
    (红色)测试,因为没有代码
    测试。
  • 编写代码以便测试
    有效(绿色)。
  • 现在您可以进行测试来验证您的
    代码,您可以轻松地重写和
    高水平地改进您的代码
    确信你不是
    打破它。 (重构)

这里人们会提到这种红-绿-重构方法。

网上有很多关于如何进行单元测试的信息。只是 Goole“C# 单元测试”。

不过,请查看 NUnit 网站 上的文档(由于某种原因,我在写这篇文章时已将其关闭),然后查看其他与单元测试相关的 SO 帖子。

另外,这里是一篇相当广泛的代码项目文章,尽管我还没有深挖了一下,不知道有没有用。

也许其他有好的参考资料的人可以将它们添加为本文的评论。

NUnit is a unit testing framework - rather than using it directly in your application, it allows you to write seperate suites of tests with which to test the components of your framework.

Unit testing is a test approach whereby you are testing small individual units of your application, rather than testing the whole thing as one.

The reason this helps improve the design of an application is that it encourages you to write your application in small units, each of which is decoupled from the others and so can be tested. This enforces the notion of "seperation of concerns"; making sure that each type is responsible for one task only.

Furthermore, lots of people approach development using a "unit test first" philosphy, whereby you write your test for some code before you write the actual code. The idea here is the following sequence:

  • Write your test. This is a failing
    (RED) test as there is no code to
    test.
  • Write your code so that the test
    works (GREEN).
  • Now you have a test to verify your
    code, you can easily re-write and
    improve your code, with a high level
    of confidence that you are not
    breaking it. (REFACTOR)

You will here people refer to this RED-GREEN-REFACTOR approach

There is lots of information on how to approach unit testing on the web. Just Goole "unit testing C#".

However, have a look at the documentation on NUnit's site (down as I write this for some reason), and look at other SO posts related to unit testing.

Also, here is a pretty extensive code project article, although I haven't dug deep into it so I don't know if it is any good.

Maybe others with good references can add them as comments to this article.

心病无药医 2024-08-24 07:45:56

NUnit 是一个用于进行单元测试

如果您不熟悉单元测试,“测试被感染”可能是给你一个很好的介绍。这是关于 jUnit (Java) 而不是 NUnit,但我认为这对您来说不应该是一个太大的问题。

有很多关于单元测试的书籍 - Roy Osherove 的“单元测试的艺术” 强烈推荐。

NUnit is a framework for conducting unit testing.

If you're not familiar with unit testing, "Test Infected" may be a good introduction for you. It's about jUnit (Java) rather than NUnit, but I don't think that should be too much of a problem for you.

There are lots of books on unit testing - Roy Osherove's "The art of unit testing" comes pretty highly recommended.

另类 2024-08-24 07:45:56

您要问的是单元测试。通俗地说,您不是通过运行应用程序来测试应用程序,而是编写小块测试应用程序的代码。测试的一个例子是这样的:

[Test]
public void Get_All_People_Returns_Correct_Number_Of_People()
{
    // setup
    database.Add(new Person());
    database.Add(new Person());
    database.Add(new Person());

    // test
    Assert.That(database.GetAllPeople().Count(), Is.EqualTo(3));
}

您绝对应该阅读更多关于一般单元测试的内容,分离关注,以及在开始使用 NUnit 之前良好的应用程序设计。仅当您的应用程序设计为小型、可测试且互​​不干扰的块时,单元测试才真正有效。

What you're asking about is unit testing. In Layman's terms, instead of testing your application by running it, you instead write code that tests your application in small chunks. An example of a test would be something like this:

[Test]
public void Get_All_People_Returns_Correct_Number_Of_People()
{
    // setup
    database.Add(new Person());
    database.Add(new Person());
    database.Add(new Person());

    // test
    Assert.That(database.GetAllPeople().Count(), Is.EqualTo(3));
}

You should definitely read more about unit testing in general, separation of concerns, and good application design before you get started with NUnit. Unit testing is only really effective when your application is designed in small, testable blocks that don't interfere with each other.

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