是否可以为程序中的每个类编写测试用例?

时间:2021-04-20 15:33:16

Im am just getting introduced to unit testing and test driven development. Thus far, I have only used Junit as testing framework. A question which emerged and for which I have not yet found a definite answer is: how much test cases do I need to write? Do I have to write a test case for every single class in my program? Or is this a stupid question because unit testing implies testing at the lowest (i.e. class) level?

我刚刚介绍了单元测试和测试驱动开发。到目前为止,我只使用Junit作为测试框架。出现了一个我尚未找到明确答案的问题是:我需要编写多少测试用例?我是否必须为我的程序中的每个类编写一个测试用例?或者这是一个愚蠢的问题,因为单元测试意味着在最低(即类)级别进行测试?

I think writing a test case for every class is probably the more safe way to go (after all the more you test the lower the number of unforeseen bugs). But I was wondering if there are any widely agreed upon strategies regarding the amount of test cases to write?

我认为为每个班级编写一个测试用例可能是更安全的方法(毕竟你测试的越多,不可预见的错误数量就越多)。但是我想知道是否有关于要编写的测试用例数量的广泛商定的策略?

6 个解决方案

#1


4  

There are no strict rules in this area, only guidelines. You usually have one test case per class, but there are different strategies:

这个领域没有严格的规定,只有指导方针。您通常每个类有一个测试用例,但有不同的策略:

For example you can use 'Per Class' approach for classes that are relatively small and follow SRP. But if you have legacy code with a giant *Manager class it is fine to use 'Per Method' and have dedicated test case for only one of its methods. I think that choosing naming strategy for tests is at least as important as test code organization.

例如,您可以对相对较小的类使用“Per Class”方法并遵循SRP。但是如果你有一个巨大的* Manager类的遗留代码,可以使用'Per Method'并且只为其中一个方法提供专用的测试用例。我认为选择测试的命名策略至少与测试代码组织一样重要。

Using code coverage tool can help you find spots of untested code. It is less useful as metric. Having high code coverage does not necessarily mean that you have good tests. At the end of the day what matters is that you have meaningful and readable tests.

使用代码覆盖率工具可以帮助您找到未经测试的代码点。它作为指标不太有用。拥有高代码覆盖率并不一定意味着您有良好的测试。在一天结束时,重要的是你有有意义和可读的测试。

#2


7  

If you're trying on TDD, then you shouldn't be writing any code at all without having a failing test that tells you to do so. By implication, then, you'll never have a class that doesn't have one or more tests for it. A rule of thumb that tends to get quoted is that you should end up with about 2.5 times as much test source as main source.

如果您正在尝试使用TDD,那么在没有告知您这样做的失败测试的情况下,您根本不应该编写任何代码。通过暗示,你永远不会有一个没有一个或多个测试的类。一个经常被引用的经验法则是你应该得到大约2.5倍的测试源作为主要来源。

#3


3  

I wouldn't recommend a strict mapping of one test per class. Some classes may not have much worth testing on their own. Some classes may require multiple tests because you want to specify different setups for different cases. You should use a code coverage tool like Cobertura and try to cover as much code as possible. Also you should look at the code you're testing and see what kind of different data should break it, and try to test it with different combinations of sample data (so 100% code coverage is not the end of testing, of course).

我不建议每个类严格映射一个测试。有些课程可能没有多少值得自己测试。某些类可能需要多次测试,因为您要为不同的情况指定不同的设置。你应该使用像Cobertura这样的代码覆盖工具,尽量覆盖尽可能多的代码。此外,您应该查看您正在测试的代码,看看哪种不同的数据应该破坏它,并尝试使用不同的样本数据组合进行测试(因此100%的代码覆盖率当然不是测试的结束)。

#4


2  

Though better than 1:1 ratio or over 100% coverage is ideal, in general I tend to abide by "test that which could possibly break".

虽然优于1:1的比例或超过100%的覆盖率是理想的,但总的来说我倾向于遵守“测试可能会破坏的”。

This means only testing code with 'working parts'. This excludes POJOs, DTOs, thin wrapper/adapter classes and classes that only exist to fit a framework, and so on.

这意味着只用“工作部件”测试代码。这不包括POJO,DTO,瘦包装器/适配器类以及仅适合框架的类,等等。

Also, "test only code that is under your control". This generally means not writing explicit tests for generated code—such as Web service clients generated from WSDL (but it still makes sense to cover them as part of tests written for your own classes).

此外,“仅测试您控制的代码”。这通常意味着不为生成的代码编写显式测试 - 例如从WSDL生成的Web服务客户端(但是仍然有必要将它们作为为您自己的类编写的测试的一部分)。

#5


1  

If you're doing TDD and extreme programming, (I think this helps explain it), you program in pairs. The first person writes a test for a feature that doesn't yet exist. The test should prove that the feature is 100% functional and handles all required cases. The second programmer then writes the code that makes the test pass perfectly. It must be rewritten until it completely satisfies the test. Usually you maintain a test suite for TDD which can be rerun constantly, detecting any failures and generating a report - though this is ambitious for personal use.

如果您正在进行TDD和极限编程,(我认为这有助于解释它),您可以成对编程。第一个人为一个尚不存在的功能编写测试。测试应证明该功能100%正常运行并处理所有必需的案例。然后第二个程序员编写使测试完全通过的代码。必须重写它才能完全满足测试要求。通常,您维护一个TDD测试套件,可以不断重新运行,检测任何故障并生成报告 - 尽管这对于个人使用是雄心勃勃的。

In any case, for TDD a new feature cannot exist without a new test being made first - the test Drives the Development. So, your test is there for every feature by default if you're doing it right.

在任何情况下,对于TDD,如果没有首先进行新测试,则不能存在新功能 - 测试驱动开发。因此,如果您正确执行此操作,默认情况下您的测试适用于所有功能。

#6


1  

I'm not strict as other commenters about writing the test fist and code afterwards. You should do that, but not many people actually do that. The important thing is to write tests ideally before writing code, or right after you wrote the code. But not days/weeks/months/years after that, because you wouldn't do it and the test would suck.

我不像其他评论员那样严格地写下测试拳和代码。你应该这样做,但实际上没有多少人这样做。重要的是在编写代码之前或编写代码之后立即编写测试。但不是之后的几天/几周/几个月/几年,因为你不会这样做而且测试会很糟糕。

I usually divide application to several parts: application logic, business logic, data access layer, domain objects and helper classes. It is most important to test completely business logic and helper classes. Other parts of application are less important, but you can test some parts. Also do some integration tests.

我通常将应用程序划分为几个部分:应用程序逻辑,业务逻辑,数据访问层,域对象和帮助程序类。完全测试业务逻辑和帮助程序类是最重要的。其他应用部分不太重要,但您可以测试一些部件。还做一些集成测试。

The most important thing is not to overthink it. Just try to do it on some small project and you will see by yourself.

最重要的是不要过度思考它。试着在一个小项目上做,你会自己看。

#1


4  

There are no strict rules in this area, only guidelines. You usually have one test case per class, but there are different strategies:

这个领域没有严格的规定,只有指导方针。您通常每个类有一个测试用例,但有不同的策略:

For example you can use 'Per Class' approach for classes that are relatively small and follow SRP. But if you have legacy code with a giant *Manager class it is fine to use 'Per Method' and have dedicated test case for only one of its methods. I think that choosing naming strategy for tests is at least as important as test code organization.

例如,您可以对相对较小的类使用“Per Class”方法并遵循SRP。但是如果你有一个巨大的* Manager类的遗留代码,可以使用'Per Method'并且只为其中一个方法提供专用的测试用例。我认为选择测试的命名策略至少与测试代码组织一样重要。

Using code coverage tool can help you find spots of untested code. It is less useful as metric. Having high code coverage does not necessarily mean that you have good tests. At the end of the day what matters is that you have meaningful and readable tests.

使用代码覆盖率工具可以帮助您找到未经测试的代码点。它作为指标不太有用。拥有高代码覆盖率并不一定意味着您有良好的测试。在一天结束时,重要的是你有有意义和可读的测试。

#2


7  

If you're trying on TDD, then you shouldn't be writing any code at all without having a failing test that tells you to do so. By implication, then, you'll never have a class that doesn't have one or more tests for it. A rule of thumb that tends to get quoted is that you should end up with about 2.5 times as much test source as main source.

如果您正在尝试使用TDD,那么在没有告知您这样做的失败测试的情况下,您根本不应该编写任何代码。通过暗示,你永远不会有一个没有一个或多个测试的类。一个经常被引用的经验法则是你应该得到大约2.5倍的测试源作为主要来源。

#3


3  

I wouldn't recommend a strict mapping of one test per class. Some classes may not have much worth testing on their own. Some classes may require multiple tests because you want to specify different setups for different cases. You should use a code coverage tool like Cobertura and try to cover as much code as possible. Also you should look at the code you're testing and see what kind of different data should break it, and try to test it with different combinations of sample data (so 100% code coverage is not the end of testing, of course).

我不建议每个类严格映射一个测试。有些课程可能没有多少值得自己测试。某些类可能需要多次测试,因为您要为不同的情况指定不同的设置。你应该使用像Cobertura这样的代码覆盖工具,尽量覆盖尽可能多的代码。此外,您应该查看您正在测试的代码,看看哪种不同的数据应该破坏它,并尝试使用不同的样本数据组合进行测试(因此100%的代码覆盖率当然不是测试的结束)。

#4


2  

Though better than 1:1 ratio or over 100% coverage is ideal, in general I tend to abide by "test that which could possibly break".

虽然优于1:1的比例或超过100%的覆盖率是理想的,但总的来说我倾向于遵守“测试可能会破坏的”。

This means only testing code with 'working parts'. This excludes POJOs, DTOs, thin wrapper/adapter classes and classes that only exist to fit a framework, and so on.

这意味着只用“工作部件”测试代码。这不包括POJO,DTO,瘦包装器/适配器类以及仅适合框架的类,等等。

Also, "test only code that is under your control". This generally means not writing explicit tests for generated code—such as Web service clients generated from WSDL (but it still makes sense to cover them as part of tests written for your own classes).

此外,“仅测试您控制的代码”。这通常意味着不为生成的代码编写显式测试 - 例如从WSDL生成的Web服务客户端(但是仍然有必要将它们作为为您自己的类编写的测试的一部分)。

#5


1  

If you're doing TDD and extreme programming, (I think this helps explain it), you program in pairs. The first person writes a test for a feature that doesn't yet exist. The test should prove that the feature is 100% functional and handles all required cases. The second programmer then writes the code that makes the test pass perfectly. It must be rewritten until it completely satisfies the test. Usually you maintain a test suite for TDD which can be rerun constantly, detecting any failures and generating a report - though this is ambitious for personal use.

如果您正在进行TDD和极限编程,(我认为这有助于解释它),您可以成对编程。第一个人为一个尚不存在的功能编写测试。测试应证明该功能100%正常运行并处理所有必需的案例。然后第二个程序员编写使测试完全通过的代码。必须重写它才能完全满足测试要求。通常,您维护一个TDD测试套件,可以不断重新运行,检测任何故障并生成报告 - 尽管这对于个人使用是雄心勃勃的。

In any case, for TDD a new feature cannot exist without a new test being made first - the test Drives the Development. So, your test is there for every feature by default if you're doing it right.

在任何情况下,对于TDD,如果没有首先进行新测试,则不能存在新功能 - 测试驱动开发。因此,如果您正确执行此操作,默认情况下您的测试适用于所有功能。

#6


1  

I'm not strict as other commenters about writing the test fist and code afterwards. You should do that, but not many people actually do that. The important thing is to write tests ideally before writing code, or right after you wrote the code. But not days/weeks/months/years after that, because you wouldn't do it and the test would suck.

我不像其他评论员那样严格地写下测试拳和代码。你应该这样做,但实际上没有多少人这样做。重要的是在编写代码之前或编写代码之后立即编写测试。但不是之后的几天/几周/几个月/几年,因为你不会这样做而且测试会很糟糕。

I usually divide application to several parts: application logic, business logic, data access layer, domain objects and helper classes. It is most important to test completely business logic and helper classes. Other parts of application are less important, but you can test some parts. Also do some integration tests.

我通常将应用程序划分为几个部分:应用程序逻辑,业务逻辑,数据访问层,域对象和帮助程序类。完全测试业务逻辑和帮助程序类是最重要的。其他应用部分不太重要,但您可以测试一些部件。还做一些集成测试。

The most important thing is not to overthink it. Just try to do it on some small project and you will see by yourself.

最重要的是不要过度思考它。试着在一个小项目上做,你会自己看。