


(PHP) Use Behat and Mink to do BDD (behavioral test-driven development) for web applications_PHP tutorial
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
