Cucumber 使用例子

1. junit  配置
@RunWith(Cucumber.class)
@CucumberOptions(format ={"pretty","html:target/cucumber"},
features={"src/main/java/demoapp"},tags={"@third"})
public class test {
 
 
 
 
}
# language: zh-CN
功能:测试
@third
场景大纲:一些测试
假如 firstw "demo"
当 demoinfo <name>
那么 appdemo <info>
例子:
|name| info|
|first|dalong|
|second|aaaaa|
Feature:Belly
@first
ScenarioOutline: a few cukes
Given firstw "demo"
When demoinfo <name>
Then appdemo <info>
Examples:
|name| info|
|first|dalong|
|second|aaaaa|
publicclassMyStepdefs{
@Given("I have (\\d+) cukes in my belly")
publicvoid I_have_cukes_in_my_belly(int cukes){
System.out.format("Cukes: %n\n", cukes);
}
@When("^I wait (\\d+) hour$")
publicvoid i_wait_hour(int arg1)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
thrownewPendingException();
}
@Then("^dodemo$")
publicvoid dodemo()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("dodemo");
}
@Given("^first \"([^\"]*)\"$")
publicvoid first(String arg1)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("first"+": "+ arg1);
}
@Then("^my belly should growl$")
publicvoid my_belly_should_growl()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
thrownewPendingException();
}
@Given("^first demo$")
publicvoid first_demo()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("first_demo");
}
@When("^user names$")
publicvoid user_names(DataTable arg1)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
List<List<String>>list= arg1.raw();
System.out.println(list.get(0));
System.out.println(arg1.toString());
}
@When("^demoinfo ([^\"]*)$")
publicvoid demoinfo_name(String name)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println(name+"demoinfo_name");
//Assert.assertTrue(false);
}
@Then("^appdemo ([^\"]*)$")
publicvoid appdemo_info(String name )throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println(name+"appdemo_info");
}
@When("^call me$")
publicvoid call_me()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("call me");
}
@Then("^docall$")
publicvoid docall()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("docall");
}
@Given("^firstw \"([^\"]*)\"$")
publicvoid firstw(String arg1)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("firstw");
}
@When("^demoinfo$")
publicvoid demoinfo()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("demoinfo");
}
@When("^printinfos$")
publicvoid printinfos(List<String> info)throwsThrowable{
// Write code here that turns the phrase above into concrete actions
// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
info.forEach(newConsumer<String>(){
@Override
publicvoid accept(String t){
// TODO Auto-generated method stub
System.out.println(t+"demo info");
}
});
}
@Then("^appdemo$")
publicvoid appdemo()throwsThrowable{
// Write code here that turns the phrase above into concrete actions
System.out.println("appdemo");
}
}
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
 

  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python语言中的Cucumber示例使用了Python的BDD库behave。下面是一个简单的示例: 1. 首先需要安装behave库,在命令行中执行以下命令: ``` pip install behave ``` 2. 在项目中创建一个features目录,用于存放测试用例,然后在该目录下创建一个名为example.feature的文件,用于编写测试用例。 ``` Feature: Example feature As a user I want to see a welcome message So that I know the application is working Scenario: User visits homepage Given I am on the homepage Then I should see "Welcome to the application" ``` 该测试用例描述了一个场景,即用户访问网站首页时应该看到欢迎信息。 3. 接下来,需要编写step definitions,用于将测试用例中的文本转换为可执行的代码。在项目中创建一个名为steps的目录,然后在该目录下创建一个名为example_steps.py的文件,用于编写step definitions。 ``` from behave import * @given('I am on the homepage') def step_impl(context): context.browser.get("http://localhost:8000/") @then('I should see "{text}"') def step_impl(context, text): assert text in context.browser.page_source ``` 该文件中定义了两个step definitions,分别对应测试用例中的Given和Then。需要注意的是,该示例中使用了selenium库进行网页自动化测试。 4. 最后,在命令行中执行以下命令运行测试用例: ``` behave features/example.feature ``` 运行结果应该如下所示: ``` Feature: Example feature # features/example.feature:1 As a user I want to see a welcome message So that I know the application is working Scenario: User visits homepage # features/example.feature:5 Given I am on the homepage # features/steps/example_steps.py:5 Then I should see "Welcome to the application" # features/steps/example_steps.py:9 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 2 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.123s ``` 这表明测试用例已经通过了。 以上是一个简单的Python语言中的Cucumber示例,希望能够帮助你了解behave库的基本用法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值