gpt4 book ai didi

java - TestNG 运行每个方法并忽略 testng.xml 中包含的方法

转载 作者:行者123 更新时间:2023-12-01 11:25:16 25 4
gpt4 key购买 nike

我以编程方式创建 testng.xml,我想添加我想要运行的每个方法。我现在正在按以下方式进行:

XmlClass myClass = new XmlClass("test.login.LoginTest");
List<XmlInclude> includedMethods = new ArrayList<XmlInclude>();

for (int k = 0; k < 10; k++) {

includedMethods.add(new XmlInclude("golog" + k));
}

myClass.setIncludedMethods(includedMethods);

根据我生成的 testng.xml 文件,看起来它工作正常:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="11" verbose="11" name="Login Test" parallel="tests">
<test name="1" group-by-instances="true">
<classes>
<class name="test.login.LoginTest">
<methods>
<include name="golog1"/>
<include name="golog2"/>
<include name="golog3"/>
<include name="golog4"/>
<include name="golog5"/>
<include name="golog6"/>
<include name="golog7"/>
<include name="golog8"/>
<include name="golog9"/>
</methods>
</class> <!-- test.login.LoginTest -->
</classes>
</test> <!-- 1 -->
</suite> <!-- Login Test -->

问题是,当我的代码在生成 xml 文件后执行时,每个 @Test 方法都会执行(这些方法也具有完全不同的名称,例如“gssig01”) 尽管事实上我没有包含它们。

我的问题是,在包含任何内容之前我是否必须排除所有方法,或者我做错了什么? :)

提前谢谢您!

最佳答案

试试这个代码,它有效

//Create a list which can contain the classes that you want to run including methods.
List<XmlClass> myClasses = new ArrayList<XmlClass> ();
XmlClass xmlclass = new XmlClass("stack1.LoginTest");

List<XmlInclude> includedMethods = new ArrayList<XmlInclude>();

for (int k = 0; k < 5; k++) {

includedMethods.add(new XmlInclude("golog" + k));
}
xmlclass.setIncludedMethods(includedMethods);

myClasses.add(xmlclass);
<小时/>

这是输出,其中我有 7 个方法,但只运行了 4 个

golog1test1
golog2test2
golog3test3
golog4test4

===============================================
StackOverFlow-Answer
Total tests run: 4, Failures: 0, Skips: 0
===============================================

如果您想查看完整类(class):
RunTestNG.java - 测试运行类
LoginTest.java - 测试类

关于java - TestNG 运行每个方法并忽略 testng.xml 中包含的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com