java 是否可以以编程方式生成 JUnit 测试用例和套件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646671/
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-29 17:24:59  来源:igfitidea点击:

Is it possible to programmatically generate JUnit test cases and suites?

javaunit-testingjunitjunit4

提问by Uri

I have to write a very large test suite for a complex set of business rules that are currently captured in several tabular forms (e.g., if parameters X Y Z are such and such, the value should be between V1 and V2). Each rule has a name and its own semantics.

我必须为当前以多种表格形式捕获的一组复杂业务规则编写一个非常大的测试套件(例如,如果参数 XYZ 如此这般,则该值应介于 V1 和 V2 之间)。每个规则都有一个名称和它自己的语义。

My end goal is to have a test suite, organized into sub test suites, with a test case to each rule.

我的最终目标是有一个测试套件,组织成子测试套件,每个规则都有一个测试用例。

One option is to actually hard code all these rules as tests. That is ugly, time consuming, and inflexible.

一种选择是将所有这些规则硬编码为测试。这是丑陋的,耗时的,而且不灵活。

Another is to write a Python script that would read the rule files and generate Java classes with the unit tests. I'd rather avoid this if I can. Another variation would be to use Jython.

另一种方法是编写一个 Python 脚本来读取规则文件并生成带有单元测试的 Java 类。如果可以,我宁愿避免这种情况。另一种变体是使用 Jython。

Ideally, however I would like to have a test suite that would read the files, and would then define sub-suites and tests within them. Each of these tests might be initialized with certain values taken from the table files, run fixed entry points in our system, and then call some validator function on the results based on the expected value.

理想情况下,但是我想要一个测试套件来读取文件,然后在其中定义子套件和测试。这些测试中的每一个都可能使用从表文件中获取的某些值进行初始化,在我们的系统中运行固定的入口点,然后根据预期值对结果调用一些验证器函数。

Is there a reasonable way to pull this off using only Java?

有没有一种合理的方法可以只使用 Java 来解决这个问题?

Update: I may have somewhat simplified our kind of rules. Some of them are indeed tabular (excel style), others are more fuzzy. The general question though remains as I'm probably not the first person to have this problem.

更新:我可能稍微简化了我们的规则。其中一些确实是表格(excel 样式),而另一些则更加模糊。不过,一般问题仍然存在,因为我可能不是第一个遇到这个问题的人。

采纳答案by Kathy Van Stone

Within JUnit 4 you will want to look at the Parameterized runner. It was created for the purpose you describe (data driven tests). It won't organize them into suites however.

在 JUnit 4 中,您将需要查看Parameterized runner。它是为您描述的目的而创建的(数据驱动的测试)。但是,它不会将它们组织成套房。

In Junit 3 you can create TestSuites and Tests programatically. The answer is in Junit Recipes, which I can expand if you need it (remember that JUnit 4 can run Junit 3 tests).

在 Junit 3 中,您可以以编程方式创建测试套件和测试。答案在Junit Recipes 中,如果您需要,我可以扩展它(请记住,JUnit 4 可以运行 Junit 3 测试)。

回答by ankon

Have you considered using FITfor that?

您是否考虑过为此使用FIT

You seem to have the tables already ready, and "business rules" sounds like "business people write them using excel".

您似乎已经准备好了表格,“业务规则”听起来就像“业务人员使用 excel 编写它们”一样。

FIT is a system for checking tests based on tables with input->expected output mappings, and a open source java library for running those tests is available.

FIT 是一种基于具有输入-> 预期输出映射的表来检查测试的系统,并且可以使用用于运行这些测试的开源 Java 库。

回答by Vladimir

We tried FIT and decided to go with Concordion. The main advantages of this library are:

我们尝试了 FIT 并决定使用Concordion。这个库的主要优点是:

  • the tests can be checked in alongside the code base (into a Subversion repository, for example)
  • they are executed by a standard JUnit runner
  • 测试可以与代码库一起检入(例如,进入 Subversion 存储库)
  • 它们由标准的 JUnit 运行程序执行

回答by ndp

I wrote something very similar using JUnit. I had a large number of test cases (30 pages) in an XML file. Instead of trying to generate different tests, I did it all in a single test, which worked just fine.

我用 JUnit 写了一些非常相似的东西。我在一个 XML 文件中有大量的测试用例(30 页)。我没有尝试生成不同的测试,而是在一个测试中完成了所有工作,效果很好。

My test looked something like this:

我的测试看起来像这样:

void setup() { 
  cases = read in the xml file
}

void test_fn_works() {
  for case in cases {
    assert(case.expected_result, fn(case.inputs), 
        'Case ' + case.inputs + ' should yield ' + case.expected_result);

  }
}

With Ruby, I did exactly what you are saying-- generating tests on the fly. Doing this in Java, though, is complex, and I don't think it is worth it since there is another, quite reasonable approach.

使用 Ruby,我完全按照您说的做了——即时生成测试。但是,在 Java 中执行此操作很复杂,而且我认为不值得这样做,因为还有另一种非常合理的方法。

Hope this helps.

希望这可以帮助。