【问题标题】:how to call testng.xml from java main method?如何从 java main 方法调用 testng.xml?
【发布时间】:2014-06-09 23:19:59
【问题描述】:

我创建了 testng.xml 文件。 有什么方法可以从 java main 方法运行这个文件?

类似的东西-

Class test {
  public static void main ( String [ ] args) 
  {
    Run(testng.xml);
  }
}

【问题讨论】:

  • 您能否解释一下您通过运行 testng.xml 想要实现的目标?
  • 在 Eclipse 中运行测试我正在运行 testng.xml,方法是右键单击它,然后选择作为 testng 套件运行的选项。我想做一个罐子,想在一定的时间间隔后运行它。这就是为什么尝试从 main 方法调用 testng.xml

标签: selenium-webdriver


【解决方案1】:

您可以直接从命令行运行 testng,可能在其上制作一个 bat 文件或使用 jenkins 来触发它。参考这里

如果你想要它在main中,那么你可以尝试

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("c:/tests/testng1.xml");//path to xml..
suites.add("c:/tests/testng2.xml");
testng.setTestSuites(suites);
testng.run();

【讨论】:

  • 使用 testng.xml 导出可运行 jar 文件并从 CMD 提示符尝试,总是找不到 xml。但是当我明确使用类名时,能够从 CMD 运行测试。你能帮我解决这个问题吗? [注:没用过ANT、MAVEN]
  • 您好 niharika,我认为第一种方法中缺少 URL。您打算提供在 jar 文件之上创建 bat 文件的 URL,但没有插入链接。我需要那个解决方案。你能提供那个链接吗?
【解决方案2】:

请尝试以下代码,并确保您的 jar 清单文件中添加了 testNG jar。

public static void main(String[] args)
{
    org.testng.TestNG.main(args);
}

现在您可以将与 testNG jar 文件相同的所有参数传递给您的 jar。

例如

java -jar yourjar.jar testng.xml

java -jar yourjar.jar -testclass org.test.xyz.java

【讨论】:

    【解决方案3】:

    这对我有用。更多详情here.

    // Create object of TestNG Class
    TestNG runner=new TestNG();
    
    // Create a list of String 
    List<String> suitefiles=new ArrayList<String>();
    
    // Add xml file which you have to execute
    suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");
    
    // now set xml file for execution
    runner.setTestSuites(suitefiles);
    
    // finally execute the runner using run method
    runner.run();
    

    希望这会有所帮助!

    【讨论】:

    • 当我尝试运行它时没有找到 ClassPath。我的 testng.xml 包含来自同一项目的不同包的所有类的列表,它们存在于 /test/java 文件夹中。
    【解决方案4】:

    以上所有答案都像一个魅力,但有一个限制 它会运行,除非你删除 pom.xml 中的 scope 标记

    在 pom.xml 中会存在 testNg 的 maven 依赖关系 会有一个范围标签将我们的 jar 限制为只能进行测试

    删除那个

    【讨论】:

      【解决方案5】:

      我没有收到任何错误,并且 XML 路径是正确的。但它只是以失败的测试用例结束执行。它不打开火狐浏览器。 我在类中使用 testng 注释(即@Before Suite、@Test、@group 等),而它在 Eclipse 中使用 Testng Suite 成功执行。

      <!--  suite name="Example"  parallel="methods" thread-count="2" parallel="false" preserve-order="true"> -->
      <suite name="Example Donate System" verbose='1'>
      
          <test name="Agency"   >
          <packages>
            <package name="com.Donatesystem.AgencyController" />
         </packages>
       </test>
      </suite>
      

      【讨论】:

        猜你喜欢
        • 2013-11-28
        • 2013-05-16
        • 2021-08-30
        • 2021-12-21
        • 1970-01-01
        • 2017-02-13
        • 1970-01-01
        • 2013-12-19
        • 2019-04-21
        相关资源
        最近更新 更多