(PHP) use Behat and Mink to perform BDD on Web Applications (behavior test-driven development)

Source: Internet
Author: User
First of all, based on my own understanding, let's briefly talk about the differences between TDD and BDD? The first is the difference in thinking. the traditional TDD focuses on whether the interface is correctly implemented. Therefore, each interface usually has a corresponding unit Test class. While BDD describes production through story templates and scenarios... SyntaxHighlighter. all ();


First of all, based on my own understanding, let's briefly talk about the differences between TDD and BDD?

The first is the difference in thinking. the traditional TDD focuses on whether the interface is correctly implemented. Therefore, each interface usually has a corresponding unit Test class. While BDD describes the specific functions of a product during user operations through story templates and scenarios, which is a bit similar to the traditional Use Case. Writing a BDD test is more like a test case written by a tester. Because BDD is generally expressed in natural language, it is understandable even if it is not a developer. Here is an example of BDD:

 

 

Behat is a BDD framework in PHP and can be used with PHPUnit.
For more details, go to behat's official website to view, http://behat.org/
 
When performing automated tests on Web applications and interface tests, both TDD and BDD may cause some problems.
There are usually two solutions:
1. bypass the GUI test. Most Web development uses the MVC framework. according to the MVC principle, the Controller only needs to pass the correct data to the View to complete the task. Therefore, during the test, we only need to verify whether the Controller passes the correct data to the specified View. As for whether the View can correctly display data, it depends on the integration test.
2. use some GUI testing frameworks or browser simulators. Today I want to talk about using browser simulators.
Browser simulators can be divided into two types:
• Headless browser emulators-browser emulators, that can be executed fully without GUI through console. such emulators can do HTTP request and emulate browser applications on high level (HTTP stack), but on lower level (JS, CSS) they are totally limited. but they are much faster than real browsers, cuz you don't need to parse CSS or execute JS in order to open pages or click links with them.
• Simply put, this simulator is faster than a real browser, but cannot process CSS or JS.
• In-browser emulators-this emulators works with real browsers, taking full controll of them and using them as zombies for their testing needs. this way, you'll have standart fully-configured real browser, which you will be able to controll. CSS styling, JS and AJAX execution-all supported out of the box.
• This kind of simulator is closer to the real browser and can process CSS and JS, but it is slower than the first one.
However, in practice, two simulators are generally used. Because you cannot all use the In-browser simulator for testing, because when there are many tests, the speed is intolerable. However, when you need to test Ajax-like requests, you must use the In-browser simulator instead of the Headless simulator.
 
Because these two simulators are very different and have different APIs, it is very troublesome to use two sets of APIs for testing at the same time.
The advantage of MINK is that MINK is a browser simulator abstraction layer that supports both In-browser and Headless simulators using the same API.
 
The following describes how to use Behat and MINK to perform BDD on WEB applications.
1. install Behat
$ Pear channel-discover pear.symfony.com
$ Pear channel-discover pear.behat.org
$ Pear install behat/behat
2. install MINK
$ Pear channel-discover pear.behat.org
$ Pear install behat/mink-beta
3. create a sosozhike Directory, enter the sosozhidao directory, and run the behat -- init command.
$ Mkdir sosobaike
$ Cd sosobaike
$ Behat -- init
Behat will help you create a features directory and the features/bootstrap/FeatureContext. php file.
FeatureContext. php is the place where the unit test method is actually stored.
 
4. create the features/search. feature file

 


 

However, if we do not use MINK and run the behat command directly, behat recommends that you use features/boostrap/FeatureContext. php adds some testing methods, and you need to use scripts like Selenium in these testing methods for assertions.

 

 

5. but if you use MINK, it will be much simpler. you only need to modify the features/bootstrap/FeatureContext. php file,

Add two lines of code.

A) require_once 'Mink/autoload. php ';

B) change the parent class of FeatureContext from BehatContext to Behat \ Mink \ Behat \ Context \ MinkContext.

6. execute the behat-dl command. if everything is normal, you will see the following results.

 

 

The above are actually some APIs supported by MINK.

7. execute the command behat features/search. feature and you will see the following results:

 


 

At this point, even if a BDD test is completed, the entire process is very simple, and it does not take much time to write a test case, and obtain the functions not available in the test case-the verification function can be automatically executed.

 

Therefore, I personally think that you can use this method to replace the test case, which can save a lot of time.

 

For developers, I personally think that when using BDD or TDD for Web applications, we should use a test scheme that bypasses the GUI, that is, only the Controller solution.

The reasons are as follows:

1. developers and testers have different requirements on the test speed. testers do not have high requirements on the test speed. they can start the test and then do other things, you can check the results later. But developers are different. each time they modify a code, they need to run a complete test to ensure that their modifications do not destroy other functions. If it takes a long time to run a complete test, developers may want to perform a unit test. Although using MINK is faster than accessing in a real browser, because it is always accessing the real product code, many tests will access the database, it takes a lot of time to complete all tests.

 

2. because MINK is used to access the real product code, it means that you need to configure the relevant environment (dependent database, database, etc.) and even restore the database to the initial state. Any configuration error may cause the unit test to fail. This cannot meet the standard of a good unit test. A good unit test must run at any time to ensure that the running results are consistent. To achieve this effect, you cannot rely on external conditions (such as databases and networks). use code to simulate these external conditions as much as possible.


From NEE's Blog
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.