如何使用TestNG注释执行黄瓜步骤定义 [英] how to execute Cucumber Step defination with TestNG annotation

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

问题描述

我应该在黄瓜上迁移.我确实有使用Selenium的项目框架,使用Data Driven Framework的TestNG,Maven.我正在探索用TestNG注释进行黄瓜的可行性.

I am supposed to migrate on Cucumber. I do have project framework with Selenium, TestNG with Data Driven Framework, Maven. I am exploring Cucumber feasibility with TestNG annotation.

我的问题是,我们如何在@Test方法和黄瓜的Step定义之间建立连接.让我们举例说明我们的代码是用@ BeforeClass,@ Test,@ AfterClass方法编写的.因此,如何使用Step定义进行迁移.

My question is, How we can create connection between @Test method and Step definition of cucumber. Let's example our code is written in @BeforeClass, @Test, @AfterClass method. So how we can migrate with Step definition.

功能文件:

Feature: Is it Friday yet?
  Everybody wants to know when it's Friday

  Scenario: Sunday isn't Friday
    Given today is Sunday
    When I ask whether it's Friday yet

步骤定义:

@Given("^today is Sunday$")
public void today_is_Sunday() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("this is demo1");
}

@When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("this is demo2");
}

课堂锻炼:

@CucumberOptions(features = "cfeature/firstDemo.feature", glue = { "mytest/Stepd" })
public class demo01  extends AbstractTestNGCucumberTests {

    private TestNGCucumberRunner tcr;

    @BeforeClass(alwaysRun = true)
    public void beforeClass() throws Exception {
        tcr = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups="cucumber", description="Runs CucumberFeature")    
    public void testdemo() {
        System.out.println("Hello");        
    }

    @AfterClass(alwaysRun = true)
    public void afterClass() {
        tcr.finish();
    }   
}

控制台:

Hello

[33mUndefined scenarios:[0m
[33mcfeature/firstDemo.feature:4 [0m# Sunday isn't Friday

1 Scenarios ([33m1 undefined[0m)
5 Steps ([33m5 undefined[0m)
0m0.073s


You can implement missing steps with the snippets below:

截至目前,@ Test批注正在调用.但是,如何将其替换为步骤定义".请协助.

As of now, @Test annotation is calling. But, How to replace it with Step Definition. Please assist.

推荐答案

不确定此处的混乱之处.这是将TestNG和黄瓜术语联系起来的方法.

Not sure what the confusion here. Here's how you can relate TestNG and cucumber terminologies.

    可以将TestNG中的
  • < test> 标记可视化为黄瓜中的特征文件.
  • 可以将TestNG中的
  • @Test 方法可视化为黄瓜中的一个场景.
  • 黄瓜中的步骤定义与TestNG中没有任何直接等价的关系,因为这是场景的一部分.但是为了理解,您可以将其可视化为在TestNG中执行逻辑操作的一行代码.
  • <test> tag in TestNG can be visualized as a feature file in cucumber.
  • @Test method in TestNG can be visualized as a scenario in cucumber.
  • A Step definition in cucumber has nothing directly equivalent to in TestNG because, its part of a scenario. But for the sake of understanding you can visualize it as one line of code doing a logical operation in TestNG.

AbstractTestNGCucumberTests 的默认实现如下:

  • 它内部包含一个数据提供程序,该数据提供程序一次提供一个功能文件.
  • 它包含一个 @Test 方法,该方法绑定到上述数据提供程序,该方法将检索功能文件中的所有方案,然后一个接一个地运行它们.
  • It contains a data provider internally which provides one feature file at a time.
  • It contains a @Test method which is bound to the above mentioned data provider, which retrieves all the scenarios in the feature file and then runs them one after the other.

您可以构建自己的 AbstractTestNGCucumberTests 变体来执行各种不同的操作(例如,支持并发脚本执行,而Cucumber JVM绑定中目前尚不支持并发脚本执行).

You can build your own variant of AbstractTestNGCucumberTests to do various different things (such as support concurrent scenario execution which is currently not available in Cucumber JVM bindings).

作为示例,您可以查看我建立的 Cucumber-roadrunner 库使用以上概念来支持并行方案执行,并提供线程安全报告.

As an example you can take a look at Cucumber-roadrunner library that I built which uses the above concept to support parallel scenario execution and also provides thread safe reports.

关于您所面临的错误,即您可以使用以下代码片段实施缺少的步骤:基本上是因为黄瓜jvm绑定可能无法用胶水来绑定您的功能文件代码(这是您通过 @CucumberOptions 注释提供的内容).您也许应该仔细研究Cucumber jvm绑定文档,以了解如何提供正确的值.

With respect to the error you are facing viz., You can implement missing steps with the snippets below: is basically because cucumber jvm bindings perhaps isn't able to bind your feature file with a glue code (which is what you are providing via the @CucumberOptions annotation). You should perhaps take a closer look at the cucumber jvm bindings documentation to understand how to provide the correct values.

这篇关于如何使用TestNG注释执行黄瓜步骤定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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