(PHP) using behat and mink to do BDD (behavioral test-driven development) _php tutorials for Web applications

Source: Internet
Author: User
First, according to my own understanding, what is the difference between TDD and BDD?

The first is the difference in thinking, the traditional TDD is concerned about whether the interface is implemented correctly, so usually each interface has a corresponding unit test class. BDD, through story templates and scenarios, describes how the product behaves in a user's context, somewhat like a traditional use case. Writing BDD tests is more like a test case that testers normally write. Since BDD is generally expressed in natural language, it is not a developer who can understand it. Here is an example of BDD:



Behat is a BDD framework under PHP that can be used with phpunit.
More detailed introduction can go to Behat's official website to view, http://behat.org/

When testing Web applications in an automated way, there are some problems with both TDD and BDD when encountering interface testing.
There are usually two solutions:
1. Bypass the GUI for testing. Since most web development uses the MVC framework, the controller simply passes the correct data to view, based on the MVC principle, to complete the task. As a result, we only need to verify that the controller is passing the correct data to the specified view when doing the testing, and that if the view is able to present the data correctly, it will be verified by the integration test.
2. Use some GUI test framework or browser simulator. Today I'm going to talk about using the browser emulator.
There are probably two types of browser emulators:
Headless Browser Emulators-browser emulators, that can is 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, CS S) They is totally limited. But they is 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 say this simulator is faster than the real browser, but cannot handle CSS or JS.
In-browser emulators-this emulators works with real browsers, taking full controll of them and using them as zombies f or their testing needs. This is the Standart fully-configured Real Browser, which you'll be able to controll. CSS styling, JS and AJAX Execution-all supported out of the box.
• This simulator is closer to the real browser, can handle CSS and JS, but the speed is relatively slower than the first.
But when it really does the test, the general two simulators need to be used. Because you can't use the In-browser simulator to do all the testing, because when the test is relatively long, the speed is unbearable. But when you need to test a similar AJAX request, you cannot use the Headless emulator, you must use the In-browser emulator.

Because the two simulators are very different, each with a different API, and the use of two sets of APIs to do testing is very cumbersome.
At this point the advantage of mink comes out, mink is a browser emulator abstraction layer, with the same set of APIs supporting both In-browser and headless simulator.

Here's how to use behat and mink to do BDD for Web apps.
1. Installing Behat
$ pear Channel-discover pear.symfony.com
$ pear Channel-discover pear.behat.org
$ pear Install Behat/behat
2. Installing 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
Then Behat will help you create a features directory and features/bootstrap/featurecontext.php file
Featurecontext.php is the place to actually store the unit test method

4. Create a Features/search.feature file




But if we do not use mink and run the behat command directly, Behat will advise you in features/boostrap/ featurecontext.php plus some test methods, you need to assert yourself in these test methods using scripts like selenium.



5. But if you use mink, it's a lot easier, 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 Behat\mink\behat\context\minkcontext

6. Execute the BEHAT-DL command, and if all is normal, you will see the result of being tired like this



These are actually some of the APIs supported by Mink.

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




In this case, even if a BDD test is completed, the whole process is simple, not much longer than writing a test case, and getting the functionality that the test cases do not have-you can automate the verification function.

Therefore, individuals who feel that they can try to use this method instead of test cases can save a lot of time.

For the development of the students, I personally think that the Web application to do BDD or TDD, should be used to bypass the GUI test scenario, that is, only the controller test.

There are several reasons for this:

1. Developers and testers of the test run speed requirements are not the same, testers are not very fast to run the test, they can put the test up, and then to do other things, one will come back to see the results. But developers are different, and they need to run a complete test every time they modify a piece of code to ensure that their modifications do not break other functionality. If it takes time to run a complete test, then there will be a developer who wants to do the unit test. Using mink is faster than accessing a real browser, but because it's always access to the real product code, it's because many tests will access the database, and once a lot of testing is done, it's time-consuming to run all the tests.

2. Since the use of mink is to access the real product code, it means that you need to configure the relevant environment (dependent libraries, databases, etc.), and even need to restore the database to the initial state. As long as there is an error in any of the configuration links, it is possible that the unit tests passed will fail. It is not possible to achieve the standard of a good unit test. A good unit test must run at any time to ensure that the results are consistent. To achieve this, you cannot rely on external conditions (for example, databases, networks), and for these external conditions you should use code to simulate as much as possible.


From NEE ' s Blog

http://www.bkjia.com/PHPjc/478296.html www.bkjia.com true http://www.bkjia.com/PHPjc/478296.html techarticle first, according to my own understanding, what is the difference between TDD and BDD? The first is the idea of the difference, the traditional TDD is concerned about whether the interface is correctly implemented, so usually each ...

  • 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.