本文介绍了如何用eclipse运行testng工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 eclipse 2018-09 (4.9.0) 和 testng 插件(版本 6.14.0.201802161500).我创建了一个 Maven 项目来从教程中学习 testng.我想在 Eclipse 中运行 testng 工厂方法,但运行菜单中没有testng"选项.我如何让它运行工厂?

I am using eclipse 2018-09 (4.9.0) with the testng plugin (version 6.14.0.201802161500). I created a maven project to learn testng from tutorials. I want to run a testng factory method in Eclipse but there is no "testng" option in the run menu. How do I make it run the factory ?

我的代码:

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 运行它.

SOLUTION :Create an xml suite for the above class or method and run that with testng.

例如.我的工厂.xml

Ex. 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>

推荐答案

你看到这种行为的原因是因为 TestNG eclipse 插件会寻找任何测试方法(至少一个用 @Test 注释的方法)code> annotation),然后通过右键单击启用该上下文选项.

The reason why you see this behavior is because the TestNG eclipse plugin looks for any test methods (at-least one method annotated with @Test annotation) in a class before enabling that contextual option via right click.

在工厂的情况下(例如 SimpleTestFactory 的示例代码的样子),没有测试方法.这就是为什么它禁用 Run As >TestNG 测试 选项.

In the case of a factory (like how your sample code for SimpleTestFactory looks like), there are no test methods. That is why it disables the Run As > TestNG test option.

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

So one of the alternatives for this is to basically create a suite xml file, add a reference to SimpleTestFactory and then run it via the suite file.

您也可以在 eclipse TestNG 插件 github 页面 上提出问题并询问是否可以修复.

You could also file an issue on the eclipse TestNG plugin github page and ask if this can be fixed.

理想情况下,@Factory 注释方法也可以被视为一个起点.虽然我不知道它的可行性.

Ideally speaking a @Factory annotated method could be regarded as a starting point as well. I don't know the feasibility of it though.

我知道 IntelliJ 能够做到这一点.

I do know that IntelliJ is able to do this.

这篇关于如何用eclipse运行testng工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:59