±à¼ÍƼö: |
±¾ÎĽ²½âÁËÈçºÎ°²×°Cucumber£¬ÈçºÎ×ñÑBDDÁ÷³ÌÀ´¿ª·¢Ò»¸ö¼òµ¥µÄ·½·¨£¬ÒÔ¼°ÈçºÎʹÓø÷½·¨À´ÆÀ¹À¶à¸ö³¡¾°,Ï£ÍûÄܶÔÄúÓÐËù°ïÖú¡£
±¾ÎÄÀ´×ÔÓÚ²©¿ÍÔ°£¬ÓÉ»ðÁú¹ûÈí¼þç÷ç÷±à¼¡¢ÍƼö¡£ |
|
°²×°
cucumberÊÇÒ»¿î²âÊÔ¹¤¾ß¡£¿ÉÓÃÓÚ´ó¶àÊýÖ÷Á÷±à³ÌÓïÑÔ¡£±ÈÈçJAVA¡¢JS¡¢Ruby¡¢C++¡¢Lua¡¢Android¡¢Kotlin¡¢C#/F#¡¢PHP¡¢Python¡¢Go¡¢Groovy¡¢ScalaµÈµÈ¡£ÆäÖÐJAVA¡¢JS¡¢RubyµÄ´úÂëÍйÜÔÚcucumberÏ¡£¹Ù·½½¨ÒéÑ¡ÔñÓëÉú²ú´úÂëÏàͬµÄƽ̨»ò±à³ÌÓïÑÔµÄʵÏÖ¡£±¾ÎÄÖ÷ÒªÊÇJAVAƽ̨ϵĽéÉ̡̳ܽ£Ê¹Ó÷½·¨·Ç³£¼òµ¥£¬´´½¨Ò»¸ömvn¹¤³Ì£¬ÔÚpom.xmlÎļþÒýÈëÒÔÏÂÒÀÀµ¼´¿É¡£
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.2.0</version> <scope>test</scope>
</dependency> |
Ò²¿ÉÒÔ¸ù¾Ý¹Ç¼Ü´´½¨cucumberÏîÄ¿¡£
´´½¨Ò»¸ö¿ÕµÄCucumberÏîÄ¿
ÎÒÃÇÊ×ÏÈʹÓÃcucumber- prototype Maven²å¼þ´´½¨Ò»¸öÐÂÏîĿĿ¼¡£´ò¿ªÖÕ¶Ë£¬×ªµ½Òª´´½¨ÏîÄ¿µÄĿ¼£¨±ÈÈç±¾ÎÄÊÇhellocucumber)£¬ÔËÐÐÒÔÏÂÃüÁî:
mvn archetype:generate
\
-DarchetypeGroupId=io.cucumber \
-DarchetypeArtifactId=cucumber-archetype \
-DarchetypeVersion=2.3.1.2 \
-DgroupId=hellocucumber \
-DartifactId=hellocucumber \
-Dpackage=hellocucumber \
-Dversion=1.0.0-SNAPSHOT \
-DinteractiveMode=false |
ÄãÓ¦¸ÃµÃµ½ÈçϽá¹û:
[INFO] Project
created from Archetype in dir: hellocucumber/cucumber
[INFO] -------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------- |
Çл»µ½¸Õ²ÅÔËÐÐÃüÁî´´½¨µÄĿ¼:
ÔÚIntelliJ IDEA(»òÕßeclipse¶¼ÐУ©Öдò¿ªÏîÄ¿:
Îļþ->´ò¿ª¡->(Ñ¡Ôñpom.xml)
Ñ¡ÔñOpen as Project
ÏÖÔÚ£¬ÄúÒѾ°²×°ÁËÒ»¸ö¼òµ¥µÄCucumberÏîÄ¿¡£
ÑéÖ¤cucumber°²×°
ÄúÓ¦¸Ã¿´µ½ÈçÏÂÄÚÈÝ:
------------------------------------
T E S T S
------------------------------------
Running hellocucumber.RunCucumberTest
No features found at [classpath:hellocucumber]
0 Scenarios
0 Steps
0m0.004s
Tests run: 0, Failures: 0, Errors: 0, Skipped:
0, Time elapsed: 0.541 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped:
0
[INFO] -----------------------------------------
[INFO] BUILD SUCCESS
[INFO] ----------------------------------------- |
CucumberµÄÊä³ö¸æËßÎÒÃÇËüûÓÐÕÒµ½ÈκοÉÒÔÔËÐеĶ«Î÷¡£
дһ¸öScenario£¨³¡¾°£©
µ±ÎÒÃÇʹÓÃCucumber½øÐÐÐÐΪÇý¶¯¿ª·¢Ê±£¬ÎÒÃÇʹÓþßÌåµÄÀý×ÓÀ´Ö¸¶¨ÎÒÃÇÏ£ÍûÈí¼þ×öʲô¡£ ScenarioÊÇÔÚÉú²ú´úÂë֮ǰ±àдµÄ¡£ËüÃÇÒÔ¿ÉÖ´Ðй淶µÄÐÎʽ¿ªÊ¼ÉúÃü¡£Ëæ×ÅÉú²ú´úÂëµÄ³öÏÖ£¬³¡¾°°çÑÝÁËÊÂʵÎĵµºÍ×Ô¶¯»¯²âÊԵĽÇÉ«¡£
ÔÚCucumberÖУ¬Ò»¸öexample³ÆÎªScenario¡£Scenario¶¨ÒåÔÚ.featureÎļþÖУ¬ÕâЩÎļþ´æ´¢ÔÚsrc/test/resources/hellocucumberĿ¼(»ò×ÓĿ¼)ÖС£
Ò»¸ö¾ßÌåµÄÀý×Ó¾ÍÊÇ£ºÐÇÆÚÌì²»ÊÇÐÇÆÚÎå¡£
´´½¨Ò»¸öÃûΪsrc/test/resources/ hellocucumber/is_it_friday_yet.featureµÄÎļþ£¬ Îļþ°üÀ¨ÒÔÏÂÄÚÈÝ:
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
Then I should be told "Nope" |
Õâ¸öÎļþµÄµÚÒ»ÐÐÒԹؼü×Ö'''Feature'''¿ªÊ¼:ºóÃæ¸ú×ÅÒ»¸öÃû³Æ¡£×îºÃʹÓÃÓëÎļþÃûÀàËÆµÄÃû³Æ¡£
µÚ¶þÐÐÊǶԸÃÌØÐԵļòÒªÃèÊö¡£Cucumber²¢²»Ö´ÐÐÕâÒ»ÐУ¬ËüÖ»ÊÇÒ»¸öÎĵµ¡£
µÚ4ÐУ¬³¡¾°:Sunday is not FridayÊÇÒ»¸öscenario£¬ËüÊÇ˵Ã÷Èí¼þÓ¦¸ÃÈçºÎ¹¤×÷µÄ¾ßÌåʾÀý¡£
×îºóÈýÐÐÒÔGiven¿ªÍ·£¬WhenºÍThenÊÇÎÒÃǵij¡¾°µÄ²½Öè¡£Õâ¾ÍÊÇCucumber½«ÒªÖ´ÐеIJÙ×÷¡£
¿´Ò»¸ö䶨ÒåµÄscenario±¨¸æ
ÏÖÔÚÎÒÃÇÓÐÁËÒ»¸ö³¡¾°£¬ÎÒÃÇ¿ÉÒÔÈÃCucumberÖ´ÐÐËü£º
Cucumber¸æËßÎÒÃÇÓÐÒ»¸öundefinedµÄ³¡¾°ºÍÈý¸öundefinedµÄ²½Öè¡£Ëü»¹½¨ÒéÎÒÃÇʹÓÃһЩ´úÂëÆ¬¶ÎÀ´defineÕâЩ²½Öè:
-------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/ is_it_friday_yet.feature:4
Given today is Sunday # null
When I ask whether it's Friday yet # null
Then I should be told "Nope" # null
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.040s
You can implement missing steps with the snippets
below:
@Given("^today is Sunday$")
public void today_is_Sunday() {
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet()
{
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String arg1) {
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
} |
¸´ÖÆÒÔÉÏ䶨Òå²½ÖèµÄÈý¸ö´úÂëÆ¬¶Î£º
@Given("^today
is Sunday$")
public void today_is_Sunday() {
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet()
{
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String arg1) {
// Write code here that turns the phrase above
into concrete actions
throw new PendingException();
} |
²¢½«ËüÃÇÕ³Ìùµ½src/test/java/hellocucumber/steps.javaÖС£
¿´Ò»¸öpendingµÄscenario±¨¸æ
ÔÙ´ÎÔËÐÐCucumber: mvn test¡£Õâ´ÎµÄÊä³öÂÔÓв»Í¬:
------------------------------------
T E S T S
------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/ is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
cucumber.api.PendingException: TODO: implement
me
at hellocucumber.Stepdefs.today_is_Sunday (Stepdefs.java:12)
at ?.today is Sunday(hellocucumber/ is_it_friday_yet.feature:5)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.188s
cucumber.api.PendingException: TODO: implement
me
at hellocucumber.Stepdefs.today_is_Sunday (Stepdefs.java:12)
at ?.today is Sunday(hellocucumber/ is_it_friday_yet.feature:5) |
CucumberÕÒµ½ÎÒÃǵIJ½Ö趨Òå²¢Ö´ÐÐËüÃÇ¡£ËüÃǵ±Ç°±»±ê¼ÇΪpending£¬ÕâÒâζ×ÅÎÒÃÇÐèÒªÈÃËüÃÇ×öһЩÓÐÓõÄÊÂÇé¡£
¿´Ò»¸öfallingµÄscenario±¨¸æ
ÏÂÒ»²½Êǰ´ÕÕ²½Ö趨ÒåÖеÄ×¢ÊÍËù¸æËßÎÒÃǵÄÈ¥×ö:
Write code here
that turns the phrase above into concrete actions |
³¢ÊÔÔÚ´úÂëÖÐʹÓÃÓë²½ÖèÖÐÏàͬµÄµ¥´Ê¡£
½«²½Ö趨Òå´úÂë¸ü¸ÄΪ:
package hellocucumber;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import static org.junit.Assert.*;
class IsItFriday {
static String isItFriday(String today) {
return null;
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
@Given("^today is Sunday$")
public void today_is_Sunday() {
today = "Sunday";
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet()
{
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer)
{
assertEquals(expectedAnswer, actualAnswer);
}
} |
ÔÙ´ÎÔËÐÐmvn test :
-------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/ is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "Nope" # Stepdefs. i_should_be_told(String)
java.lang.AssertionError: expected:<Nope>
but was:<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals (Assert.java:834)
at org.junit.Assert.assertEquals (Assert.java:118)
at org.junit.Assert.assertEquals (Assert.java:144)
at hellocucumber.Stepdefs.i_should_be_told (Stepdefs.java:30)
at ?.I should be told "Nope" (hellocucumber/is_it_friday_yet.feature:7)
Failed scenarios:
hellocucumber/is_it_friday_yet.feature:4 # Sunday
isn't Friday
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m0.404s |
Õâ¾ÍÊǽø²½! µ«Ç°Á½²½ÒѾpassing£¬×îºóÒ»²½È´failingÁË¡£
¿´Ò»¸öpassingµÄscenario±¨¸æ
ÈÃÎÒÃÇ×ö×î¼òµ¥µÄÊÂÇéÀ´È󡾰ͨ¹ý¡£ÔÚ±¾ÀýÖУ¬ÕâÖ»ÊÇΪÁËÈÃÎÒÃǵķ½·¨·µ»ØNope :
static String
isItFriday (String today) {
return "Nope";
} |
ÔÙ´ÎÔËÐÐmvn test:
-------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.255s |
¹§Ï²Äã!ÕâÊǵÚÒ»¸ö³É¹¦(passing)µÄCucumber Scenario¡£
Ìí¼ÓÁíÒ»¸öʧ°ÜµÄ²âÊÔ
ÏÂÒ»¼þÒª²âÊÔµÄÊÂÇéÊÇ£¬ÎÒÃÇÒ²»áÔÚÖÜÎåµÃµ½ÕýÈ·µÄ½á¹û¡£
¸üÐÂis-it-friday-yet.featureÎļþ:
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
Then I should be told "Nope"
Scenario: Friday is Friday
Given today is Friday
When I ask whether it's Friday yet
Then I should be told "TGIF" |
ÎÒÃÇÐèÒªÌí¼ÓÒ»¸ö²½Ö趨Ò壬½«todayÉèÖÃΪ¡°Friday¡±:
@Given("^today
is Friday$")
public void today_is_Friday() {
this.today = "Friday";
} |
µ±ÎÒÃÇÔËÐÐÕâ¸ö²âÊÔʱ£¬Ëü½«Ê§°Ü¡£
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/isitfriday.feature:4
Given today is "Sunday" # Stepdefs.today_is(String)
When I ask whether is's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario: Friday is Friday # hellocucumber/is_it_friday.feature:9
Given today is "Friday" # Stepdefs.today_is(String)
When I ask whether is's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
org.junit.ComparisonFailure: expected:<[TGIF]>
but was:<[Nope]>
at org.junit.Assert.assertEquals (Assert.java:115)
at org.junit.Assert.assertEquals (Assert.java:144)
at hellocucumber.Stepdefs. i_should_be_told(Stepdefs.java:26)
at ?.I should be told "TGIF" (hellocucumber/is_it_friday.feature:12)
org.junit.ComparisonFailure:
Expected :TGIF
Actual :Nope
<Click to see difference>
at org.junit.Assert.assertEquals (Assert.java:115)
at org.junit.Assert.assertEquals (Assert.java:144)
at hellocucumber.Stepdefs. i_should_be_told(Stepdefs.java:26)
at ?.I should be told "TGIF" (hellocucumber/is_it_friday.feature:12) |
ÄÇÊÇÒòΪÎÒÃÇ»¹Ã»ÓÐʵÏÖÂß¼!ÎÒÃǽÓ×Å×ö¡£
ÈÃËüͨ¹ý
ÎÒÃÇÓ¦¸Ã¸üÐÂÎÒÃǵÄÓï¾äÀ´Êµ¼ÊÆÀ¹ÀTodayÊÇ·ñµÈÓÚ¡°Friday¡±¡£
static String
isItFriday(String today) {
if (today.equals("Friday")) {
return "TGIF";
}
return "Nope";
} |
ÔÙ´ÎÔËÐÐmvn test:
-------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Friday is Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Friday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
2 scenarios (2 passed)
6 steps (6 passed)
0m0.255s |
ʹÓñäÁ¿ºÍʵÀý
ËùÒÔ£¬ÎÒÃǶ¼ÖªµÀÒ»ÖÜÖв»Ö¹ÖÜÈÕºÍÖÜÎå¡£ÈÃÎÒÃǸüÐÂÎÒÃǵÄscenarioÒÔʹÓñäÁ¿²¢ÆÀ¹À¸ü¶àµÄ¿ÉÄÜÐÔ¡£ÎÒÃǽ«Ê¹ÓñäÁ¿ºÍʾÀýÀ´¼ÆËãÐÇÆÚÎå¡¢ÐÇÆÚÌìºÍÆäËûÈκÎʱ¼ä!
¸üÐÂis-it-friday-yet.featureÎļþ¡£×¢Ò⣬µ±ÎÒÃÇ¿ªÊ¼Ê¹Óöà¸öExamplesʱ£¬ÎÒÃÇÊÇÈçºÎ´ÓÒ»¸öScenarioÇл»µ½Scenario OutlineµÄ¡£
Feature: Is it
Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
| day | answer |
| Friday | TGIF |
| Sunday | Nope |
| anything else! | Nope | |
ÎÒÃÇÐèÒªÓÃÒ»¸öÒÔΪ×Ö·û´®µÄ²½Ö趨ÒåÀ´Ìæ»»today is SundayºÍtoday is FridayµÄ²½Ö趨Òå¡£¸üÐÂstepdefs.javaÎļþÈçÏÂ:
package hellocucumber;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import static org.junit.Assert.*;
class IsItFriday {
static String isItFriday(String today) {
if (today.equals("Friday")) {
return "TGIF";
}
return "Nope";
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
@Given("^today is \"([^\"]*)\"$")
public void today_is(String today) {
this.today = today;
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet()
{
this.actualAnswer = IsItFriday.isItFriday(today);
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer)
{
assertEquals(expectedAnswer, actualAnswer);
}
} |
ÔÙ´ÎÖ´ÐÐmvn test:
-------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
# hellocucumber/is_it_friday_yet.feature:4
Given today is <day> # hellocucumber/is_it_friday_yet.feature:5
When I ask whether it's Friday yet # hellocucumber/is_it_friday_yet.feature:6
Then I should be told <answer> # hellocucumber/is_it_friday_yet.feature:7
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Examples:
| day | answer |
| "Friday" | "TGIF" |
| "Sunday" | "Nope" |
| "anything else!" | "Nope"
|
3 scenarios (3 passed)
9 steps (9 passed)
0m0.255s |
ÖØ¹¹
ÏÖÔÚÎÒÃÇÓÐÁ˹¤×÷´úÂ룬ÎÒÃÇÓ¦¸Ã×öÒ»Ð©ÖØ¹¹:
ÎÒÃÇÓ¦¸Ã½«isItFriday·½·¨´Ó²âÊÔ´úÂëÒÆµ½Éú²ú´úÂëÖС£
ÎÒÃÇ¿ÉÒÔÔÚij¸öʱºò´Ó²½Ö趨ÒåÖÐÌáÈ¡helper·½·¨£¬ÓÃÓÚÎÒÃÇÔÚ¼¸¸öµØ·½Ê¹Óõķ½·¨¡£
|