猿问

如何用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>


料青山看我应如是
浏览 151回答 1
1回答

拉丁的传说

之所以看到此行为,是因为TestNG eclipse插件会在@Test通过右键单击启用该上下文选项之前,在一个类中查找任何测试方法(至少一个带有注释的方法)。对于工厂(例如您的示例代码的SimpleTestFactory外观),没有测试方法。这就是为什么它禁用该Run As > TestNG test选项的原因。因此,替代方法之一是基本上创建一个套件xml文件,添加一个引用SimpleTestFactory,然后通过套件文件运行它。您还可以在Eclipse TestNG插件github页面上提交问题,并询问是否可以解决。理想情况下,带@Factory注释的方法也可以视为起点。我不知道它的可行性。我知道IntelliJ可以做到这一点。
随时随地看视频慕课网APP

相关分类

Java
我要回答