【问题标题】:How to use cucumber/gherkin in different language?如何用不同的语言使用黄瓜/小黄瓜?
【发布时间】:2018-09-15 01:58:22
【问题描述】:

我正在公司中使用 Cucumber、Sikuli 和 Eclipse 建立一个测试环境。我已经设法让一切工作正常,但现在我需要让 .feature 文件在葡萄牙语中工作。

据我所知,我所要做的就是将评论 #language: pt 放在 .feature 文件的开头,但它不起作用。

我还运行了命令cucumber --i18n helpcucumber --i18n pt 来检查该语言是否真的存在,并且确实存在。

还有其他我不知道的配置吗?

我正在使用这些罐子:

  • cucumber-core-3.0.2.jar
  • 黄瓜-java-3.0.2.jar
  • 黄瓜-junit-3.0.2.jar
  • 黄瓜-jvm-deps-1.0.6.jar
  • gherkin-5.1.0.jar
  • junit-4.12.jar
  • mockito-all-1.10.19.jar

【问题讨论】:

  • 我在答案中添加了一些代码 sn-ps。也许你可以从他们开始。
  • 您在 Windows 上吗?检查您的编辑器是否将功能文件保存为 UTF-8,并以 BOM 字符 (EF BB BF) 开头(最好使用十六进制查看器或类似工具)。

标签: java eclipse cucumber bdd gherkin


【解决方案1】:

您可以通过在第一行添加# language: xx 来控制Gherkin 关键字的语言。

葡萄牙语是

# language: pt
Funcionalidade: um exemplo

可以在https://docs.cucumber.io/gherkin/reference/#spoken-languages 找到所有支持的口语列表。您可能会在 gherkin-languages.json

中查找的本地化关键字

一个简单的 Maven 项目。

结构

pom.xml
src/test/java/features/example.feature
src/test/java/runner/TestRunner.java

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>com.suboptimal</groupId>
    <artifactId>cuke-test-13.so</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.cucumber>3.0.2</version.cucumber>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${version.cucumber}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${version.cucumber}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

src/test/java/features/example.feature

# language: pt
Funcionalidade: um exemplo

  Cenario: foo
  Dado something given

src/test/java/runner/TestRunner.java

package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/features/example.feature"
        )
public class TestRunner {
}

使用mvn compile test 运行测试将产生以下错误。

Undefined scenarios:
src/test/java/features/example.feature:4 # foo

1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.036s


You can implement missing steps with the snippets below:

@Dado("something given")
public void something_given() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

编辑当功能文件在 Windows 上以 Notepad 保存为 UTF-8 时,会在开头插入 BOM 字符 (EF BB BD)。

$ hexdump -C example.feature
00000000 ef bb bf 23 20 6c 61 6e 67 75 61 67 65 3a 20 70 |...# >语言:p|

如果运行mvn test,则执行失败并出现异常。

cucumber.runtime.CucumberException: gherkin.ParserException$CompositeParserException: Parser errors:
(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '# language: pt'
(2:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Funcionalidade: um exemplo'
(4:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Cenario: foo'
(5:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Dado something given'

【讨论】:

  • 所以本质上,他只是错过了空间?
  • @KyleFairns 我错过了问题中的这一部分。但空白并不重要。我将添加一个示例。
  • 我没有使用 maven,但我确实按照这些说明使用 maven 创建了另一个,调整了需要调整的内容,但结果是一样的。当我使用Funcionalidade: abrindo a lista de pedidos 时,它仍然会抛出这个错误Missing 'Feature' at: 'Funcionalidade'
  • @BrunoPinheiro 我添加了完整的pom.xml。您使用的是哪个 Maven/Java 版本?你的语言环境是什么 (java -XshowSettings:properties 2&gt;&amp;1 | egrep "user.(language|country)")?
  • 现在我正在使用 Java 1.8.0.171、Maven Compiler 3.3、Cucumber、1.2.5、Gherkin 2.12.2,但我也尝试使用 maven 存储库中可用的最新版本,国家是 BR 和语言是pt。
【解决方案2】:

我用不同的十六进制查看器打开了 .feature 文件,这似乎更正确:

奇怪的是,尽管出现了错误,我还是尝试执行代码并且它有效。我真的不知道为什么。我只是出于测试目的将关键字更改为葡萄牙语。代码显示了错误,但执行了所有步骤,就好像一切正​​常。

eclipse 关于错误的唯一说法是missing 'Feature:' at 'Funcionalidade:'

【讨论】:

  • 您的 hexdump 似乎被转移或类似的东西。由于图像中的字节代表`在主菜单中的上午\n Quando 我单击订单按钮\n Então 应打开订单列表`。 Eclipse 是否会说明错误的类型?
  • 新的 hexdump 看起来更好。可能是Eclipse Cucumber插件本地化关键字的问题。
【解决方案3】:

首先,感谢所有帮助过我的人。

经过一段时间,我终于找到了问题所在。

Sébastien Le Callonnec 在this thread 上的回答 让我明白了。问题出在 Natural 插件上。

从一开始一切正常,但事实证明 Natural 只支持英语。这就是为什么它抛出错误但测试正在执行。卸载插件后,错误消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多