Testng BeforeSuite 只运行一个测试类
我有以下 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 版本实际上是正确的。这里有什么问题吗?
I have the following 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>
My TestBase class has this:
@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();
}
When i execute the tests, only the second test in the xml, i.e. RestoreActivityTests seems to run and then it exits the suite. The first class in not executed and I see NullPointerException for each of the tests in the first.
I want only one instance of the browser and have multiple tests executed. The testng doc suggests the xml version I have used above is in fact correct. What is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 @BeforeSuite 也有类似的问题,并使用 xml 定义我的测试 - xml 文件中除最后一个之外的所有测试的空指针。 TestNG 使用反射来创建运行测试所需的类 - 在我的案例中,它从 xml 文件反射性地为 xml 中的每个“测试”创建一个新对象。我怀疑你有两门课的情况也是如此。在第一个测试中,空指针是因为 sm 在第一个测试中未初始化(因此您得到空指针),但初始化发生在第二个测试的实例中。
我的解决方案是使我的引用静态(在您的情况下受保护的静态 SeleniumMngr sm;
换句话说,您不需要使用 @BeforeTest 或 @BeforeClass (无论如何都不会按您想要的方式工作,因为将继续实例化 sm 对象)。
I had the similar issue with @BeforeSuite and using xml to define my tests - null pointer for all of my tests in the xml file but the last. TestNG is using reflection to create a the class it needs to run the test - from the xml file it creates a new object reflectively for each 'test' in the xml in my case & I suspect the same for your scenario where you have 2 classes. In the first one the null pointer is because sm is not initialized in the first test (so you get null pointer) but the initialation occurred in the instance for the 2nd test.
My solution was to make my reference static (in your case protected static SeleniumMngr sm;
In otherwords you don't need to use @BeforeTest or @BeforeClass (won't work as you want anyway since will keep instantiating the sm object).
如果您需要在执行每个类之前运行该方法,那么您应该使用
@BeforeClass
@BeforeSuite
将仅执行一次 - 在触发套件之前。If you need the method to run before executing each class, then you should use
@BeforeClass
@BeforeSuite
will be executed only once - before the suite is triggered.