简体   繁体   English

使用JUnit运行端到端测试

[英]Running end-to-end tests with JUnit

We are developing a Java command-line app for which we'd like to apply data-driven tests from many many external (properties) files AND allow some knowledgeable users to add tests without touching the Java codebase. 我们正在开发一个Java命令行应用程序,我们希望从许多外部(属性)文件中应用数据驱动的测试,并允许一些知识渊博的用户在不触及Java代码库的情况下添加测试。 Of course, we want to assure that each time we run the app it starts from a clean state (ie no static class side effects, clean file environment...). 当然,我们希望确保每次运行应用程序时都从干净状态开始(即没有静态类副作用,清理文件环境......)。 Here are some options: 以下是一些选项:

(a) Run all tests in a single method in a single JUnit class, calling the app's main() method: (a)在单个JUnit类中的单个方法中运行所有测试,调用app的main()方法:

@Test
public void appTest () {
    <get all properties files>
    <for each property file>
        String[] args = <construct command line from property file>
        MyApp.main (args);
        <test result>
     ...
}

does not work as the whole thing is running in a single JVM. 因为整个事情在单个JVM中运行,所以不起作用。

(b) Run all tests in a single method forking the app: (b)用一种方法运行所有测试,分叉应用程序:

@Test
public void appTest () {
    <get all properties files>
    <for each property file>
        String[] args = <construct command line from property file>
        <fork app with args>
        <test result>
     ...
}

does give us separate JVMs but JUnit (and Surefire) don't know about the individual tests so reporting is pretty useless. 确实为我们提供了单独的JVM,但JUnit(和Surefire)不了解各个测试,因此报告非常无用。

(c) One test per JUnit class: (c)每个JUnit类一次测试:

public class MyAppTest1 {
private static final String PROP_FILE = "src/test/resources/myapp/myapp1.properties
@Test
public void appTest () {
       String[] args = <construct command line from PROP_FILE>
        MyApp.main (args);
        <test result> 
}

} }

This works but it's cumbersome and repetitive and you have to add a class for every test. 这有效,但它很麻烦且重复,你必须为每个测试添加一个类。

(d) JUnit parameterized tests (@RunWith(Parameterized.class) are not useful as they run in the same JVM. (d)JUnit参数化测试(@RunWith(Parameterized.class)没有用,因为它们在同一个JVM中运行。

(e) Surefire parallelization is WITHIN a JVM. (e)Surefire并行化是在JVM中。

We are obviously missing something here, as our testing situation is not unusual! 我们在这里显然遗漏了一些东西,因为我们的测试情况并不罕见! Any suggestions much appreciated. 任何建议非常感谢。

At least a partial answer: Surefire supports forking every test case. 至少部分答案:Surefire支持分配每个测试用例。 Try the following in your surefire configuration section: 在您的surefire配置部分中尝试以下操作:

<reuseForks>false<reuseForks>
<forkCount>1<forkCount>

Read more about that on http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html 有关详情,请访问http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html

For the general problem with "data driven tests", best would be a custom Junit runner. 对于“数据驱动测试”的一般问题,最好的是自定义Junit运行程序。 This is not as hard as you might think, just take a look at the runners package in the Junit sourcecode https://github.com/junit-team/junit/tree/master/src/main/java/org/junit/runners and reimplement/extend the Parameterized Runner using external data. 这并不像你想象的那么难,只需看一下Junit源代码中的runners包https://github.com/junit-team/junit/tree/master/src/main/java/org/junit/运行者并使用外部数据重新实现/扩展参数化运行器。

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

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