SuperMario
SuperMario

Reputation: 77

Programmatically stop TestNG run

I develop some GUI in which user can start and stop TestNG tests.

Can I anyhow stop TestNG process without creating conditions check in @AfterMethod cause some tests are very long (more than 15 minutes) so @AfterMethod solution is not effective. There is NO requirements to stop tests gracefully.

Are there any solutions in the latest TestNG versions (>7.0)? For example, with the help of IExecutorFactory or ITestRunnerFactory?

References:

Upvotes: 1

Views: 423

Answers (1)

Gautham M
Gautham M

Reputation: 4935

Since you rely on GUI to start and stop the tests, I assume the presence of start and stop methods.

In the start method, you create an ExecutorService which would execute the tests programmatically and in the stop method you could shut down this executor.

Also, note that it is recommended that you do some mechanism to share the ExecutorService between start and stop. Otherwise it would cause trouble if multiple test runs(continuous start operations) are allowed through GUI.

public class MyClass {
    private ExecutorService ex;

    public static void testRunner() {
        TestNG t = new TestNG();
        t.setTestClasses(new Class[] {YourTestClass.class});
        t.run();
    }

    public void start(){
        ex = Executors.newFixedThreadPool(1);
        ex.execute(MyClass::testRunner);        
    }

    public void stop(){
        ex.shutdownNow();
    }

}

Upvotes: 1

Related Questions