java JUnit 理论与参数化测试的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5727616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:29:51  来源:igfitidea点击:

Difference between JUnit Theories and Parameterized Tests

javajunit4parameterized

提问by dogbane

What is the difference between a Theory and a Parameterized test?

理论和参数化测试有什么区别?

I'm not interested in implementation differences when creating the test classes, just when you would choose one over the other.

我对创建测试类时的实现差异不感兴趣,只是当您选择一个而不是另一个时。

采纳答案by Fabio Kenji

From what I understand: With Parameterized tests you can supply a series of static inputs to a test case.

据我了解:通过参数化测试,您可以为测试用例提供一系列静态输入。

Theories are similar but different in concept. The idea behind them is to create test cases that test on assumptions rather than static values. So if my supplied test data is true according to some assumptions, the resulting assertion is always deterministic. One of the driving ideas behind this is that you would be able to supply an infinite number of test data and your test case would still be true; also, often you need to test an universe of possibilities within a test input data, like negative numbers. If you test that statically, that is, supply a few negative numbers, it is not guaranteed that your component will work against all negative numbers, even if it is highly probable to do so.

理论相似,但概念不同。它们背后的想法是创建测试用例而不是静态值来测试。因此,如果我提供的测试数据根据某些假设是正确的,则结果断言始终是确定性的。这背后的驱动思想之一是您将能够提供无限数量的测试数据并且您的测试用例仍然是真实的;此外,通常您需要在测试输入数据中测试各种可能性,例如负数。如果您进行静态测试,即提供一些负数,则不能保证您的组件将针对所有负数工作,即使这样做的可能性很高。

From what I can tell, xUnit frameworks try to apply theories' concepts by creating all possible combinations of your supplied test data.

据我所知,xUnit 框架尝试通过创建您提供的测试数据的所有可能组合来应用理论概念。

Both should be used when approaching a scenario in a data-driven scenario (i.e only inputs change, but the test is always doing the same assertions over and over).

当接近数据驱动场景中的场景时,两者都应该使用(即只有输入发生变化,但测试总是一遍又一遍地做相同的断言)。

But, since theories seem experimental, I would use them only if I needed to test a series of combinations in my input data. For all the other cases I'd use Parameterized tests.

但是,由于理论似乎是实验性的,因此我仅在需要测试输入数据中的一系列组合时才使用它们。对于所有其他情况,我将使用参数化测试。

回答by gliptak

Parameterized.class tests "parametrize" tests with a single variable, while Theories.class "parametrize" with all combinations of several variables.

Parameterized.class 使用单个变量测试“参数化”测试,而 Theories.class 使用多个变量的所有组合进行“参数化”测试。

For examples please read:

例如,请阅读:

http://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junit

http://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junit

http://blog.schauderhaft.de/2010/02/07/junit-theories/

http://blog.schauderhaft.de/2010/02/07/junit-theories/

http://blogs.oracle.com/jacobc/entry/junit_theories

http://blogs.oracle.com/jacobc/entry/junit_theories

Theories.class is similar to Haskell QuickCheck:

Theories.class 类似于 Haskell QuickCheck:

http://en.wikibooks.org/wiki/Haskell/Testing

http://en.wikibooks.org/wiki/Haskell/Testing

but QuickCheck autogenerates parameter combinations

但 QuickCheck 会自动生成参数组合

回答by andreyro

In addition to above responses: On a input with 4 values and 2 test methods

除了上述响应:在具有 4 个值和 2 个测试方法的输入上

  • @RunWith(Theories.class) - will generate 2 JUnit tests

  • @RunWith(Parameterized.class) - will generate 8 (4 inputs x 2 methods) JUnit tests

  • @RunWith(Theories.class) - 将生成 2 个 JUnit 测试

  • @RunWith(Parameterized.class) - 将生成 8 个(4 个输入 x 2 个方法)JUnit 测试

回答by VHS

A little late in replying. But it would be helpful to the future testers.

回复有点晚。但这对未来的测试人员会有所帮助。

Parameterized Tests vs Theories

参数化测试与理论

  • Class annotated with "@RunWith (Parameterized.class)" VS "@RunWith(Theories.class)"
  • Test inputs are retrieved from a static method returning Collection and annotated with @Parameters vs static fields annotated with @DataPoints or @DataPoint.
  • Inputs are passed to the constructor (mandatory) and used by the test method vs inputs are directly passed to the test method.
  • Test method is annotated with @Test and doen't take arguments vs Test method is annotated with @Theory and may take arguments
  • 用“@RunWith (Parameterized.class)” VS “@RunWith(Theories.class)”注释的类
  • 测试输入从返回 Collection 的静态方法中检索,并使用 @Parameters 进行注释,而静态字段使用 @DataPoints 或 @DataPoint 进行注释。
  • 输入传递给构造函数(强制)并由测试方法使用,而输入则直接传递给测试方法。
  • 测试方法用@Test 注释并且不带参数 vs 测试方法用@Theory 注释并且可以带参数

回答by Ricardo Gomes

From my understanding the difference is that a Parameterized Test is used when all you want to do is test a different set of inputs (test each one individually), a Theory is a special case of a Parameterized Test in which you are testing every input as a whole (every parameter needs to be true).

根据我的理解,不同之处在于当您只想测试一组不同的输入时使用参数化测试(单独测试每个输入),理论是参数化测试的特例,其中您将每个输入测试为一个整体(每个参数都需要为真)。