且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中编写黄瓜步骤定义

更新时间:2022-12-10 09:02:22

黄瓜目前支持14种语言,包括JVM上的Python(也称为Jython).

Cucumber supports 14 languages right now, including Python on the JVM also called Jython.

我将从阅读 Cucumber-JVM 开始,它是Cucumber的实现JVM.要使用Java 6/7,可以使用Cucumber API.您需要编写带有Java批注的Python方法,以告诉Cucumber哪些正则表达式与每种方法相关.

I would start by reading up on Cucumber-JVM, it is an implementation of Cucumber for the JVM. To use the Java 6/7 so you can use the Cucumber API. You need to write Python methods with Java annotations, to tell Cucumber which regexes correlate with each method.

这听起来像是很多间接的操作,但是很简单:

This sounds like a lot of indirection, but it is fairly straight forward:

小黄瓜:

Scenario: Some cukes
  Given I have 48 cukes in my belly

Python/Jython:

Python/Jython:

@Given('^I have (\d+) cukes in my belly')
def i_have_cukes_in_my_belly(self, cukes):
   print "Cukes: " + cukes

这是从每个代码示例角落中的黄瓜参考页复制的(不是小黄瓜,而是步骤定义),您可以选择自己喜欢的语言.

This was copied from the cucumber reference page in the corner of each code sample (not gherkin, but step definition), you can select the language of you choice.

文档不完整,但是在完整的地方很有用.如果您使用的是maven配置,则它确实包含条目 .基本使用所需.只要您熟悉从Jython调用Java .

The documentation is incomplete, but where it is complete it is useful. It does include the entry for your maven config if you are using that and most of the information needed for basic use. Any documentation you find elsewhere on the web for cucumber in Java should work with Jython as long as you are familiar with calling Java from Jython.