到目前为止,我已经能够在GitHub测试项目中表明可以将正确的个人名称打印到控制台,但是我只需要弄清楚如何在报告上打印它们即可.解决方案 djangofan,不确定您是否找到了一种方法,但是我想出了一种使用您的repro来做到这一点的方法,尽管有点hacky,但是我从这条帖子中抢了下来.在这里,创建属性名称,但不要增加名称.这会导致属性列表增长,然后您会失去有关哪个属性属于哪个测试的上下文.出于所有目的和目的,我们只想指定一个测试名称:@Test( dataProvider = "dp" )public void testSample1( int num, String name, String desc, ITestContext ctx ) { //ctx.setAttribute( "testName" + num, name ); //ctx.setAttribute( "testDesc" + num, desc ); ctx.setAttribute("testName", name); ctx.setAttribute("testDesc", desc); assertTrue( true );}现在创建您的自定义侦听器,在这里我们将设置方法名称等于属性名称,并将其反映在testng-results.xml文件中.注意,我们对"testName"参数名称进行了硬编码. public class CustomListener extends TestListenerAdapter { @Override public void onTestSuccess(ITestResult tr) { setTestNameInXml(tr); super.onTestSuccess(tr); } ... private void setTestNameInXml(ITestResult tr) { try { ITestContext ctx = tr.getTestContext(); Set<String> attribs = ctx.getAttributeNames(); for ( String x : attribs ) { if (x.contains("testName")) { Field method = TestResult.class.getDeclaredField("m_method"); method.setAccessible(true); method.set(tr, tr.getMethod().clone()); Field methodName = BaseTestMethod.class.getDeclaredField("m_methodName"); methodName.setAccessible(true); methodName.set(tr.getMethod(), ctx.getAttribute( x )); break; } } } catch (Exception e) { e.printStackTrace(); } }}这将自动用属性中设置的测试用例名称替换方法名称"列中的实际方法名称.更新testng-results.xml文件不是理想的方法,但这是基于您的代码的最简单的解决方案,而且我认为它将对我有用! I tried following instructions on this thread but they fail to work for me. Basically I need to figure out how to set the TestNG test name, as seen on the HTML report, dynamically from the DataProvider input.I created a GitHub project that re-creates the problem I am experiencing and I am hoping someone can help me solve it? I created a CustomReport class that reveals the "test name" on the HTML report but the test name is not yet shown correctly.To solve this, all I need is to modify the CustomReport class to somehow read the name values from the ITestContext object, after the tests are finished, and report them on the report properly.So far I am able to show in my GitHub test project that I can print out the correct individual names to the console but I just need to figure out how to print them on the report. 解决方案 djangofan, not sure if you found a way or not, but i figured out a way to do it from using your repro, albeit a bit hacky, part of which i snagged from this post.Here, create attribute names, but do not increment the names. That causes the attribute list to grow, and then you lose context of which attribute belongs to which test. For all intents and purposes, we only want to id a single test name:@Test( dataProvider = "dp" )public void testSample1( int num, String name, String desc, ITestContext ctx ) { //ctx.setAttribute( "testName" + num, name ); //ctx.setAttribute( "testDesc" + num, desc ); ctx.setAttribute("testName", name); ctx.setAttribute("testDesc", desc); assertTrue( true );}Now create your custom listener, here we will set the method name equal to the attribute name, and will be reflected in the testng-results.xml file. Note, we are hard-coding "testName" parameter name. public class CustomListener extends TestListenerAdapter { @Override public void onTestSuccess(ITestResult tr) { setTestNameInXml(tr); super.onTestSuccess(tr); } ... private void setTestNameInXml(ITestResult tr) { try { ITestContext ctx = tr.getTestContext(); Set<String> attribs = ctx.getAttributeNames(); for ( String x : attribs ) { if (x.contains("testName")) { Field method = TestResult.class.getDeclaredField("m_method"); method.setAccessible(true); method.set(tr, tr.getMethod().clone()); Field methodName = BaseTestMethod.class.getDeclaredField("m_methodName"); methodName.setAccessible(true); methodName.set(tr.getMethod(), ctx.getAttribute( x )); break; } } } catch (Exception e) { e.printStackTrace(); } }}This will automatically replace the actual method name in the "Method Name" column with the test case name set in the attributes.It is not ideal to update testng-results.xml file, but it was easiest solution based on your code, plus i think it will work for me! 这篇关于从DataProvider输入动态设置TestNG测试名称(如HTML报告所示)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 23:04