如何使用Eclipse运行Testng工厂?

时间:2019-01-06 01:27:08

标签: java eclipse testng

我正在使用带testng插件(版本6.14.0.201802161500)的Eclipse 2018-09(4.9.0)。我创建了一个maven项目,以从教程中学习testng。我想在Eclipse中运行testng工厂方法,但运行菜单中没有“ testng”选项。如何使其在工厂运行?

我的代码:

package com.learn.classes;

import org.testng.annotations.Test;

//run with testng is present for this.
public class SimpleTest {
    @Test
    public void simpleTest() {
        System.out.println("Simple test Method");
    }
}

package com.learn.factory;

import org.testng.annotations.Factory;

import com.learn.classes.SimpleTest;

//run with testng is NOT present for this.
public class SimpleTestFactory {

    @Factory
    public Object[] factoryMethod() {
        return new Object [] {new SimpleTest(), new SimpleTest()};
    }

}

解决方案: 为上述类或方法创建一个xml套件,并使用testng运行它。

例如myfactory.xml

<suite name="Factorty Methods" verbose="1">
    <test name="My factory method">
        <classes>
            <class name="factory.SimpleTestFactory" />
            <methods>
                <include name="factoryMethod" />
            </methods>
        </classes>
    </test>
</suite>

1 个答案:

答案 0 :(得分:2)

您看到此行为的原因是,在通过右键单击启用上下文选项之前,TestNG eclipse插件会在类中查找任何测试方法(至少一个用@Test标注的方法)。

对于工厂而言(例如SimpleTestFactory的示例代码的样子),没有测试方法。这就是为什么它禁用Run As > TestNG test选项的原因。

因此,替代方法之一是基本上创建一个套件xml文件,添加对SimpleTestFactory的引用,然后通过套件文件运行它。

您还可以在eclipse TestNG plugin github page上提出问题,并询问是否可以解决。

理想地讲,@Factory注释方法也可以视为起点。我不知道它的可行性。

我确实知道IntelliJ能够做到这一点。