黄瓜重新运行失败的功能:不一致的过滤器:[@ test1,@ test,23,36]。一次只能使用一种类型[行,名称,标签]

时间:2017-04-20 04:49:00

标签: maven selenium cucumber testng

尝试使用Cucumber中的重新运行选项重新执行Cucumber中的失败功能时获取以下错误消息,

[Utils] [ERROR] [Error] java.lang.IllegalArgumentException: **Inconsistent filters: [@test1, @test, 23, 36]. Only one type [line,name,tag] can be used at once.**
    at gherkin.formatter.FilterFormatter.detectFilter(FilterFormatter.java:59)
    at gherkin.formatter.FilterFormatter.<init>(FilterFormatter.java:41)
    at cucumber.runtime.FeatureBuilder.parse(FeatureBuilder.java:126)
    at cucumber.runtime.model.CucumberFeature.loadFromFeaturePath(CucumberFeature.java:104)
    at cucumber.runtime.model.CucumberFeature.loadFromFileSystemOrClasspath(CucumberFeature.java:75)
    at cucumber.runtime.model.CucumberFeature.loadFromRerunFile(CucumberFeature.java:67)
    at cucumber.runtime.model.CucumberFeature.load(CucumberFeature.java:52)
    at cucumber.runtime.model.CucumberFeature.load(CucumberFeature.java:34)
    at cucumber.runtime.RuntimeOptions.cucumberFeatures(RuntimeOptions.java:239)
    at cucumber.api.testng.TestNGCucumberRunner.getFeatures(TestNGCucumberRunner.java:85)
    at cucumber.api.testng.TestNGCucumberRunner.provideFeatures(TestNGCucumberRunner.java:94)
    at android.consumer.tests.FailedTestRunner.features(FailedTestRunner.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
    at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:509)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1308)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1036)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1126)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:281)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

Pom.xml 看起来像,

    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    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.company</groupId>
    <artifactId>company</artifactId>
    <version>0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>5.0.0-BETA6</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

        <dependency> 
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.5</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <!-- <scope>test</scope> -->
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
    </dependencies>    

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <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.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <suiteXmlFiles>
                        <suiteXmlFile>${testngxml}</suiteXmlFile>
                    </suiteXmlFiles>
                    <!-- <groups>${groups}</groups> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

TestRunner类看起来像,

    @CucumberOptions(monochrome = true,strict=true,plugin = { "json:target/Result.json","rerun:target/rerun.txt"},features = {
        "." }, glue = { "stepdefinition" })

FailedTestRunner类看起来像,

@CucumberOptions(monochrome = true,strict=true,plugin = { "json:target/Result.json"} ,features = {
        "@target/rerun.txt" }, glue = { "stepdefinition" })

TestNG.xml包含,

<test name="AndroidConsumerTest">
        <parameter name="device" value="FQ79PZKNLJCEGE4H" />
        <!-- production - prod ; staging - debug d356be51 7f8dd844 FQ79PZKNLJCEGE4H -->
        <classes>
            <class name="TestRunner" />
            <class name="FailedTestRunner" />
        </classes>
    </test>

成功执行了一轮执行并生成了重新运行文本文件,但是在将此文本文件作为第二个运行程序类的输入时,为了重新执行失败的测试,我收到了“Inconsistent Filter”错误消息。

1 个答案:

答案 0 :(得分:0)

使用以下模板创建您的TestRunner和FailedTestRunner类,请明智地使用它根据您的要求只使用一个块

package org.sugat.cucumber.Main;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

/**
 * Created by sugat.mankar on 4/20/2017.
 */
public class MyCLIMain {

    public static void main(String[] args) {
        List<String> arguments = new ArrayList<String>();

        //for running only scenarios with tags use this block
        arguments.add("path to feature file");
        String[] tags = {"@tag1","@tag2","@tag3"};
        for (String tag : tags) {
            arguments.add("--tags");
            arguments.add(tag);
        }

        //for running only scenario outlines with example line number
        arguments.add("path to feature file:line number of example");

        // for rerun
        try {
            if(new File("target/rerun.txt").exists() && new BufferedReader(new FileReader("target/rerun.txt")).readLine() != null ) {
                arguments.add("@target/rerun.txt");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        arguments.add("--plugin");
        arguments.add("pretty:target/cucumber-pretty.txt");
        arguments.add("--plugin");
        arguments.add("json:target/Result.json");
        arguments.add("--plugin");
        arguments.add("rerun:target/rerun.txt");
        String[] gluepackages = "org.sugat.steps".split(",");
        for (String packages : gluepackages) {
            if (!packages.contains("none")) {
                arguments.add("--glue");
                arguments.add(packages);
            }
        }
        final String[] argv = arguments.toArray(new String[0]);
        try {
            byte exitStatus = run(argv, MyCLIMain.class.getClassLoader());
            System.exit(exitStatus);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        runtime.run();
        return runtime.exitStatus();
    }
}

例如,TestRunner.java应该看起来像

package org.sugat.cucumber.Main;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

/**
 * Created by sugat.mankar on 4/20/2017.
 */
public class TestRunner{

    public static void main(String[] args) {
        List<String> arguments = new ArrayList<String>();

        //BEGIN ********for running only scenarios with tags use this block 
        arguments.add("path to feature file");
        String[] tags = {"@tag1","@tag2","@tag3"};
        for (String tag : tags) {
            arguments.add("--tags");
            arguments.add(tag);
        }
        //END ********for running only scenarios with tags use this block

        // ********** OR*******************

        //BEGIN****** for running only scenario outlines with example line number
        arguments.add("path to feature file:line number of example");
        //END****** for running only scenario outlines with example line number

        arguments.add("--plugin");
        arguments.add("pretty:target/cucumber-pretty.txt");
        arguments.add("--plugin");
        arguments.add("json:target/Result.json");
        arguments.add("--plugin");
        arguments.add("rerun:target/rerun.txt");
        String[] gluepackages = "org.sugat.steps".split(",");
        for (String packages : gluepackages) {
            if (!packages.contains("none")) {
                arguments.add("--glue");
                arguments.add(packages);
            }
        }
        final String[] argv = arguments.toArray(new String[0]);
        try {
            byte exitStatus = run(argv, TestRunner.class.getClassLoader());
            System.exit(exitStatus);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        runtime.run();
        return runtime.exitStatus();
    }
}

FailedTestRunner.java类应该如下

package org.sugat.cucumber.Main;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

/**
 * Created by sugat.mankar on 4/20/2017.
 */
public class FailedTestRunner{

    public static void main(String[] args) {
        List<String> arguments = new ArrayList<String>();
        // for rerun
        try {
            if(new File("target/rerun.txt").exists() && new BufferedReader(new FileReader("target/rerun.txt")).readLine() != null ) {
                arguments.add("@target/rerun.txt");
            } else {
                throw new Exception("Either rerun.txt file is empty or there is no failed test");
            }
        } catch (IOException e) {
            e.printStackTrace();
         } catch (Exception e) {
            e.printStackTrace();
        }
        arguments.add("--plugin");
        arguments.add("pretty:target/cucumber-pretty.txt");
        arguments.add("--plugin");
        arguments.add("json:target/Result.json");
        arguments.add("--plugin");
        arguments.add("rerun:target/rerun.txt");
        String[] gluepackages = "org.sugat.steps".split(",");
        for (String packages : gluepackages) {
            if (!packages.contains("none")) {
                arguments.add("--glue");
                arguments.add(packages);
            }
        }
        final String[] argv = arguments.toArray(new String[0]);
        try {
            byte exitStatus = run(argv, FailedTestRunner.class.getClassLoader());
            System.exit(exitStatus);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        runtime.run();
        return runtime.exitStatus();
    }
}