TestNG @Test 方法在功能级别运行,如何在场景级别运行

Posted

技术标签:

【中文标题】TestNG @Test 方法在功能级别运行,如何在场景级别运行【英文标题】:TestNG @Test method running at feature level, how to run at scenario level 【发布时间】:2022-01-17 11:24:15 【问题描述】:

我想在功能文件的场景级别(就在每个场景之后)执行@AfterMethod。但是 @AfterMethod 在功能文件的功能级别(在所有场景之后)执行。 我怎样才能做到这一点。 下面是我的代码 sn-p。

    跑步者类。

    @SuppressWarnings("未使用") @CucumberOptions(features = "Features", 胶水 = "com.dell.clouddam.stepdefinitions" , 干运行=假, 插件 = “com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html”,“漂亮”, “json:target/cucumber-reports/Cucumber.json”,单色=真) 公共类 MyTestRunner

     private TestNGCucumberRunner testNGCucumberRunner;
    
     private DriverFactory driverFactory;    
     private WebDriver driver;   //these are made private because these are specific to this class only.
     private ConfigReader configReader;
     Properties properties;
    
    
     @BeforeSuite(alwaysRun = true)
     public void getProperty() 
         configReader = new ConfigReader();
         properties = configReader.initProp();
         System.out.println("@BeforeClass getpropeety ");
    
     
    
     @BeforeClass(alwaysRun = true)
     public void setUpClass() throws Exception 
         testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
     
    
     @BeforeMethod(alwaysRun = true)
     public void launchBrowser() throws FileNotFoundException, IOException, Exception 
         String browserName = properties.getProperty("environment");
         driver = driverFactory.initDriver(browserName); 
         driver.get(properties.getProperty("author_url"));
         ((RemoteWebDriver) driver).getSessionId();
    
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     
    
    
    
     @DataProvider
     public Object[][] features() 
         return testNGCucumberRunner.provideFeatures();
     
    
     @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
     public void feature(CucumberFeatureWrapper cucumberFeature) 
         testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
     
    
     @AfterMethod(description = "Executes After Each Iteration")
     public void afterMethod(ITestResult res) throws Exception 
         driver.quit();
     
    
     @AfterTest(alwaysRun = true)
     public void afterTest() throws Throwable 
    
     
    
     @AfterClass(alwaysRun = true)
     public void tearDownClass() throws Throwable 
         testNGCucumberRunner.finish();
     
    

    DriverFactory.java

    公共类 DriverFactory

    公共静态 WebDriver 驱动程序; HashMap moonConfig = new HashMap();

    私有 ConfigReader configReader=new ConfigReader();; 属性properties = configReader.initProp();

    public WebDriver initDriver(String environment) throws FileNotFoundException, IOException, Exception System.out.println("浏览器值为-" + 环境);

     if (environment.equals("chrome")) 
         WebDriverManager.chromedriver().setup();
             driver = new ChromeDriver();
     
    
     else if (environment.equals("firefox")) 
         WebDriverManager.firefoxdriver().setup();
         driver = new FirefoxDriver();
     
    
     else if (environment.equals("safari")) 
         driver = new SafariDriver();
      
     else 
         System.out.println("Please select the correct browser value");
     
     getDriver().manage().deleteAllCookies();
     getDriver().manage().window().maximize();
     return getDriver();
    

    公共静态同步 WebDriver getDriver() 回程司机;

    Pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sample</groupId>
     <artifactId>SampleDAM</artifactId>
     <version>1.0</version>
     <dependencies>
         <dependency>
             <groupId>io.github.bonigarcia</groupId>
             <artifactId>webdrivermanager</artifactId>
             <version>4.2.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
         <dependency>
             <groupId>io.rest-assured</groupId>
             <artifactId>rest-assured</artifactId>
             <version>4.2.0</version>
         </dependency>
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.17</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-scratchpad</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml-schemas</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-excelant</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.xmlbeans</groupId>
             <artifactId>xmlbeans</artifactId>
             <version>2.6.0</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-collections4</artifactId>
             <version>4.1</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>3.12.0</version>
         </dependency>
         <dependency>
             <groupId>com.paulhammant</groupId>
             <artifactId>ngwebdriver</artifactId>
             <version>1.1.6</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-remote-driver</artifactId>
             <version>2.48.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.github.jesg/phantomjsdriver -->
         <dependency>
             <groupId>com.github.jesg</groupId>
             <artifactId>phantomjsdriver</artifactId>
             <version>2.0.0</version>
         </dependency>
         <dependency>
             <groupId>com.saucelabs</groupId>
             <artifactId>sauce_bindings</artifactId>
             <version>1.0.0</version>
         </dependency>
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <version>6.14.3</version>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>2.53.0</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm</artifactId>
             <version>1.2.5</version>
             <type>pom</type>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1.1</version>
         </dependency>
         <dependency>
             <groupId>org.json</groupId>
             <artifactId>json</artifactId>
             <version>20180130</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-junit</artifactId>
             <version>1.2.5</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-core</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-html</artifactId>
             <version>0.2.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-java</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm-deps</artifactId>
             <version>1.0.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>gherkin</artifactId>
             <version>2.12.2</version>
         </dependency>
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-all</artifactId>
             <version>1.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-picocontainer</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-testng</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>com.aventstack</groupId>
             <artifactId>extentreports</artifactId>
             <version>3.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.freemarker</groupId>
             <artifactId>freemarker</artifactId>
             <version>2.3.26-incubating</version>
         </dependency>
         <dependency>
             <groupId>com.vimalselvam</groupId>
             <artifactId>cucumber-extentsreport</artifactId>
             <version>3.0.1</version>
         </dependency>
     </dependencies>
    
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>3.0.0-M5</version>
                 <configuration>
                     <parallel>methods</parallel>
                     <useUnlimitedThreads>true</useUnlimitedThreads>
                     <suiteXmlFiles>
                         <suiteXmlFile>CloudDAM.xml</suiteXmlFile>
                     </suiteXmlFiles>
                 </configuration>
             </plugin>
         </plugins>
     </build>
    

【问题讨论】:

【参考方案1】:

对于黄瓜,您需要使用 Cucumber-jvm 的注释,例如。 (io.cucumber.java.After) @After, @Before - 在每个场景之前和之后运行。没有支持特征结束的注释。有 @AfterAll - 它在所有场景之后运行。如果你想要更精细的控制,你需要实现 ConcurrentEventListener。

【讨论】:

以上是关于TestNG @Test 方法在功能级别运行,如何在场景级别运行的主要内容,如果未能解决你的问题,请参考以下文章

TestNG基础教程 - TestNG.xml中的测试级别和常用注解执行顺序

如何在 TestNG 单元测试类中分配类级别数据

如何使用 maven mvn test 命令行运行动态 testng.xml?

JUnit 4 Vs TestNG比较

TestNg注解使用

testNG 多线程测试(xml文件实现)