简体   繁体   English

JUnit:放置测试方法的最佳策略

[英]JUnit: best strategy for placing test-methods

I started a project and using JUnit for the 1st time. 我开始一个项目,第一次使用JUnit。

Whats the best practice of putting testcases? 放置测试用例的最佳实践是什么?

  • 1 testclass for every "real" class. 每个“真实”类都有1个测试类。

  • 1 testclass for every package or even the complete project. 每个包甚至整个项目都有1个测试类。

  • Testmethods in the "real" class without a testclass. 没有测试类的“真实”类中的测试方法。

As far I see I can technically do every 3 ways but I do not have experience in that so I ask for some guidance to do it right from the very beginning. 据我所知,我可以从技术上每三种方式进行操作,但是我没有这方面的经验,因此我希望从一开始就寻求一些指导。

EDIT 编辑

I am talking about code unit testing. 我说的是代码单元测试。 I am using maven too but I think that is not important for my question. 我也正在使用Maven,但我认为这对我的问题并不重要。

1 test class for every "real" class. 每个“真实”课程都有1个测试课程。

I typically go with this pattern. 我通常采用这种模式。 Of course, tests for interfaces don't make much sense and there are times when small "entity" classes with getter and setter methods only (ie no logic) don't need a corresponding test class. 当然,对接口的测试没有多大意义,并且有时仅具有getter和setter方法的小型“实体”类(即无逻辑)不需要相应的测试类。

That said, I've been surprised at what utility I've found in unit tests even on very small classes. 就是说,即使在很小的类上,我也对单元测试中发现的实用程序感到惊讶。 For example, even entity classes with only get/set methods which are stored in databases through DAO methods should be tested in case some of the database wiring is incorrect. 例如,即使某些仅具有get / set方法的实体类(通过DAO方法存储在数据库中)也应进行测试,以防某些数据库连接不正确。 You never know when you have a mismatched get/set method or if the toString() , asymmetric hashcode() or equals() , or other issues. 您永远不知道何时使用的get / set方法不匹配,或者toString() ,非对称hashcode()equals()或其他问题。

The entire point of "unit" tests is (IMHO) to test the smallest unit of your code in isolation -- this is the class. “单元”测试的重点是(IMHO),用于隔离测试代码的最小单元-这就是该类。 So therefore when I have a ContainerUtil class, I look for a corresponding ContainerUtilTest class in the test directory. 因此,当我有一个ContainerUtil类时,我会在测试目录中寻找相应的ContainerUtilTest类。 I run coverage tests often and I expect just about any logic portions of all classes to be covered. 我经常运行覆盖率测试,并且我期望所有类的几乎所有逻辑部分都将被覆盖。

1 test class for every package or even the complete project. 每个软件包甚至整个项目都有1个测试类。

I might have this as well but then I'd consider these to be "integration" tests. 我可能也有这个但是后来我认为它们是“集成”测试。 Tests that bridge between classes or between various parts of the project to ensure that your project works as a whole. 测试之间或项目的各个部分之间建立桥梁以确保您的项目可以整体运行。

But these would be in addition to your unit tests. 但是这些将是对单元测试的补充

Test methods in the "real" class without a test class. 没有测试类的“真实”类中的测试方法。

Yeah, no. 是的不 Really bad idea. 真是个坏主意。 You don't want your production code to include test code if at all possible. 您根本不希望生产代码包含测试代码。 It decreases the readability of your classes, increases the change that you break something while trying to test, etc.. Just say no. 它降低了类的可读性,增加了您在尝试测试时破坏某些内容的更改,等等。请说不。

I also keep my test classes away from my sources. 我还将测试课程远离我的资源。 I usually use maven so I have my sources in src/main/java and my tests in src/test/java . 我通常使用Maven,因此我的源代码在src/main/java而我的测试则在src/test/java You don't want your tests to end up in the jar or war files where they might confuse others. 您不希望测试最终出现在jar或war文件中,它们可能会使其他人感到困惑。

It really depends on how big your project is, but in my experience the best approach would be one test class for every "big" functionality (this may not apply for unit testing), or in this case, for every "real" class. 这实际上取决于您的项目的大小,但是以我的经验,最好的方法是针对每个“大型”功能(这可能不适用于单元测试)使用一个测试类,或者在这种情况下,针对每个“真实”类。

About the other 2: 关于其他2:

1 testclass for every package or even the complete project. 每个包甚至整个项目都有1个测试类。

This may grow to big and messy, wouldn't recommend mixing different things in the same test class, the same way that you wouldn't mix classes within the same file 这可能会变得又大又混乱,不建议您在同一测试类中混合不同的内容,就像您不会在同一文件中混合类一样

Testmethods in the "real" class without a testclass. 没有测试类的“真实”类中的测试方法。

I do not recommend this one either as you lose track of where are the tests and which things have tests implemented vs are missing. 我也不建议您使用该工具,因为您会丢失测试的位置以及实施了测试的内容与丢失的内容。 Also, your code may require accessing other classes as well, so this may become a mess as again. 同样,您的代码也可能需要访问其他类,因此这可能又变得一团糟。

For unit testing, i so far have used one test class for each tested class. 对于单元测试,到目前为止,我已经为每个测试类使用了一个测试类。 FOR ME, it seemed to be the least messy order. 对我来说,这似乎是最不混乱的命令。 I put the unit tests under src/test/java in the same package tree as the tested classes in src/main/java. 我将单元测试放在src / test / java下,与src / main / java中的测试类放在同一包树中。 Integration tests are different and have their own files each. 集成测试是不同的,并且每个都有自己的文件。

One testclass has different disadvantages. 一个测试类具有不同的缺点。 Source code will become unreadable. 源代码将变得不可读。 You will do a lot of unnecessary work in @Before and @BeforeEach methods. 您将在@Before和@BeforeEach方法中做很多不必要的工作。

And i don't get the point of putting tests into the tested class. 而且我没有把测试放入测试类的意义。 Lots of imports, and how would you differ between „real“ and test methods? 大量进口,“真实”和测试方法之间有何区别? And because of the additional methods, source code will become unreadable. 并且由于其他方法,源代码将变得不可读。

I would suggest you try the first approach. 我建议您尝试第一种方法。 It is very useful because you can track using some tool as Sonar the percentage of coverage from your unit tests. 这非常有用,因为您可以使用Sonar这样的工具来跟踪单元测试的覆盖率。

Also, I strongly recommend you apply TDD to develop your code: first you write your test code to fail, then you write code to make your test pass and then you refactor. 另外,我强烈建议您使用TDD来开发代码:首先,编写失败的测试代码,然后编写代码以使测试通过,然后进行重构。

Allow me to suggest two sources of reading to help you with that: 请允许我提出两种阅读来源,以帮助您:

These are the same reading resources I used to start building tests and using TDD. 这些与我用来开始构建测试和使用TDD的阅读资源相同。

I woudn't recommend you to use the other approaches, as you don't need to ship test code to production and using a single test class would cause a code smell known as "Large Class". 我不建议您使用其他方法,因为您不需要将测试代码发送到生产环境,并且使用单个测试类会导致一种称为“大类”的代码气味。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM