我在BaseClass中使用@BeforeClass
注释方法。我试图在运行时中获取当前执行类(Child类到BaseClass)的名称。
我们是否有任何机制可以在运行时从其Baseclass获取类名?
当我尝试运行单个类时,我能够获取类名,但在运行多个类时遇到问题。为报告中的所有类获取相同的类名。
答案 0 :(得分:0)
您可以参考此链接 How can I get the classname at runtime, but only the classname?
如果你想获得超类名,你可以使用getSuperClass()。
答案 1 :(得分:0)
您可以在此处使用TestNg ITestContext对象:
Class x = ctx.getSuite()。getAllMethods()。listIterator.next.getRealClass(); //然后将其作为字符串
字符串名称= x.getName()。substring(x.getName()。lastIndexOf(“。”)+ 1);
答案 2 :(得分:0)
HashMap<String, HashMap<String, Boolean>> games = new HashMap<>();
HashMap<String, Boolean> values = new HashMap<>();
values.put("IN_LOBBY", true);
values.put("IN_MID_GAME", false);
values.put("IN_DEATHMATCH", false);
games.put("String", values);
System.out.println(games.get("String").get("IN_MID_GAME"));
或者您可以通过以下方式检索类本身:
public String getClassName(ITestContext iTestContext) {
return iTestContext.getCurrentXmlTest().getClasses().stream()
.findFirst().get().getName();
}