Генерирование ExtentReport огурца с TestNG при выполнении нескольких потоков - PullRequest
0 голосов
/ 09 ноября 2018

Я параллельно запускаю тесты на мобильных устройствах с использованием Cucumber и TestNG. Мой класс бегуна TestNG приведен ниже.

@CucumberOptions(
        features="src/test/resources/features",
        glue={"org.cucumber.stepdefs"},
        plugin = {
        "com.cucumber.listener.ExtentCucumberFormatter:" }, monochrome = true)

public class TestRunner extends BaseTest {

    private static TestNGCucumberRunner testRunner;

    @BeforeClass
    public void setUP() {   
        System.out.println("Thread = " + Thread.currentThread().getId() + " - object hash = " + this.hashCode());
        testRunner = new TestNGCucumberRunner(TestRunner.class);
        ExtentProperties extentProperties = ExtentProperties.INSTANCE;
        extentProperties.setReportPath("output/" + this.hashCode() + "-report.html");
    }

    @Test(description="Tests",dataProvider="features")
    public void setUpClass(CucumberFeatureWrapper cFeature) {
        testRunner.runCucumber(cFeature.getCucumberFeature());
    }

    @DataProvider(name="features")
    public Object[][] getFeatures() {
        return testRunner.provideFeatures();
    }

    @AfterClass
    public static void teardown() {
        testRunner.finish();
    }

}

Что работает должным образом - мои тесты Cucumber выполняются параллельно в отдельных потоках для каждого подключенного устройства (определено в моем файле testng.xml).

Я использую ExtentReports для генерации отчетов после каждого запуска, хотя в данный момент я получаю только один отчет, где мне нужен отдельный отчет для каждого выполняющегося потока.

Я не могу понять, почему это происходит. Я добавил распечатки на консоли, чтобы проверить, существует ли мгновение класса TestRunner для каждого запущенного потока, и действительно есть - мой журнал консоли после выполнения содержит следующее (при условии, что подключено 2 мобильных устройства):

Thread = 33 - object hash = 923219673
Thread = 32 - object hash = 280884709

Так что это работает, как я и ожидал, за исключением того, что когда создается отчет, создается только 1 отчет, содержащий все данные теста. Я предполагал, что, поскольку я устанавливаю путь к отчету с уникальным именем файла в каждом потоке в методе setUp(), следует создать отчет для каждого потока?

Похоже, что когда дело доходит до генерации отчета, плагин Cucumber ExtendReport ожидает завершения всех тестов и генерирует массовый отчет по умолчанию. Это правильно? И если так, как я могу изменить это?

Ниже приведена основная часть моего pom.xml файла для дополнительной информации о конфигурации.

<properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>1.2.5</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm</artifactId>
         <version>4.2.0</version>
         <type>pom</type>
      </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-testng</artifactId>
         <version>1.2.5</version>
         <scope>compile</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-picocontainer</artifactId>
         <version>1.2.5</version>
         <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-server</artifactId>
         <version>3.4.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>6.11</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
      <dependency>
         <groupId>io.github.bonigarcia</groupId>
         <artifactId>webdrivermanager</artifactId>
         <version>1.7.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
      <dependency>
         <groupId>com.aventstack</groupId>
         <artifactId>extentreports</artifactId>
         <version>3.1.5</version>
      </dependency>
      <dependency>
         <groupId>com.vimalselvam</groupId>
         <artifactId>cucumber-extentsreport</artifactId>
         <version>3.0.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>1.2.5</version>
         <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
      <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
      <dependency>
         <groupId>io.appium</groupId>
         <artifactId>java-client</artifactId>
         <version>6.0.0-BETA3</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>2.19</version>
            <configuration>
               <parallel>tests</parallel>
               <threadCount>10</threadCount>
               <suiteXmlFiles>
                  <suiteXmlFile>testng.xml</suiteXmlFile>
               </suiteXmlFiles>
            </configuration>
         </plugin>
      </plugins>
   </build>

1 Ответ

0 голосов
/ 10 ноября 2018
@BeforeClass
public void setUP() {   
    System.out.println("Thread = " + Thread.currentThread().getId() + " - object hash = " + this.hashCode());
    testRunner = new TestNGCucumberRunner(TestRunner.class);
    ExtentProperties extentProperties = ExtentProperties.INSTANCE;
    extentProperties.setReportPath("output/" + this.hashCode() + "-report.html");
}

Этот метод настройки вызывается дважды параллельно. Если вы меняете setReportPath на ExtentProperties.INSTANCE, который разделяется между этими потоками. Таким образом, темы перезаписывают результаты друг друга. Вы можете убедиться в этом, распечатав хеш ExtentProperties.INSTANCE.

...