【问题标题】:@Test annotation is running first for all the tests in testNG?@Test 注释首先为 testNG 中的所有测试运行?
【发布时间】:2017-12-20 19:34:14
【问题描述】:

我的 testng.xml 下有几个测试用例。我的 testng.xml 如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AutomationPractice" parallel="none">
  <test name="Logins">
    <classes>
      <class name="newMavenProject.Gmail_Login_Firefox"/>
      <class name="newMavenProject.Facebook_Login_Chrome"/>
    </classes>
  </test> <!-- Logins -->
</suite> <!-- AutomationPractice -->

在我的两个测试中,我都有@BeforeTest、@Test 和@AfterTest 注释。当我将 testng.xml 作为 testNGSuite 运行时。我看到浏览器同时为两个测试(@BeforeTest)打开,然后 Gmail_Login 测试将运行 @Test 注释。然后 Facebook_Login_Chrome 将运行其 @Test 注释。然后我的 Gmail_Login 测试将运行 @AfterTest 注释(关闭浏览器),然后我的 Facebook_Login_Chrome 测试将运行 @AfterTest 注释(关闭浏览器)。

如何才能让我的 Gmail_Login 测试完全运行(@BeforeTest、@Test、@AfterTest),然后我的 Facebook_Login_Chrome 测试完全运行(@BeforeTest、@Test、@AfterTest)。

非常感谢!!

【问题讨论】:

  • 你能告诉我们你用来模拟这个问题的代码吗?此外,当您打印出线程 ID 时,您会看到什么? TestNG 不应该并行运行任何东西,除非并且直到它被明确配置。因此,您可能想要添加打印线程 ID 逻辑并共享输出。还请包括您正在使用的 TestNG 版本。今天最新发布的版本是6.13.1
  • 您应该使用在测试方法周围立即运行的 beforemethod 和 aftermethod。为测试标签运行前测和后测。

标签: selenium-webdriver automation testng


【解决方案1】:

&lt;test&gt;标签被执行时,它会运行所有方法,从所有包含的类中注释@BeforeTest

您似乎需要使用 @BeforeClass@AfterClass 注释来注释设置和拆卸方法。

根据TestNG documentation

@BeforeTest: 带注释的方法将在任何测试方法之前运行 属于&lt;test&gt;标签内的类运行。

@AfterTest: 被注解的方法将在所有测试后运行 属于&lt;test&gt; 标签内的类的方法已经运行。

@BeforeClass:注解的方法会在第一次测试之前运行 调用当前类中的方法。

@AfterClass: 被注解的方法将在所有测试后运行 当前类中的方法已运行。

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多