2121import io .qameta .allure .util .ResultsUtils ;
2222import org .testng .IAttributes ;
2323import org .testng .IClass ;
24+ import org .testng .IConfigurationListener ;
2425import org .testng .IInvokedMethod ;
2526import org .testng .IInvokedMethodListener2 ;
2627import org .testng .ISuite ;
7677 "PMD.ExcessiveImports" , "PMD.TooManyMethods" , "PMD.GodClass" ,
7778 "ClassFanOutComplexity" , "ClassDataAbstractionCoupling" , "PMD.ExcessiveClassLength"
7879})
79- public class AllureTestNg implements ISuiteListener , ITestListener , IInvokedMethodListener2 {
80+ public class AllureTestNg implements
81+ ISuiteListener ,
82+ ITestListener ,
83+ IInvokedMethodListener2 ,
84+ IConfigurationListener {
8085
8186 private static final String ALLURE_UUID = "ALLURE_UUID" ;
8287
@@ -193,14 +198,31 @@ public void onAfterClass(final ITestClass testClass) {
193198 }
194199
195200 @ Override
196- @ SuppressWarnings ({"Indentation" , "PMD.ExcessiveMethodLength" , "deprecation" })
197201 public void onTestStart (final ITestResult testResult ) {
198202 Current current = currentTestResult .get ();
199203 if (current .isStarted ()) {
200204 current = refreshContext ();
201205 }
202206 current .test ();
207+ final String uuid = current .getUuid ();
203208 final String parentUuid = getUniqueUuid (testResult .getTestContext ());
209+
210+ startTestCase (testResult , parentUuid , uuid );
211+
212+ Optional .of (testResult )
213+ .map (ITestResult ::getMethod )
214+ .map (ITestNGMethod ::getTestClass )
215+ .map (classContainerUuidStorage ::get )
216+ .ifPresent (testClassContainerUuid -> getLifecycle ().updateTestContainer (
217+ testClassContainerUuid ,
218+ container -> container .getChildren ().add (uuid )
219+ ));
220+ }
221+
222+ @ SuppressWarnings ({"Indentation" , "PMD.ExcessiveMethodLength" , "deprecation" })
223+ protected void startTestCase (final ITestResult testResult ,
224+ final String parentUuid ,
225+ final String uuid ) {
204226 final ITestNGMethod method = testResult .getMethod ();
205227 final ITestClass testClass = method .getTestClass ();
206228 final List <Label > labels = new ArrayList <>();
@@ -223,7 +245,7 @@ public void onTestStart(final ITestResult testResult) {
223245 labels .addAll (getLabels (testResult ));
224246 final List <Parameter > parameters = getParameters (testResult );
225247 final TestResult result = new TestResult ()
226- .setUuid (current . getUuid () )
248+ .setUuid (uuid )
227249 .setHistoryId (getHistoryId (method , parameters ))
228250 .setName (getMethodName (method ))
229251 .setFullName (getQualifiedName (method ))
@@ -235,17 +257,7 @@ public void onTestStart(final ITestResult testResult) {
235257 .setLabels (labels );
236258 processDescription (getClass ().getClassLoader (), method .getConstructorOrMethod ().getMethod (), result );
237259 getLifecycle ().scheduleTestCase (parentUuid , result );
238- getLifecycle ().startTestCase (current .getUuid ());
239-
240- final String uuid = current .getUuid ();
241- Optional .of (testResult )
242- .map (ITestResult ::getMethod )
243- .map (ITestNGMethod ::getTestClass )
244- .map (classContainerUuidStorage ::get )
245- .ifPresent (testClassContainerUuid -> getLifecycle ().updateTestContainer (
246- testClassContainerUuid ,
247- container -> container .getChildren ().add (uuid )
248- ));
260+ getLifecycle ().startTestCase (uuid );
249261 }
250262
251263 @ Override
@@ -271,12 +283,18 @@ public void onTestFailure(final ITestResult result) {
271283 }
272284
273285 current .after ();
286+ final String uuid = current .getUuid ();
287+
274288 final Throwable throwable = result .getThrowable ();
275289 final Status status = getStatus (throwable );
290+ stopTestCase (uuid , throwable , status );
291+ }
292+
293+ protected void stopTestCase (final String uuid , final Throwable throwable , final Status status ) {
276294 final StatusDetails details = getStatusDetails (throwable ).orElse (null );
277- getLifecycle ().updateTestCase (current . getUuid () , setStatus (status , details ));
278- getLifecycle ().stopTestCase (current . getUuid () );
279- getLifecycle ().writeTestCase (current . getUuid () );
295+ getLifecycle ().updateTestCase (uuid , setStatus (status , details ));
296+ getLifecycle ().stopTestCase (uuid );
297+ getLifecycle ().writeTestCase (uuid );
280298 }
281299
282300 @ Override
@@ -293,10 +311,7 @@ public void onTestSkipped(final ITestResult result) {
293311 createTestResultForTestWithoutSetup (result );
294312 }
295313 current .after ();
296- final StatusDetails details = getStatusDetails (result .getThrowable ()).orElse (null );
297- getLifecycle ().updateTestCase (current .getUuid (), setStatus (Status .SKIPPED , details ));
298- getLifecycle ().stopTestCase (current .getUuid ());
299- getLifecycle ().writeTestCase (current .getUuid ());
314+ stopTestCase (current .getUuid (), result .getThrowable (), Status .SKIPPED );
300315 }
301316
302317 private void createTestResultForTestWithoutSetup (final ITestResult result ) {
@@ -441,6 +456,26 @@ public void afterInvocation(final IInvokedMethod method, final ITestResult testR
441456 }
442457 }
443458
459+ @ Override
460+ public void onConfigurationSuccess (final ITestResult itr ) {
461+ //do nothing
462+ }
463+
464+ @ Override
465+ public void onConfigurationFailure (final ITestResult itr ) {
466+ final String uuid = UUID .randomUUID ().toString ();
467+ final String parentUuid = UUID .randomUUID ().toString ();
468+
469+ startTestCase (itr , parentUuid , uuid );
470+ stopTestCase (uuid , itr .getThrowable (), getStatus (itr .getThrowable ()));
471+ //do nothing
472+ }
473+
474+ @ Override
475+ public void onConfigurationSkip (final ITestResult itr ) {
476+ //do nothing
477+ }
478+
444479 protected String getHistoryId (final ITestNGMethod method , final List <Parameter > parameters ) {
445480 final MessageDigest digest = getMd5Digest ();
446481 final String testClassName = method .getTestClass ().getName ();
0 commit comments