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

时间:2015-06-16 13:10:13

标签: java selenium testng

我以编程方式创建了我的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方法都会被执行(这些方法也有完全不同的名称,例如&#34; gssig01&#34;) 尽管我没有包含它们。

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

提前谢谢!

1 个答案:

答案 0 :(得分:1)

试用此代码,它可以正常工作

//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
===============================================

如果您想查看全班:
RunTestNG.java - 测试跑步者类
LoginTest.java - 测试班次