From 35dbc1f4979fdbbe80330e84b42a9349f4a64ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 28 Nov 2020 22:42:25 +0800 Subject: [PATCH] also look current thread for test case name --- .../testable/core/util/TestableUtil.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java index 0591449..bc85364 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -41,13 +41,11 @@ public class TestableUtil { * @return method name */ public static String currentTestCaseName(String testClassName) { - StackTraceElement[] stack = getMainThread().getStackTrace(); - for (int i = stack.length - 1; i >= 0; i--) { - if (stack[i].getClassName().equals(testClassName)) { - return stack[i].getMethodName(); - } + String testCaseName = findFirstMethodFromTestClass(testClassName, getMainThread().getStackTrace()); + if (testCaseName.isEmpty()) { + return findFirstMethodFromTestClass(testClassName, Thread.currentThread().getStackTrace()); } - return ""; + return testCaseName; } /** @@ -69,6 +67,15 @@ public class TestableUtil { return ""; } + private static String findFirstMethodFromTestClass(String testClassName, StackTraceElement[] stack) { + for (int i = stack.length - 1; i >= 0; i--) { + if (stack[i].getClassName().equals(testClassName)) { + return stack[i].getMethodName(); + } + } + return ""; + } + private static String getRealClassName(Class testClass) { String className = testClass.getName(); int posOfInnerClass = className.lastIndexOf('$');