How to write cucumber Step definitions in python

前端 未结 2 633
南旧
南旧 2021-01-12 06:43

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 Pyth

2条回答
  •  时光说笑
    2021-01-12 07:12

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

    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:

    Gherkin:

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

    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.

    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.

提交回复
热议问题