如何为Java设置黄瓜测试

Java黄瓜测试分为3部分。

  1. 功能文件步骤定义(“胶水”文件)junit测试文件

功能文件是测试的行为或业务描述所在。 这些文件使用Gherkin语法编写。

编写Cucumber测试时,请首先编写功能文件。 如果您的组织可以这样做,请利益相关者,企业所有者和用户来写。 如果不是,那么首席开发人员和/或项目经理应与利益相关者,企业所有者和用户一起编写功能文件。 投入工作并收集他们的故事和场景。

写入功能文件后,编写步骤定义。 这就是黄瓜人们所说的“胶水”代码。 本质上是正则表达式,设置和测试代码。

最后,为了使在Java中轻松运行Cucumber测试,编写一个junit包装器类。 这是一个带注释的空类,它将使Cucumber可以使用junit的测试设备来运行Cucumber测试。

Example

I've created an example project. Cucumber uses Maven, so we have a project with a standard Maven directory structure:

+---src
|   +---main
|   |   +---java
|   \---test
|       +---java
|       \---resources

Organization

让我们将功能文件置于测试/资源下。 让我们将junit类放在主体测试包下-在这种情况下com.gunnargissel.cucumberexample.artfight。 让我们将步骤定义(“胶水”)放在主测试包下的stepdefs包中-com.gunnargissel.cucumberexample.artfight.stepdefs

Behavior Scenario

For this example, we are creating the business engine for an art supply store. Let's use Stuart Semple as an example. Stuart Semple sells "Black 2.0 - The world's mattest, flattest, black art material" to everyone except Anish Kapoor. There's a bit of a backstory.

这是功能文件的外观,no_anish_kapoor.feature:

Feature: Stuart Semple will sell Black 2.0 to any artist except Anish Kapoor

    Scenario Outline: Anish Kapoor tries to buy Black 2.0
                       Given a purchaser, <purchaser name>, Black 2.0 is <saleable>
                       Examples:
                                 | purchaser name    | saleable     |
                                 | Anish Kapoor      | not saleable |
                                 | John Doe          | saleable     |
                                 | Jane Doe          | saleable     |
                                 | Manish Kapoor     | saleable     |
                                 | Anish Kapoorski   | saleable     |

使用功能文件,可以创建步骤定义或“胶水”代码。 在这种情况下,请创建src / test / java / com.gunnargissel.blacktwopointoh.stepdefs.NoAnishKapoor.java

上面概述的方案仅使用一个关键字“给定”。 这意味着我们将在我们的步骤定义“ glue”类中创建一个使用@吉文注解。 的目标@吉文注解是编写与场景匹配的正则表达式。 在这种情况下,我们希望<purchaser name>和<saleable>作为变量的质量,我们传递给stepdef。 剩下的@吉文是我们进行模式匹配的业务语言。 stepdef会执行典型的junit测试以及测试的设置。

@Given("^a purchaser, (.*), Black 2\\.0 is (not saleable|saleable)$")
            public void testWhoCanPurchaseBlackTwoPointOh(String purchaser, String saleable) {
                    boolean actual_saleable = BusinessRules.saleable(purchaser);
                    boolean expected_saleable = saleable.equals("saleable");
                    assertEquals(expected_saleable, actual_saleable);
            }

现在有了一个场景和一个步骤定义,需要像下面这样创建一个junit运行器类:

@RunWith(Cucumber.class)
@CucumberOptions(
                       format={"pretty", "junit:target/cucumber/black_two_point_oh_sales_list.xml"},
                        features = {"classpath:no_anish_kapoor.feature"},
                        glue={"com.gunnargissel.cucumberexample.artfight.stepdefs"}
)
public class TestBusinessRules {}

创建必要的文件后,运行MVN测试将执行上面创建的功能方案。 Maven将创建black_two_point_oh_sales_list.xml在里面目标/黄瓜目录。

Further Resources

Ĵust Enough Regular Expressions for Cucumber is a great guide that explains how to use regexes (and Java) with Cucumber. The same author also made a cheatsheet that is a helpful reference.

The artfight example code.

Get a monthly email with great tech and tech leadership articles from around the web

Visit my blog for the original article

Thank you Ali Burçin Titizel for the header image

from: https://dev.to//monknomo/how-to-set-up-cucumber-tests-for-java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值