单元测试要测试什么,是方法还是场景?

发布于 2024-07-18 01:51:21 字数 202 浏览 8 评论 0原文

单元测试要测试什么,是方法还是场景?

如果测试每种方法,则需要最少的测试用例设置。

如果测试调用其他方法的方法,则测试用例所需的设置会很大。 如果各个方法的单元测试已经存在,那么为什么要为使用它们的方法编写单元测试呢?

但它也有一些需要测试的功能。 代码覆盖率工具也会抱怨覆盖率。

请提供您的实际意见。

What to test in unit testing, a method or a scenario?

If test each method then minimal test case setup is required.

If test a method which calls other methods then setup required for the test case is huge. If unit-tests for the individual methods are already there then why to write for this method which is using them?

But then it also have little bit of functionality which should be tested. Also the code coverge tool will complain about coverage percentage.

Please provide your practical inputs.

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

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

发布评论

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

评论(3

漫雪独思 2024-07-25 01:51:21

如果测试一个调用其他方法的方法
方法然后设置所需的
测试用例很大。 如果单元测试
各个方法已经
那为什么要写这个
使用它们的方法?

因为该方法可能会使用错误的参数或以错误的顺序调用底层方法,或者对返回值执行错误的操作。

根据我的经验,与独立方法和此类“粘合代码”之间的交互相比,独立方法中出现错误的可能性通常相当小。

“经典”单元测试,即完全独立地测试每个类的行为,是一个很好的概念,但实际上,这种情况并不那么常见或有用。 这取决于设计代码的模块化程度和(隐含的)可测试性的程度,但永远不可能实现所有目标。

If test a method which calls other
methods then setup required for the
test case is huge. If unit-tests for
the individual methods are already
there then why to write for this
method which is using them?

Because that method may call the underlying methods with the wrong parameter, or in a wrong sequence, or do the wrong thing with the return values.

In my experience, the potential for errors in self-contained methods is usually rather small compared to the interaction between them and such "glue code".

"Classic" unit tests, where you completely test each class's behaviour in isolation are a nice concept, but in reality, this scenario is not all that common or useful. It depends on how much care was taken to design the code for modularity and (implied in that) testability, but it's never possible to achieve for everything.

玻璃人 2024-07-25 01:51:21

您可以对一种方法进行单元测试,每个可能的执行路径使用一个测试用例。

单元测试的典型结构是 Arrange-Act-assert(三个 A):

  • Arrange - 为要测试的案例创建环境(这可以在 Suite 设置或 TestCase 中完成,或两者都完成)
  • Act - 测试用例测试用例调用被测方法
  • 断言 - 验证被测方法做了它应该做的事情

过多的设置应该表明你应该检查你的设计,例如,你的类可能太大,或者做了太多事物。 过度设置是一种反模式。

您可以使用模拟对象来隔离您正在测试的类。

You unit-test a method, with one test case per possible execution path.

The typical structure of an unit test is Arrange-Act-assert (triple A's):

  • Arrange - create the environment for the case you want to test (this can be done in the Suite setup or in the TestCase, or both)
  • Act - the test case invokes the method under test
  • Assert - to verify the method under test did what it is supposed to do

Excessive setup should be an indication that you should look at your design, for instance, your class could be too big, or doing too many things. Excessive setup is an anti-pattern.

You can use mock objects to isolate the class you're testing.

冷夜 2024-07-25 01:51:21

单元测试的第一条规则是:

一次只测试一件事!

这意味着:您甚至不测试方法,而是测试方法的一个方面单一条件下的方法。 因此,您对同一方法提出了多个测试。

隔离是良好单元测试的关键。 您需要模拟您的依赖项(例如使用 RhinoMocks)。 一开始看起来比较复杂,但从长远来看,管理和维护起来要容易得多。 在测试中只测试少量代码,使您的测试尽可能简单,并且您将拥有可维护的测试。

The number-one rule of unit-testing is:

Only test one thing at once!

This means: you don't even test a method, you test a single aspect of a method under a single condition. So you come up with several tests for the same method.

Isolation is a key for good unit tests. You need to mock your dependencies (eg. using RhinoMocks). At the beginning it looks more complex, but in the long run it is much easier to manage and maintain. Test only a small amount of code in a test, make you test as trivial as possible and you will have maintainable tests.

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