简体   繁体   English

生成用于所有对测试的测试

[英]Generating tests for all-pairs testing

I have a short (Java) method with several parameters that performs some non-intuitive computation. 我有一个简短的(Java)方法,其中包含几个参数,它们执行一些非直观的计算。 Writing unit tests for this method in order to achieve code coverage is trivial, but the tests don't cover the subtleties of the method. 为此方法编写单元测试以实现代码覆盖是微不足道的,但是测试并未涵盖该方法的精妙之处。 I am looking for a testing framework or some other tool that will allow me to do all-pairs (all-tuples) testing for this method, based on partitions that I define for the parameters. 我正在寻找一个测试框架或其他工具,该工具将允许我基于为参数定义的分区对该方法进行所有对(所有元组)测试。

For example, suppose I have the following method: 例如,假设我有以下方法:

int foo(int begin, int end, int first, int last) {
    ...
}

I already know some constraints, which may be enforced externally: 我已经知道一些约束,可以从外部强制执行:

begin <= end
first <= last

I want to define explicit equivalence relations, based on my own knowledge of the parameters. 我想根据我自己对参数的了解来定义显式的等价关系。 For example: 例如:

begin == MIN
begin > MIN && begin < 0
begin == 0
begin == 1
begin > 1 && begin < MAX
begin == MAX

I would also like to define equivalence relations involving more than one parameter. 我还想定义涉及多个参数的等价关系。 For example: 例如:

begin + 1 < end
begin + 1 == end
begin == end

A combination to test is the selection of an equivalence class from each of the equivalence relations (eg, begin == 0 from the first relation and begin + 1 < end from the second relation), such that the constraints are satisfiable. 要进行测试的组合是从每个等价关系中选择一个等价类(例如,从第一个关系begin == 0从第二个关系begin + 1 < end ),这样约束就可以满足了。 [Incidentally, I realize satisfiability is NP-complete. [顺便说一句,我意识到可满足性是NP完全的。 I'm willing to live with it, given combinations that comprise a relatively small set of constraints.] With several parameters, the number of combinations becomes unwieldy. 如果组合包含相对较少的约束集,我愿意使用它。]通过几个参数,组合的数量变得笨拙。 Manually writing a test for each combination and finding arguments that satisfy the resultant set of constraints is tedious and error prone. 手动为每种组合编写测试并找到满足最终约束集的参数很乏味且容易出错。

I'm looking for a testing framework that can automatically test each combination, or a test generator that can generate a test for each logically consistent combination (invariant checks to be inserted by yours truly). 我正在寻找一个可以自动测试每个组合的测试框架,或者一个可以为每个逻辑上一致的组合生成测试的测试生成器(由您的真实插入的不变检查)。 This is different from test-generation tools like CodePro , which generate test cases from the actual implementation. 这与CodePro之类的测试生成工具不同 ,后者从实际实现中生成测试用例。 Effectively, I want to generate tests for an interface of the method, rather than its implementation. 实际上,我想为该方法的接口而不是其实现生成测试。 Not only would this allow me to write the tests before the method is implemented (TDD), but it would ensure the method is comprehensively tested even after its later modification. 这不仅使我能够在实现该方法(TDD)之前编写测试,而且即使在以后的修改之后,也可以确保对该方法进行了全面的测试。

Does such a tool exist, or can something be kludged together? 是否存在这样的工具,或者可以将某些东西放在一起吗? Or perhaps I'm approaching this the wrong way, and you have another suggestion... 也许我正以错误的方式来对待,而您还有另一个建议...

Wouldn't simple nested loops, where a test method is invoked at each innermost loop iteration, fit the bill (or at least mostly fit the bill)? 简单的嵌套循环(在每个最内层的循环迭代中调用测试方法)是否符合要求(或至少主要符合要求)?

Something like: 就像是:

int[] ends = new int[] {-50, -10, -1, 0, 1, 50, MIN, MAX};
for (int end : ends) {
    int[] begins = new int[] {end - 10, end - 1, end, MIN, MAX};
    for (int begin : begins) {
        if (begin <= end && begin >= MIN && begin <= MAX) {
             testFoo(begin, end);
        }
    }
}

有关此方面的一些想法,请参阅David Saff理论测试方面 工作。

Can you use a tool outside your test framework? 您可以在测试框架之外使用工具吗? If so, I've used James Bach's AllPairs with good results. 如果是这样,我使用James Bach的AllPairs效果很好。 There's also Hexawise . 还有Hexawise

If you weren't aware of it, http://pairwise.org is a great place to read on pairwise and combinatorial testing and tools! 如果您不了解, http://pairwise.org是阅读成对和组合测试和工具的好地方!

If you are still interested in libraries that perform combinatorial tests under JUnit framework. 如果您仍然对在JUnit框架下执行组合测试的库感兴趣。 I recently implemented a library 'JCUnit', which does exactly it. 我最近实现了一个库“ JCUnit”,它确实做到了。

It generates test cases under JUnit and run the test method with them. 它在JUnit下生成测试用例,并使用它们运行测试方法。

http://dakusui.github.io/jcunit/ http://dakusui.g​​ithub.io/jcunit/

I may suggest that if you TDDed the algorithm, then you already likely have sufficient test coverage. 我可能建议,如果您对算法进行了TDD,那么您可能已经具有足够的测试覆盖率。 If anyone subsequently comes in and changes the algorithm, you should see at least one of the original tests break. 如果随后有人进入并更改了算法,则应该看到至少有一个原始测试中断。 If you have some edge conditions to test, you could write a couple, but if these edges aren't interesting enough to turn red once you've added them, then they probably aren't interesting enough to keep around and duplicate the existing tests. 如果您有一些要测试的边缘条件,则可以编写一些,但是如果添加后这些边缘没有足够的趣味性使其变为红色,那么它们可能就没有足够的趣味性来保留和复制现有测试。 Redundant tests are less valuable and I tend to delete them. 冗余测试的价值较低,我倾向于将其删除。

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

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