java 测试前设置TestNG的输出目录

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

Set output directory of TestNG before test

javamaventestng

提问by Student

I am running some TestNG tests with Eclipse, using XML files (right click, run as TestNG suite). I use maven only for dependencies, not for running tests. Is there a way to set the output directory in the code?

我正在使用 Eclipse 运行一些 TestNG 测试,使用 XML 文件(右键单击,作为 TestNG 套件运行)。我仅将 maven 用于依赖项,而不用于运行测试。有没有办法在代码中设置输出目录?

For example

例如

System.out.println(ctx.getOutputDirectory());

prints the current output directory, which by default, is test-output. But why isn't there a setOutputDirectory? I would like to set the output directory in the @BeforeTest method to a folder that I set in the testng.xml file.

打印当前输出目录,默认情况下为 test-output。但是为什么没有 setOutputDirectory 呢?我想将@BeforeTest 方法中的输出目录设置为我在 testng.xml 文件中设置的文件夹。

回答by Piotr Boho

You have setOutputDirectory() available only if you run testng programatically:

仅当您以编程方式运行 testng 时, setOutputDirectory() 才可用:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory("your_directory_here")
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();

http://testng.org/doc/documentation-main.html#running-testng-programmatically

http://testng.org/doc/documentation-main.html#running-testng-programmatically

or you can pass ouputDirectory as command line parameter -d

或者您可以将 ouputDirectory 作为命令行参数传递 -d

http://testng.org/doc/documentation-main.html#running-testng

http://testng.org/doc/documentation-main.html#running-testng

Eclipse takes default directory:

Eclipse 采用默认目录:

public class TestNG {

/** The default name of the result's output directory (keep public, used by     Eclipse). */
public static final String DEFAULT_OUTPUTDIR = "test-output";

回答by Student

Found the answer.

找到了答案。

@BeforeTest
public void setup(ITestContext ctx) {
    TestRunner runner = (TestRunner) ctx;
    runner.setOutputDirectory("/Path/To/Store/Output");
}