用Java进行单元测试 - 它是什么? [英] Unit testing in Java - what is it?

查看:161
本文介绍了用Java进行单元测试 - 它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能用几句话来解释:


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

  2. 如何对Java中的简单示例进行单元测试?

  3. 当我们不需要它们/类型的项目时,我们可以进行单元测试吗?

  4. 有用的链接

  1. Why we need it / why they make our life easier ?
  2. How to unit-test [simple example in Java] ?
  3. When do not we need them / types of projects we can leave unit-testing out?
  4. useful links


推荐答案


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

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).

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

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

      查看 JUnit 网站和 JUnit食谱了解详情。编写JUnit测试用例并不多。实际上提出好的测试用例肯定比实际实现更难。

      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?

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

      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

      Links

      JUnit - 单元测试

      EclEmma - 测试覆盖率工具

      EclEmma - test coverage tool

      链接文字 - 维基百科链接到单元测试

      link text - Wikipedia link to unit testing

      这篇关于用Java进行单元测试 - 它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆