【问题标题】:Testng BeforeSuite runs only one test classTestng BeforeSuite 只运行一个测试类
【发布时间】:2011-06-27 19:08:12
【问题描述】:

我有以下 testng.xml

<test name="CommonReportingTests" preserve-order="true">
<classes>
   <class name="com.blah.ReportRunTests">
        <methods>
            <include name="login" />
            <include name="checkHomeTab" />
            <include name="checkReportsTab" />
             <include name="checkReportsExist" />
             <include name="reportsCleanup" />               
        </methods>
   </class>

   <class name="com.blah.RestoreActivityTests">
        <methods>
            <include name="login" />
            <include name="checkHomeTab" />
            <include name="checkReportsTab" />
             <include name="checkReportsExist" />             
             <include name="expandReportsTabAndClickRestoreActivity" />    
        </methods>
   </class>
</classes>

我的 TestBase 类有这个:

    @Parameters({ "webconsoleStartupURL" })
@BeforeSuite(description = "Perform class setup tasks")
public void beforeClass(final String webconsoleStartupURL)
        throws ClassNotFoundException, InstantiationException,
        IllegalAccessException {
    sm = new SeleniumMgr(webconsoleStartupURL);
    sm.startSelenium();
}

@AfterSuite(description = "Perform class teardown tasks")
public void afterClass() {
    sm.stopSelenium();
}

当我执行测试时,只有 xml 中的第二个测试,即 RestoreActivityTests 似乎运行,然后它退出套件。第一个类未执行,我在第一个测试中看到 NullPointerException。

我只想要一个浏览器实例并执行多个测试。 testng 文档表明我上面使用的 xml 版本实际上是正确的。这里有什么问题?

【问题讨论】:

    标签: xml selenium testng suite


    【解决方案1】:

    我在 @BeforeSuite 和使用 xml 定义我的测试时遇到了类似的问题 - xml 文件中所有测试的空指针,但最后一个。 TestNG 正在使用反射来创建一个运行测试所需的类 - 在我的情况下,它从 xml 文件中为 xml 中的每个“测试”反射地创建一个新对象,我怀疑对于你有 2 个类的场景也是如此.在第一个中,空指针是因为 sm 在第一次测试中没有初始化(所以你得到空指针)但是初始化发生在第二次测试的实例中。

    我的解决方案是让我的参考静态(在你的情况下受保护的静态 SeleniumMngr sm;

    换句话说,您不需要使用@BeforeTest 或@BeforeClass(无论如何都不会按您的意愿工作,因为会继续实例化 sm 对象)。

    【讨论】:

      【解决方案2】:

      如果您需要在执行每个类之前运行该方法,那么您应该使用 @BeforeClass @BeforeSuite 将只执行一次 - 在触发套件之前。

      【讨论】:

      • 不,使用@BeforeClass 将再次实例化 Selenium,即每个测试类我都会看到一个新实例。我累了,这行得通,但这不是我想要的。我希望浏览器只出现一次,完成我在 xml 中指定的多个类中的所有测试,然后最终退出
      • 你为什么说“不”? AJ 对@BeforeSuite 的描述完全正确。回到您的问题,您可能希望使用 @BeforeTest 并将您的类放在单独的 标记中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      相关资源
      最近更新 更多