测试前设置TestNG的输出目录

Stu*_*ent 3 java testng maven

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

例如

System.out.println(ctx.getOutputDirectory());
Run Code Online (Sandbox Code Playgroud)

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

Pio*_*oho 11

只有以编程方式运行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();
Run Code Online (Sandbox Code Playgroud)

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

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

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

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";
Run Code Online (Sandbox Code Playgroud)


Stu*_*ent 5

找到了答案。

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