Set output directory of TestNG before test

11,728

Solution 1

Found the answer.

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

Solution 2

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

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

or you can pass ouputDirectory as command line parameter -d

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

Eclipse takes default directory:

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";
Share:
11,728
Student
Author by

Student

Updated on June 19, 2022

Comments

  • Student
    Student almost 2 years

    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?

    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.

  • Student
    Student over 9 years
    I actually found an answer myself as well. I have tested it, and it works.
  • Piotr Boho
    Piotr Boho over 9 years
    that's good! I forgot about the object injection mechanism in testng methods testng.org/doc/documentation-main.html#dependency-injection
  • John Chesshir
    John Chesshir about 7 years
    @Student A couple of questions: Can you tell me where you found the documentation for TestRunner.setOutputDirectory? And how do you know for sure that the ITestContext object handed to your before method will be a TestRunner object?