如何在python中编写黄瓜步骤定义 [英] How to write cucumber Step definitions in python

查看:12588
本文介绍了如何在python中编写黄瓜步骤定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cucumber框架的新手.我正在尝试使Cucumber与Python一起使用.我已经编写了功能文件,想知道如何用Python编写步骤定义.

I am new to the Cucumber framework. I am trying to make Cucumber work with Python. I have written the feature file and want to know how to write the step definitions in Python.

我在互联网上进行搜索,找到了Ruby语言的步骤定义,但没有找到Python的步骤定义.甚至可以用Python运行Cucumber吗?

I searched over the internet and found the step definitions for Ruby language but nothing for Python. Is it even possible to run Cucumber with Python?

推荐答案

黄瓜目前支持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.

这篇关于如何在python中编写黄瓜步骤定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆