Spock/VintageTestEngine - Running the selected test for a data-driven test method with several test cases

207 views Asked by At

The main problem is when I use VintageTestEngine I can't search and run selected tests that are part of the data-driven test method.

For example, my test method called name LoginPageTestSuite_ # parameters.TestCaseName is fed with data in the AkeneoLoginPageTestSuite class. This test method is fed with a list of test data and thus creates several test cases with different test data. When this test method starts, 4 test cases are created and each has a different name resulting from adding the # parameters.TestCaseName parameter to the main method name.

The VintageTestEngine has methods that allow you to search and run specific test methods. In my case, I would like to run not the whole set with 4 tests, but only one which at the time of execution will have the name LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword.

I tried to debug VintageTestEngine and if I understand it correctly in the MethodSelectorResolver class (package org.junit.vintage.engine.discovery) the method shouldRun() has a big relationship with my problem.

private static Filter matchMethodDescription (final Description desiredDescription) {
        return new Filter () {
            public boolean shouldRun (Description description) {
                if (! description.isTest ()) {
                    Iterator var2 = description.getChildren (). Iterator ();

                    Description each;
                    up to {
                        if (! var2.hasNext ()) {
                            return false;
                        }

                        each = (Description) var2.next ();
                    } while (! this.shouldRun (each));

                    return true;
                } else {
                    return desiredDescription.equals (description) || this.isParameterizedMethod (description);
                }
            }

This code will return false in my case because, desiredDescription.equals (description), desiredDescription - LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword, description - LoginPageTestSuite_ # parameters.TestCaseName

My code for Spock test class and VintageTestEngine class.

package TestSuites.LoginPageTestSuite

import Assertions.LoginPageAsserts
import DataSourceExtensions.TestDataSource
import Model.LoginPageData
import PageObjects.LoginPageObject
import TestSuites.BaseTest
import com.google.common.base.Strings
import org.junit.jupiter.api.DisplayName
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import spock.lang.Shared
import spock.lang.Unroll

class AkeneoLoginPageTestSuite extends BaseTest{

    @TestDataSource(TestSourcePath = "TestSuites/TestCaseData/LoginPageTestCaseData.json")
    private @Shared List<LoginPageData> _testCaseData

    private LoginPageObject _loginPage

    @DisplayName(value = "LoginPageTestSuite_#parameters.TestCaseName")
    @Unroll
    def "LoginPageTestSuite_#parameters.TestCaseName"(LoginPageData parameters)
    {
        given:
        _loginPage = new LoginPageObject()

        when:
        _loginPage.FillUserNameAndPassword(parameters.UserName, parameters.Password)

        then:
        if(!Strings.isNullOrEmpty(parameters.InvalidLoginMessage))
            LoginPageAsserts.VerifyingLoginPageMessageBox(
                    parameters.InvalidLoginMessage, _loginPage.getIncorrectLoginMessageBoxText())

        where:
        parameters << _testCaseData
    }
package Runners

import Configurations.Configuration
import org.junit.platform.engine.EngineDiscoveryRequest
import org.junit.platform.engine.ExecutionRequest
import org.junit.platform.engine.TestDescriptor
import org.junit.platform.engine.UniqueId
import org.junit.platform.engine.discovery.MethodSelector
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
import org.junit.vintage.engine.VintageTestEngine

class AkeneoRunner {

    static void main(String[] args){
        new Configuration()

        VintageTestEngine engine = new VintageTestEngine()
        EngineDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request().selectors(
                new MethodSelector(Class.forName("TestSuites.LoginPageTestSuite.AkeneoLoginPageTestSuite"),
                        "LoginPageTestSuite__TC_003_VerifyingLogin_LoginAndPassword"))
                .build()
        UniqueId root = UniqueId.root("LoginPageTestSuite__TC_003_VerifyingLogin_LoginAndPassword", "LoginPageTestSuite__TC_003_VerifyingLogin_LoginAndPassword")

        TestDescriptor testDescriptor = engine.discover(discovery, root)

        engine.execute(new ExecutionRequest(testDescriptor, new NoOpEngineExecutionListener(), new NoConfigurationParameters()))
    }
}

Here is the pom.xml file I am using:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.akeneo.groovy</groupId>
  <artifactId>AkeneoGroovy</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>AkeneoGroovy</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>1.3-groovy-2.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>1.25</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.sqlserver</groupId>
      <artifactId>mssql-jdbc</artifactId>
      <version>6.1.0.jre8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.12.0</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-engine</artifactId>
      <version>1.7.0-M1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.7.0-M1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.7.0-M1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Maybe its now more clear, if not please let me know. Thanks

0

There are 0 answers