简体   繁体   English

JUnit - 有多少文件用于测试单个类?

[英]JUnit - How many files for testing a single class?

Let's say I have a class MyClass.java and we have to write a tests for it.假设我有一个类MyClass.java ,我们必须为它编写一个测试。 Some of them will execute once, some of them will execute multiple times.其中一些将执行一次,其中一些将执行多次。

  1. I prepared two test classes - MyClassTestSingle.java for single tests and MyClassTestMultiple.java for tests with @Parametrized annotation.我准备了两个测试类 - MyClassTestSingle.java用于单个测试, MyClassTestMultiple.java用于带有@Parametrized注释的测试。
  2. I wanted to run them from a single file so there is another class - MyClassTestSuite.java .我想从一个文件中运行它们,所以还有另一个类 - MyClassTestSuite.java
  3. In MyClassTestSingle.java and MyClassTestMultiple.java methods I use same parameters with a same values so i started thinking about a class containing these parameters...MyClassTestSingle.javaMyClassTestMultiple.java方法中,我使用具有相同值的相同参数,所以我开始考虑包含这些参数的类......

But then I realized I wanted to create four classes for testing a single class.但后来我意识到我想创建四个类来测试一个类。 So I started looking for some "good practices" how to keep my test classes clean and simple.所以我开始寻找一些“好的实践”如何保持我的测试类干净和简单。 But found no worthy informations about number of classes I need when writing tests for a situation above.但是在为上述情况编写测试时,没有发现有关我需要的类数量的有价值的信息。

I started thinking that maybe there is a design problem in my MyClass.java , but for sure there are situations when there is need to make a parametrized and single tests and then pack them into a suite.我开始想我的MyClass.java中可能存在设计问题,但肯定存在需要进行参数化和单个测试然后将它们打包成套件的情况。

Am I wrong?我错了吗? Or maybe I just don't understand something?或者,也许我只是不明白什么?

There is no "right" or "wrong" way here.这里没有“正确”或“错误”的方式。 However there are some conventions.但是有一些约定。

First, typically test cases names are typically ended by Test .首先,通常测试用例名称通常以Test结尾。 Typically we implement test case per class and name it after the production class with Test suffix.通常我们为每个类实现测试用例,并以带有Test后缀的生产类命名。 Follow this convention and various build and reporting tools will work for you automatically without additional configuration.遵循此约定,各种构建和报告工具将自动为您工作,无需额外配置。

Splitting test case to several classes can be used if it is needed.如果需要,可以使用将测试用例拆分为多个类。 For example when number of tests or number of code line exceeds reasonable limit or if there are some test groups that should be maintained separately.例如,当测试数量或代码行数量超过合理限制时,或者如果有一些测试组应该单独维护。 Your example (parameterized and not parameterized tests) sounds like reasonable case.您的示例(参数化和非参数化测试)听起来很合理。

So, I'd create MyClassSingleTest.java and MyClassMultipleTest.java .所以,我会创建MyClassSingleTest.javaMyClassMultipleTest.java

Variants: MyTest.java and MyTestParameterized.java .变体: MyTest.javaMyTestParameterized.java

Probably a good idea to give more meaningful name.给出更有意义的名称可能是个好主意。 Try to express what do your parameters mean.试着表达你的参数是什么意思。 For example UserParameters , DbParameters etc. In this case call your parameterzied class MyClassUserTest.java or something like that.例如UserParametersDbParameters等。在这种情况下,请调用您的参数化类MyClassUserTest.java或类似的东西。

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

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