英文:
Spock/VintageTestEngine - Running the selected test for a data-driven test method with several test cases
问题
以下是您提供的内容的翻译:
主要问题是当我使用VintageTestEngine时,无法搜索和运行数据驱动测试方法中的选定测试。
例如,我的测试方法名为LoginPageTestSuite_#parameters.TestCaseName,该方法使用AkeneoLoginPageTestSuite类中的数据进行填充。此测试方法使用测试数据进行填充,从而创建具有不同测试数据的多个测试用例。当该测试方法开始时,将创建4个测试用例,每个测试用例都具有不同的名称,该名称是将#parameters.TestCaseName参数添加到主方法名称中得到的。
VintageTestEngine具有允许您搜索和运行特定测试方法的方法。在我的情况下,我希望运行的不是整个包含4个测试的集合,而是仅一个测试,该测试在执行时将具有名称LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword。
我尝试过调试VintageTestEngine,如果我理解得正确的话,在MethodSelectorResolver类(包org.junit.vintage.engine.discovery)中的方法shouldRun()与我的问题有很大关系。
这段代码将在我的情况下返回false,因为desiredDescription.equals(description),desiredDescription为LoginPageTestSuite_TC_003_VerifyingLogin_LoginAndPassword,description为LoginPageTestSuite_#parameters.TestCaseName。
我的Spock测试类和VintageTestEngine类代码如下:
// TestSuites/LoginPageTestSuite/AkeneoLoginPageTestSuite.groovy
// ... (您提供的代码)
// Runners/AkeneoRunner.groovy
// ... (您提供的代码)
这是我使用的pom.xml文件:
<!-- pom.xml -->
<!-- ... (您提供的代码) -->
也许现在更清楚了,如果不清楚,请告诉我。谢谢。
英文:
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
专注分享java语言的经验与见解,让所有开发者获益!
评论