使用 Gradle 启动黄瓜 android 运行 时如何传递标签?

How can I pass tags when starting a cucumber android run using Gradle?

我有一套 Cucumber android 测试(它们是仪器化测试),我 运行 使用 gradlew connectedCheck。我想做的是 运行 只有某些标签,当我 运行 我的测试时(这就是标签的用途,对吧?)使用命令行选项,而不必每次都修改代码测试 运行s(所以我可以很容易地从 CI 服务器等 运行 这些)。

例如,我有标签 @one@two。我想执行两个 gradle 构建,第一个包含所有 @one 测试,第二个包含所有 @two 测试。这将是自动化管道的一部分,因此我无法修改这两个构建之间的代码(使用 @CucumberOptions)。

这是我到目前为止尝试过的方法:

尝试 1:多个 CucumberOptions 注释 classes

我尝试有多个 运行 人(CucumberAndroidJUnitRunner 的子class)和不同的 @CucumberOptions 每个人,指定不同的标签,然后控制哪个通过 Gradle 属性使用。然后我做了两个版本,每个 运行ner 一个。然而,在这两种情况下,Cucumber 都只是按字母顺序使用第一个 运行ner 的 @CucumberOptions 注释,因此两个测试 运行 中的标签是相同的。 (换句话说,即使正在使用的 运行ner 有一个 @CucumberOptions 注释,也不一定是被使用的注释。)

尝试 2:使用 gradle -D 选项传递系统 属性

我从 this thread 那里找到了一个提示,我可能可以通过 -Dcucumber.options="--tags @two" 但我试过了,但没有成功。它仍然只是从 @CucumberOptions 中获取标签并忽略我在命令行上传递的任何内容。 (无论如何,这个线程令人困惑,因为它开始谈论 Gradle 但后来它谈论 Maven。我使用的是 Gradle 而不是 Maven 显然)。

我也试过-Dcucumber.filter.tags="@two"(这是我从cucumber-jvm docs中找到的,谈论的是Maven而不是Gradle)但是这也没有用,和上面一样。

其他调查

使用的版本

androidTestImplementation 'io.cucumber:cucumber-android:4.8.4'
androidTestImplementation 'io.cucumber:cucumber-picocontainer:4.8.1'

目前,我正在通过 JUnit 假设自己处理它。

我添加了一个基于项目 属性 的新构建配置字段,默认值 @main 这是我的标签之一。

android {
    defaultConfig {
        ...
        buildConfigField "String", "CUCUMBER_TAGS", "\"${getCucumberTags()}\""
    }
}

def getCucumberTags() {
    project.hasProperty("cucumbertags")
            ? project.getProperties().get("cucumbertags")
            : "@main"
}

我在胶水包中添加了一个新的 class

public class TagConfiguration {
    private boolean checkTagsMatch(final Scenario scenario) {
        // we check if at least one of the configured tags is present for this scenario
        // this is equivalent to Cucumber's "or" tag expression
        String[] configuredTags = BuildConfig.CUCUMBER_TAGS.split(" ");
        Collection<String> scenarioTags = scenario.getSourceTagNames();
        for (String configuredTag : configuredTags) {
            for (String scenarioTag : scenarioTags) {
                if (configuredTag.equals(scenarioTag)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Before(order=1)
    public void assumeTagsMatch(final Scenario scenario) {
        Assume.assumeTrue("Only run scenarios that match configured tags", checkTagsMatch(scenario));
    }
}

这只会让它跳过与其中一个标签不匹配的测试 - 我可以像 gradlew connectedCheck -Pcucumbertags="@one @two".

一样调用

一点也不理想,因为那些测试仍然出现在报告中但被标记为已跳过,而且我的解决方案不支持标记表达式。如果有人有更好的想法,我很想听听。

您可以通过 gradle 传递选项,使用:

-Pandroid.testInstrumentationRunnerArguments.key=value

例如:

-Pandroid.testInstrumentationRunnerArguments.tags=@two

然后将这些选项传递给检测并由 cucumber 处理,请参阅 https://github.com/cucumber/cucumber-jvm/pull/597

因此您可以使用例如:

gradlew connectedCheck -Pandroid.testInstrumentationRunnerArguments.tags=@two