Java 中的单元测试 - 它是什么?

发布于 2024-08-23 11:52:24 字数 1436 浏览 4 评论 0原文

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

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

发布评论

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

评论(7

┾廆蒐ゝ 2024-08-30 11:52:24

为什么我们需要它/为什么它们让我们的生活更轻松?

  • 它允许您检查正在测试的代码片段的预期行为,作为它必须满足的契约。
  • 它还允许您安全地重构代码,而不会破坏其功能(契约)。
  • 它允许您通过在更正错误后实施单元测试来确保错误修复保持固定。
  • 它可以作为编写解耦代码的一种方式(如果您在编写代码时考虑到测试)。

如何进行单元测试[Java 中的简单示例]?

查看 JUnit 网站和 JUnit 食谱 了解详细信息。编写 JUnit 测试用例没有太多内容。实际上提出好的测试用例肯定比实际实施更困难。

我们什么时候不需要它们/我们可以将单元测试排除在外的项目类型?

不要尝试测试类中的每个方法,而应专注于测试类的功能。例如,Bean,您不会为 getter 和 setter 编写测试......

链接

JUnit - 单元测试

EclEmma - 测试覆盖率工具

链接文本 - 单元测试的维基百科链接

Why we need it / why they make our life easier ?

  • It allows you to check the expected behavior of the piece(s) of code you are testing, serving as a contract that it must satisfy.
  • It also allows you to safely re-factor code without breaking the functionality (contract) of it.
  • It allows you to make sure that bug fixes stay fixed by implementing a Unit test after correcting a bug.
  • It may serve as as a way to write decoupled code (if you have testing in mind while writing your code).

How to unit-test [simple example in Java] ?

Check out the JUnit website and the JUnit cookbook for details. There isn't much to writing JUnit test cases. Actually coming up with good test cases is surely harder than the actual implementation.

When do not we need them / types of projects we can leave unit-testing out?

Don't try to test every method in a class, but rather focus on testing the functionality of a class. Beans for example, you won't write tests for the getters and setters...

Links

JUnit - Unit testing

EclEmma - test coverage tool

link text - Wikipedia link to unit testing

悲喜皆因你 2024-08-30 11:52:24
  1. 因为这是应用程序确实按预期运行的证明。您还会更容易地发现回归错误。测试变得更加容易,因为您不必手动检查每个可能的应用程序状态。最后,即使您手动测试了代码,您也很可能会发现您甚至不知道存在的错误。

  2. Google for junit

  3. 应始终编写单元测试,如上所述,它是应用程序按预期工作的证明。有些东西不能或可能很难测试,例如图形用户界面。这并不意味着不应测试 GUI,而只是意味着您应该使用其他工具。

  4. 参见第 2 点。

  1. Because that is your proof that the application actually works as intended. You'll also find regression bugs much easier. Testing becomes easier, as you don't have to manually go through every possible application state. And finally, you'll most likely find bugs you didn't even know existed even though you manually tested your code.

  2. Google for junit

  3. Unit tests should always be written, as said, it is your proof that the application works as intended. Some things cannot or can be hard to test, for example a graphical user interface. This doesn't mean that the GUI shouldn't be tested, it only means you should use other tools for it.

  4. See point 2.

夏花。依旧 2024-08-30 11:52:24

阅读有关 单元测试 的维基百科文章可能会有所帮助,因为这将回答您有关的大多数问题为什么JUnit 网站提供了用于编写 Java 单元测试的资源,其中 Junit Cookbook 可能应该是您的第一站。

就我个人而言,我编写单元测试来测试方法的契约,即特定函数的文档。这样,您将进入增加测试覆盖率和改进文档的循环。但是,您应该尽量避免测试:

  • 其他人的代码,包括 JDK
  • 非确定性代码,例如 java.util.Random

JUnit 不是唯一可用于 Java 的单元测试框架,因此您应该评估其他框架,例如 TestNG 在深入研究它之前。

除了“顶级”框架之外,您还会发现不少涵盖特定领域的项目,例如:

It's probably work reading the Wikipedia article on Unit Testing, as this will answer most of your questions regarding why. The JUnit web site has resources for writing a Java unit test, of which the Junit Cookbook should probably be your first stop.

Personally, I write unit tests to test the contract of a method, i.e. the documentation for a particular function. This way you will enter into a cycle of increasing your test coverage and improving documentation. However, you should try to avoid testing:

  • Other people's code, including the JDK
  • Non-deterministic code, such as java.util.Random

JUnit is not the only unit testing framework available for Java, so you should evaluate other frameworks like TestNG before diving into it.

In addition to "top-level" frameworks, you will also find quite a few projects covering specific areas, such as:

樱花落人离去 2024-08-30 11:52:24

我要写的内容已经在这里的许多回复中涵盖了,但我想我应该添加这个...

我读过的关于何时使用/不使用单元测试的最好的文章是 史蒂夫·桑德森的博客。这是一篇优秀的文章,涵盖了对代码库的不同部分进行单元测试的成本/收益(即反对 100% 覆盖率的令人信服的论点)

What I would have written is already covered in many of the responses here but I thought I'd add this...

The best article I ever read on when to use/not to use unit tests was on Steve Sanderson's blog. That is an excellent article covering the cost/benefit of unit testing on different parts of your code-base (i.e. a compelling argument against 100% coverage)

不如归去 2024-08-30 11:52:24

关于 Java 单元测试的方式和原因的最佳书籍之一是 使用 JUnit 在 Java 中进行实用单元测试(Andy Hunt 和 Dave Thomas)

One of the best books on the hows and whys of unit testing in Java is Pragmatic Unit Testing in Java with JUnit (Andy Hunt & Dave Thomas)

昔梦 2024-08-30 11:52:24

这就是你的编程应该是这样的:

  • 决定一个接口(不一定是java接口,但方法对每个人来说都是这样的)
  • 编写测试
  • 代码实现

This is how how your programming should be :

  • Decide on an interface (not necessarily a java interface, but how method looks like to everybody
  • Write a test
  • Code the implementation
戈亓 2024-08-30 11:52:24

正如其名称所示,它是对我们程序中的单元的测试。例如,如果您有一个返回两个整数之和的方法,您将使用两个数字来测试它,看看它是否正常工作。例如,对于2+2,它返回4。

我们需要这些测试,因为在大项目中,有很多程序员。这些程序员协调工作非常重要。单元测试就像我们程序员编写的程序的正确性证明。因此,程序员编写他的程序并进行单元测试,以表明他的程序正常工作。然后,他将更改提交给项目。这些测试可以帮助您在发生之前防止很多错误。

我有一个逐步过程作为示例,在 Eclipse 中编写 Java 单元测试。请查看“如何编写 Unite 测试”。

我希望它有帮助。

As it's name shows, it is test for our units in our program. For example, if you have a method that returns the summation of two integers, you will test it with two numbers to see if it works correctly. For example, for 2+2, it returns 4.

We need these tests, since in big projects, there are a lot of programmers. It is important that these programmers work in harmony. unit tests act like the proof of correctness for the procedures that our programmers writes. so, a programmer writes his procedure plus it's unit test in order to show that his procedure works correctly. Then, he commits his changes to the project. These tests help you to prevent a lot of bugs before occurrence.

I have a step by step procedure as an example, to write down a Java unit test in Eclipse. Please look "How To Write Unite Tests".

I hope it helps.

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