【问题标题】:How do I organize structure of my testSuite using beforeClass, afterClass, afterSuite in testNG?如何在 testNG 中使用 beforeClass、afterClass、afterSuite 组织我的 testSuite 的结构?
【发布时间】:2015-02-26 05:38:20
【问题描述】:

我对在 testNG 中使用注释 @beforeClass、afterClass、beforeSuite、afterSuite 感到困惑。

我了解以下测试结构:

package mytestNG.learning.it ;

public class sample_not_working {
  @Test
  //take action - click on links, input data etc
  public void main() {
  }
  @BeforeMethod
  //do stuff like setup browser etc
  public void beforeMethod() {
  }

  @AfterMethod
  //close browser 
  public void afterMethod() {
  }
}

但是你在课前、课后和测试中做什么呢?它是什么文件? 是运行其他类的类吗?

Next、afterSuite、beforeSuite 和 @test

  public class sample_not_working {
  @Test

  public void main() {
  //WHAT KINDA CODE YOU PUT HERE?
  }
  @BeforeSuite
  //WHAT KINDA CODE YOU PUT HERE?
  public void beforeMethod() {
  }
  @AfterSuite
  public void afterMethod() {
  //WHAT KINDA CODE YOU PUT HERE?
  }
}

我的问题是关于语义、意义,而不是实际代码。我阅读了 testNG 文档 - 没有帮助。

【问题讨论】:

    标签: unit-testing testing webdriver testng


    【解决方案1】:

    如果您有以下 testng.xml:

    <suite name="MySuite">
      <test name="MyBigTest">
        <classes>
          <class name="test.sample.firstTestClass"/>
            <methods>
              <include name="a" />
              <include name="b" />
            </methods>
          <class name="test.sample.secondTestClass"/>
            <methods>
              <include name="c" />
              <include name="d" />
            </methods>
        </classes>
      </test>
    </suite>
    

    那么……

    • @BeforeSuite 和 @AfterSuite 将在 MySuite 之前/之后运行
    • @BeforeTest 和 @AfterTest 将在 MyBigTest 之前/之后运行
    • @BeforeClass 和 @AfterClass 将在 firstTestClasssecondTestClass 之前/之后运行
    • @BeforeMethod 和 @AfterMethod 将在 a、b、cd 方法之前/之后运行

    例如,您可以在@BeforeSuite 中设置您的环境,或者您可以使用@BeforeTest 打开浏览器并在测试方法结束时使用@AfterTest 等关闭它...

    【讨论】:

      【解决方案2】:

      这实际上取决于您要测试什么以及如何进行测试。我通常将驱动程序放在@BeforeSuite 中,然后在@AfterSuite 中退出它们。

      在@AfterClass(如果我使用它)中,我正在为整个班级[关于字段]放置日志,例如更改某些变量的值。

      在@BeforeClass 中,我有时会初始化变量/字段。

      但最后,这些注释是为了帮助您,它们的创建是为了轻松执行您的想法。

      【讨论】:

        【解决方案3】:

        您可能想使用下面的示例来理解它。它没有@AfterClass / @BeforeClass。但它有助于理解结构。

        package com.kagrana.base;
        
        import java.io.IOException;
        import org.openqa.selenium.WebDriver;
        import org.testng.ITestContext;
        import org.testng.ITestResult;
        import org.testng.annotations.AfterMethod;
        import org.testng.annotations.AfterSuite;
        import org.testng.annotations.BeforeMethod;
        import org.testng.annotations.BeforeSuite;
        import org.testng.annotations.Optional;
        import org.testng.annotations.Parameters;
        
        import com.kagrana.DTO.TestCase;
        import com.kagrana.DTO.WebDriverConfig;
        import com.kagrana.util.Log;
        
        public abstract class BaseActions {
            protected WebDriver driver;
            protected static Log log;
            protected WebDriverConfig config;
            protected String baseURL;
            protected TestCase testCase;
            private static int testCaseCount = 1;
        
            /**
             * This gets invoked even before suite starts.
             * @param ReportLocation
             */
            @Parameters({"ReportLocation"})
            @BeforeSuite
            public void beforeSuite(@Optional String ReportLocation){
                log = new Log();
            }
            @BeforeMethod
            @Parameters({ "remoteURL", "baseURL", "OS", "browser",
                    "version", "internal" })
            public void beforeTest(String remoteURL, String baseURL,
                    String OS, String browser, String version, String internal)
                    throws IOException {
                this.testCase = new TestCase();
                this.testCase.setExecutionEnvironment("{browser:"+browser+",browserVersion:"+version+",OperatingSystem:"+OS+"}");
                this.testCase.setParentURL(baseURL);
                this.testCase.setTestCaseId("KT"+testCaseCount++);
                this.testCase.setScreenshotDirectory(log.getReportDirectory()+"\\images");
                config = new WebDriverConfig();
                config.setRemoteURL(remoteURL);
                this.baseURL = baseURL;
                config.setOS(OS);
                config.setBrowserName(browser);
                config.setBrowserVersion(version);
                config.setIntenal(Boolean.parseBoolean(internal));
                driver = xRemoteWebDriver.getInstance(config, log);
                driver.manage().window().maximize();
                driver.get(this.baseURL);
            }
        
            @AfterMethod
            public void afterTest(ITestResult itr) {
                testCase.setExecutionTime((itr.getEndMillis() - itr.getStartMillis()));
                testCase.setTestCaseName(itr.getName());
                log.addTestCase(testCase);
                try {
                    driver.close();
                } catch (Exception ignore) {
        
                }
                try {
                    driver.quit();
                } catch (Exception ignore) {
        
                }
            }
            @AfterSuite
            public void afterSuite(ITestContext itc) throws IOException{
                log.setTestSuiteName(itc.getSuite().getName());
                log.writeReport();
                log.zipAndEmailFile();
            }
        
        }
        

        来源:https://github.com/selenium-webdriver-software-testing/kspl-selenium-helper/blob/master/src/main/java/in/mayurshah/base/BaseActions.java

        【讨论】:

          【解决方案4】:

          大多数时候人们使用@BeforeMethod@AfterMethod 来设置和拆除驱动程序。

          对于这个和其他常见的自动化需求,use Selenium 有可用的 TestNG 扩展,TestNG 提供了这样的内置实现。例如,抽象driver management outside the code 的框架之一,因此您的测试和相关代码整洁干净,您无需担心驱动程序管理和其他常见的Web 或移动自动化自动化需求。

          【讨论】:

            猜你喜欢
            • 2021-05-26
            • 1970-01-01
            • 2016-12-03
            • 2016-08-15
            • 2011-05-13
            • 1970-01-01
            • 2010-12-27
            • 1970-01-01
            • 2015-06-27
            相关资源
            最近更新 更多