eclipse 在eclipse中传递JUnit命令行参数

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

Passing JUnit command line parameters in eclipse

eclipsecommand-linejunit

提问by Virat Kadaru

I have recently been using junit in eclipse and I am still learning. I know how to pass command line parameters in eclipse, but how do I pass them to a test case in Junit? Also how do I access them?

我最近一直在 eclipse 中使用 junit,我仍在学习。我知道如何在 eclipse 中传递命令行参数,但是如何将它们传递给 Junit 中的测试用例呢?另外我如何访问它们?

回答by Mark

You cannot pass command line arguments to the JUnit test because no main method is run. You will need to use system properties and access these in your test case.

您不能将命令行参数传递给 JUnit 测试,因为没有运行 main 方法。您将需要使用系统属性并在您的测试用例中访问这些属性。

Select your test class in the Package Explorer. Right click and select Run As -> Open Run DialogIn the run dialog there is an Arguments tab where you can specify program and VM arguments. You should be able to enter your system property parameters here.

在包资源管理器中选择您的测试类。右键单击并选择Run As -> Open Run Dialog在运行对话框中有一个 Arguments 选项卡,您可以在其中指定程序和 VM 参数。您应该能够在此处输入您的系统属性参数。

Alternatively, with the desired project as your current one, from the main menu select Run -> Run Configurationsto access the Arguments tab.

或者,将所需项目作为当前项目,从主菜单中选择Run -> Run Configurations访问参数选项卡。

回答by Cukierpudel

I will skip passing as somebody has already replied with that. To access you use:

我会跳过,因为有人已经回答过了。要访问您,请使用:

System.getProperty("propert.name.here");

(returns String)

(返回字符串)

回答by Capital Terefe

In this example I am passing an argument webDriveras firefoxDriverin the run configuration window:

在这个例子中,我传递一个参数webDriverfirefoxDriver在运行配置窗口:

Example

例子

回答by Amir

Probably you have figured this out, but when compiled and if using ANT or MVN, you can pass arguments to your JUNIT or TestNG from inside the POM.XML file.

可能您已经明白了这一点,但是在编译时并且如果使用 ANT 或 MVN,您可以从 POM.XML 文件内部将参数传递给您的 JUNIT 或 TestNG。

 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.4.3</version>
  <configuration>
    <forkMode>${test.junit.forkMode}</forkMode>
    <skip>${test.junit.skip}</skip>
    <argLine>${test.junit.argLine}</argLine>
    <jvm>${jdk.compiler.path}/binjava</jvm>
  </configuration>
</plugin>