简体   繁体   English

编写单元测试(java)的最佳位置?

[英]best location to write a unit-test (java)?

What is the difference between writing a unit test in the same class as the tested method or writing it in another class (of the same package or extern package)? 在与被测方法相同的类中编写单元测试,或在另一个类(相同的包或extern包)中编写单元测试,有什么区别? What are the advantages or disadvantages of these test-locations? 这些测试位置的优缺点是什么?

If you have your tests in separate class(es), you can 如果您在单独的课程中进行测试,则可以

  • use a well known unit testing framework which makes your life way easier - all the ones I know assume that you keep your tests separated from the tested code 使用一个众所周知的单元测试框架 ,它使您的生活更轻松-我所知道的所有假设都假定您将测试与测试的代码分开
  • have as many test cases as you like (without making your production class huge and hard to manage - note that in a well managed project, there is about as much unit test code as production code, and probably a lot more public test methods than production API methods, which may eventually drive the API indexer of your IDE, and subsequently yourself, crazy...) 拥有任意数量的测试用例(不会使您的生产类变得庞大且难以管理-请注意,在一个管理良好的项目中,单元测试代码与生产代码差不多,并且公共测试方法可能比生产更多API方法,这些方法最终可能会驱动您的IDE的API索引器,然后驱动您自己,疯狂……)
  • ship your production code without tests - including your test dependencies too (noone likes to bundle a ton of 3rd party stuff used only for testing into the product deployment bundle, right?) 无需测试就可以交付您的生产代码 -包括您的测试依赖项(没有人喜欢将大量仅用于测试的第三方材料捆绑到产品部署捆绑中,对吗?)
  • change tests without touching the production code itself (this will make auditing and change control in your SCM a lot easier, as it will always be trivial to separate changes in production code/data from changes in tests) 更改测试而无需触及生产代码本身(这将使您的SCM中的审核和更改控制变得更加容易,因为将生产代码/数据的更改与测试的更改分开总是很容易的)

The best practice is separate tests from the code they are validating. 最佳做法是将测试与要验证的代码分开。 The reason is simple: you do not want to package tests with your application. 原因很简单:您不想将测试与您的应用程序打包。 Application should be clean. 应用程序应该是干净的。 Tests should validate functionality from outside. 测试应从外部验证功能。

Tests often have additional dependencies (eg testing framework, mockup library etc). 测试通常具有其他依赖性(例如,测试框架,模型库等)。

Common practice is to put source code under src/main/java and tests under src/tests/java . 常见的做法是将源代码放在src/main/java ,将测试放在src/tests/java

Advantages of writing in separated classes is that you can separate your tests from your code. 在分离的类中编写的好处是可以将测试与代码分离。 For example with maven : 例如,使用maven:
1. Build everything 1.建立一切
2. Executes the classes including the **/*Test*.java pattern 2.执行包括**/*Test*.java pattern

I don't even know how you can unit test in the same class than your code... 我什至不知道如何在与代码相同的类中进行单元测试...

Test methods are there to test your code. 测试方法可以测试您的代码。 Putting them in the tested class clutters the API of the class, and makes the bytecode and dependencies of the class larger than necessary. 将它们放在经过测试的类中会使该类的API混乱,并使该类的字节码和依赖性更大。

If I want to use your Calculator class, I don't need the testPlusWorks() and testMinusWorks() test methods. 如果要使用您的Calculator类,则不需要testPlusWorks()testMinusWorks()测试方法。 I just need plus() and minus() . 我只需要plus()minus() And if testPlusWorks depends on some class from the JUnit or Mockito library, I don't want these dependencies in the production environment: they're only useful when testing the class. 而且,如果testPlusWorks依赖于JUnit或Mockito库中的某个类,那么我不希望这些依赖在生产环境中起作用:它们仅在测试类时有用。

There is not "right" answer for this question. 这个问题没有“正确”的答案。

Writing in the same class: Allows you to test private methods but mess up the code. 在同一个类中编写:允许您测试私有方法,但弄乱了代码。

Writing in the same project: Allows you to test internal methods/logics but mess up the project and extends your compilation time. 在同一项目中编写:允许您测试内部方法/逻辑,但会弄乱项目并延长编译时间。

Writing externally: Prevents you from testing project internal methods/classes but keeps the tests clean and external to your "production" coder. 外部编写:防止您测试项目的内部方法/类,但保持测试整洁并在“生产”编码器的外部。

I find it best to write unit tests in a different source folder altogether, but keep them in the same package as you can still access packaged(default) scoped methods and varialble. 我发现最好将单元测试完全写在不同的源文件夹中,但是将它们保留在同一程序包中,因为您仍然可以访问打包的(默认)范围内的方法和变量。

  1. This keeps your tests together for ease of navigation and sanity and 这样可以将您的测试结合在一起,从而简化导航和理智操作,
  2. When you do a build, you can exclude the test classes in the jar/war/ear. 进行构建时,可以在jar / war / ear中排除测试类。

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

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