This article has participated in the third “High Yield more text” track of the Denver Creators Training Camp, check out the details:Digg project | Creator Boot Camp phase 3 is underway, “write” to make a personal impact.

BDD behavior drives development

BDD is an extension of TDD, a shift in thinking: testing into behavior. The core logic is to write use cases first, then add/create program code; The differences are: 1. Test cases are written in descriptive English type grammar; 2. The steps of test case realization are the program under testCopy the code

The function of BDD

Moving from "testing" to "behavior" thinking The language of collaboration between business stakeholders, business analysts, quality assurance teams and developers is everywhere, It's easy to describe business value driven by extending test Driven Development (TDD) by using natural languages that non-technical stakeholders can understand. A BDD framework like Cucumber or JBehave is an enabler that acts as a bridge between business and technical languageCopy the code
  • What is the Cucumber
Cucumber is a testing framework that supports behavior Driven Development (BDD). It enables us to define application behavior in simple and meaningful English text using a simple syntax defined by a language called Gherkin. Cucumber itself is written in Ruby, but can be used to "test" code written in Ruby or other languages including but not limited to Java, C# and Python.Copy the code
  • Its counterpart is.net’s SpecFlow framework
  • Why BDD framework?
The main feature of cucumber is its focus on acceptance testing. It makes it easy for anyone on the team to read and write tests, and it helps the team explore and understand requirements by bringing business users into the testing process with this capability. As a result, it eliminates errors that may result from poor communication or misinterpretation during development. It's also important to note that Cucumber is a BDD framework that anyone can understand and execute.Copy the code

Cucumber demo

  • Taking Baidu search as an example, the following use cases are designed:
    • Baidu search box input content length?
    • Baidu search box search content does not exist
    • Baidu search box search results?
    • Baidu search box input character limit
    • And so on function description or more detailed description; As long as we have a clear understanding of user requirements and usage scenarios, we can better develop functions and reduce unnecessary problems.

I understand behavior driven development

As mentioned earlier, behavior-driven, the subject of testing is either the test case or the automated testing framework itself; Keywords need to be developed. All features correspond to keywords such as @given, @WHEN and @THEN in step. There is no one-to-one featur-->step file. Instead, the test steps are performed by matching keywords. Feature, as a descriptive scripting language, reduces communication costs, is easy to understand, and clearly knows test intentions.Copy the code

Cucumber environment construction

  1. Eclipse tools integrate cucumber framework, visit the official website for more features;

  2. Pom configures cucumber dependency

  1. Eclipse is not compatible or cucumber cannot be installed Using the MVN command, you can quickly create cucumber projects

mvn archetype:generate -DarchetypeGroupId=io.cucumber -DarchetypeArtifactId=cucumber-archetype - DarchetypeVersion = 2.3.1.2 - DgroupId = cucumberDemo - DartifactId = cucumberDemo - Dpackage = cucumberDemo - Dversion = 1.0.0 - the SNAPSHOT - DinteractiveMode = false

  1. Open the IDE development tool or go to the project directory you created: MVN Test to check whether cucumber is installed successfully

  1. Before writing the script, you need to know the Features syntax rules < feature documentation >&&Gherkin keyword

Features Is based on Gherkin, a simple English text language, which must be named with Features at the end of the file, including title<Features>, can have multiple scenarios, each scenario by multiple steps; Step definitions must begin with the Given, When, And, And Then keywords.

Example:
Feature: name of the Feature to be tested Scenario: describes the requirement, which can be newline Given: preconditions When: When: execute the condition And: And: execute the result, which can be multiple Then But: As for the order before the key words, it is the same as that in Chinese.Copy the code
  1. Create a. Feature file in the resources directory with the following contents:
Feature: Baidu search cucumber?

Everybody can to do this?

Scenario: search nothing?

Given open the browser

When we input samething

And click searchBtn

Then result is success

Then close the browser

Copy the code
  1. Run the MVN test command. The result is as follows:

  1. Copy the above code steps in the red box to stepdfs.java and write the script code

  1. Cucumber Official Tutorial

10. Cucumber data driver mode: the Examples keyword is supported by cucumber itself

conclusion

This concludes the introduction to BDD and environment setup. What is the use of BDD? Why? What is the revenue? These are also described in general; Different people have different opinions!