首页 > 解决方案 > Spock/VintageTestEngine - 为具有多个测试用例的数据驱动测试方法运行选定的测试

问题描述

主要问题是当我使用 VintageTestEngine 时,我无法搜索和运行作为数据驱动测试方法一部分的选定测试。

例如,我的名为 name LoginPageTestSuite_#parameters.TestCaseName 的测试方法由 AkeneoLoginPageTestSuite 类中的数据提供。这个测试方法被提供了一个测试数据列表,因此创建了几个具有不同测试数据的测试用例。当此测试方法启动时,将创建 4 个测试用例,每个测试用例都有不同的名称,这是通过在主方法名称中添加 #parameters.TestCaseName 参数而产生的。

VintageTestEngine 具有允许您搜索和运行特定测试方法的方法。就我而言,我想运行的不是带有 4 个测试的整个集合,而是只有一个在执行时将具有名称 LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword。

我尝试调试 VintageTestEngine,如果我在 MethodSelectorResolver 类(包 org.junit.vintage.engine.discovery)中正确理解它,则方法 shouldRun() 与我的问题有很大关系。

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);
                }
            }

在我的情况下,此代码将返回 false,因为,desiredDescription.equals(描述),desiredDescription - LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword,description - LoginPageTestSuite_ #parameters.TestCaseName

我的 Spock 测试类和 VintageTestEngine 类的代码。

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()))
    }
}

这是我正在使用的 pom.xml 文件:

<?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>

也许现在更清楚了,如果没有,请告诉我。谢谢

标签: javagroovyspockjunit5junit-vintage

解决方案


推荐阅读