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

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

问题描述

我应该在 Cucumber 上迁移.我确实有带有 Selenium 的项目框架,带有数据驱动框架的 TestNG,Maven.我正在使用 TestNG 注释探索 Cucumber 的可行性.

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>标签可以可视化为cucumber中的特征文件.
  • TestNG中的@Test方法可以可视化为cucumber中的一个场景.
  • cucumber 中的 Step 定义与 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 注释提供的内容).您或许应该仔细查看黄瓜 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 注释执行 Cucumber Step 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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