Home > Backend Development > PHP Tutorial > (PHP) Use Behat and Mink to do BDD (behavioral test-driven development) for web applications_PHP tutorial

(PHP) Use Behat and Mink to do BDD (behavioral test-driven development) for web applications_PHP tutorial

WBOY
Release: 2016-07-13 17:50:14
Original
1595 people have browsed it


First of all, according to my own understanding, what is the difference between TDD and BDD?

The first is the difference in thinking. Traditional TDD focuses on whether the interface is correctly implemented, so each interface usually has a corresponding unit test class. BDD uses story templates and scenarios to describe the specific functional performance of the product when users operate it, which is somewhat similar to the traditional Use Case. Writing BDD tests is more like test cases that testers usually write. Since BDD is generally expressed in natural language, even non-developers can understand it. The following is an example of BDD:



Behat is a BDD framework under PHP and can be used with PHPUnit.
For a more detailed introduction, you can check out behat’s official website, http://behat.org/

When doing automated testing of web applications, you will encounter some troubles when encountering interface testing, whether using TDD or BDD.
There are usually two solutions:
1. Bypass GUI testing. Since most web development uses the MVC framework, according to the principles of MVC, the Controller only needs to pass the correct data to the View to complete the task. Therefore, when doing testing, we only need to verify whether the Controller passes the correct data to the specified View. As for whether the View can display the data correctly, it depends on the integration test to verify.
2. Use some GUI testing framework or browser emulator. Today I’m going to talk about using a browser emulator.
Browser simulators can be roughly 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 emulator is faster than a real browser, but it cannot handle CSS or JS.
• In-browser emulators - this emulators works with real browsers, taking full control 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 control. CSS styling, JS and AJAX execution - all supported out of the box.
• This simulator is closer to a real browser and can handle CSS and JS, but the speed will be relatively slower than the first one.
But in fact, when actually doing testing, both simulators are generally needed. Because you can't use the in-browser simulator for all tests, because when there are many tests, the speed will be unbearable. But when you need to test similar Ajax requests, you cannot use the Headless simulator and must use the In-browser simulator.

Since the two simulators are very different and each has a different API, it is very troublesome to use two sets of APIs for testing at the same time.
At this time, the advantages of MINK come out. MINK is a browser simulator abstraction layer that supports both in-browser and headless simulators with the same set of APIs.

The following will introduce how to use Behat and MINK to do BDD for 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 new sosobaike directory, enter the sosozhidao directory, and execute the behat --init command
$ mkdir sosobaike
$ cd sosobaike
$ behat --init
At this time behat will help you create a features directory and features/bootstrap/FeatureContext.php file
FeatureContext.php is where the actual methods for unit testing are stored

4. Create features/search.feature file




But if we do not use MINK and run the behat command directly, behat will recommend that you add some test methods to features/boostrap/FeatureContext.php. You need to use scripts like Selenium to perform these test methods yourself. assertion.



5. But if you use MINK, things are much simpler, just 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 BehatMinkBehatContextMinkContext

6. Execute the behat -dl command. If everything is normal, you will see results similar to the following



The above are actually some APIs supported by MINK.

7. Execute the command behat features/search.feature, you will see the following results




At this point, even if a BDD test is completed, the whole process is very simple and does not take longer than writing a test case. It also has a function that the test case does not have - it can automatically execute the verification function.

Therefore, I personally think that students doing testing can try to use this method instead of test cases, which can save a lot of time.

For development students, I personally think that when doing BDD or TDD for web applications, you should use a solution that bypasses GUI testing, that is, a solution that only tests the Controller.

The reasons are as follows:

1. Developers and testers have different requirements for test running speed. Testers do not have high requirements for test running speed. They can start the test, then do other things, and come back later. Just look at the results. But developers are different. Every time they modify a code, they need to run a complete test to ensure that their modifications do not destroy other functions. If running a complete test takes time, then some developers will want to do unit testing. Although using MINK is faster than accessing it in a real browser, since you are always accessing the real product code, many tests will access the database. Once there are many tests, it will be very time-consuming to run all the tests.

2. Since using MINK will access the real product code, it means that you need to configure related environments (dependent libraries, databases, etc.), and even need to restore the database to its initial state. As long as there is an error in any configuration link, it may cause the unit test that originally passed to fail. This fails to meet the standards of a good unit test. Good unit tests must ensure consistent results whenever they are run. To achieve this effect, you cannot rely on external conditions (for example: database, network). These external conditions should be simulated as much as possible using code.


Excerpted from NEE's Blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478296.htmlTechArticleFirst of all, according to my own understanding, what is the difference between TDD and BDD? The first is the difference in thinking. Traditional TDD focuses on whether the interface is implemented correctly, so usually each...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template