1

我知道代码覆盖率是一个指标,但在有关单元测试的文章中经常提到它。但是在设计单元测试时,我正在尝试为我的业务逻辑编写测试并且不太关心覆盖率。那是什么关系呢?

4

3 回答 3

1

The thinking goes as follows:

If the code executed when you run your unit tests covers all the code in the class you test (system under test, SUT), you obviously tested all relevant code.
So, a high code coverage of the SUT is a good thing.

But it also can be misleading. Having a 100% code coverage doesn't mean that you actually tested all your business logic. So, concentrating on testing all your business logic is actually the better approach.
If you tested all your business logic, you will have 100% code coverage - or some code in your business logic that is not needed there.
Still, you can use the code coverage to check if you actually have tested all your business logic.

So, to sum up:

  1. If you don't have 100% code coverage of your SUT, it strongly suggest that you haven't tested your complete business logic
  2. BUT: 100% code coverage doesn't ensure that you tested all of your logic
于 2013-02-07T15:33:18.477 回答
0

从字面上看,“单元测试”测试单个单元,或者换句话说,测试单个组件。因此,单元测试不一定涵盖业务需求,而是确保组件的每个部分都按照它的用途进行。代码覆盖率衡量已测试并因此得到验证的代码的百分比。每一段未经测试的代码都可能包含缺陷,因此需要高代码覆盖率结果。

当然,单元测试,或者更确切地说,用于实现单元测试的框架也可以用于一次测试多个组件。这些测试更像是集成测试,尽管水平很低。

代码覆盖率和集成(或业务逻辑测试)之间的关系是,如果你有 100% 的代码覆盖率,你就知道每个组件都在做它应该做的事情。但是如果你想确保应用程序做它应该做的事情,那么高代码覆盖率是远远不够的:对于这个额外的集成测试(一次测试多个组件)是需要的。

于 2013-02-07T15:41:57.527 回答
0

带有代码覆盖率的单元测试可能是一个非常强大的工具。

例如。测试方法时,您可以查看是否已命中所有逻辑路径。如果不需要更多测试。如果不可能命中一段代码,你知道你可以删除它而不会产生任何副作用。

于 2013-02-07T15:53:17.883 回答