How to run PHPUnit test cases on your local machine for Drupal

On27th Aug 2023, 2023-08-27T11:30:00+05:30 Read Pause Resume Stop
How to run PHPUnit test cases on your local machine for Drupal

Running PHPUnit tests helps ensure that your Drupal module behaves as expected and provides a reliable way to identify and fix issues during development.

To run PHPUnit test cases on your local machine for Drupal, follow these steps:

Install PHPUnit and its dependency:

  • Make sure you have PHP and Composer installed on your local machine. If you don't have PHPUnit installed, you can install it using Composer by running the following command:

composer require drupal/core-dev --dev --update-with-all-dependencies

Set up a Drupal Environment & Configure:

  • Install Drupal on your local machine. You can use the Drupal project template or any other method of your choice. Ensure you have a working Drupal site with the module you want to test installed and enabled (In my case using the composer & ddev for the setup).
  • To configure PHPUnit, you need to copy the phpunit.xml.dist file to phpunit.xml and make below changes in this file.

<env name="SIMPLETEST_BASE_URL" value="https://drupal.ddev.site"/>

<env name="SIMPLETEST_DB" value="mysql://db:db@db/db"/>

<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/html/web/sites/simpletest/browser_output"/>

  • Next, create a directory to store the output and change the folder permissions to 777, below shown the commands.

mkdir -p sites/simpletest/browser_output

chmod -R 777 sites/simpletest

Run PHPUnit Tests:

  • Get into the container by running the command ddev ssh.
  • Now, navigate to the root of your Drupal site and execute the following command to run This command runs the PHPUnit tests for your Drupal module. If everything is set up correctly and your tests pass, you will see the test results displayed in the console.

cd web

../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php

  • Below are a few example commands to run the test cases for core & contrib code.

../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php

../vendor/bin/phpunit -c core core/modules/help/tests/src/Functional/HelpPageReverseOrderTest.php

../vendor/bin/phpunit -c core core/modules/action

../vendor/bin/phpunit -c core modules/contrib/admin_toolbar/tests/src/Functional/AdminToolbarAdminMenuTest.php

../vendor/bin/phpunit -c core modules/contrib/admin_toolbar

  • Below is the example output for one such test case.

karthik.dk@drupal-web:/var/www/html/web$ ../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php

PHPUnit 9.6.10 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest

......................                                            22 / 22 (100%)

Time: 00:01.852, Memory: 8.00 MB

OK (22 tests, 62 assertions)

Keep in mind that PHPUnit tests for Drupal modules are organized in specific directories and namespaces within your module's tests folder. Also, ensure that your module adheres to Drupal's coding standards for test classes and methods.

Thanks for reading the article, for more drupal related articles read and subscribe to peoples blog articles.

251OFF
Amazon Brand - Solimo Stainless Steel Induction Bottom Kadhai (25cm), Silver

Amazon Brand - Solimo Stainless Steel Induction Bottom Kadhai (25cm), Silver

449 700
  • A safe and robust cooking utensil made from 100% food grade Stainless Steel.
  • Product Dimensions : 25CM diameter , depth : 7.8CM.
  • Serves various cooking, frying and serving options for daily kitchen needs.
  • Made with high-quality induction bottom that is also gas stove compatible.
  • Sturdy and durable with a thickness of 0.6mm and capacity of 3000ml.
  • A stable grip with double riveted ear handles. Easy to clean in a dishwasher.
Kitchen & Dining
3,000OFF
Lava Storm 5G(Gale Green,8GB RAM,128GB ROM)-Powered by MediaTek Dimensity 6080 Processor|120Hz FHD+ Display|50MP+8MP Ultrawide Camera|16MP FrontCamera|33W Fast Charging|Upto 16GB ExpandableRAM

Lava Storm 5G(Gale Green,8GB RAM,128GB ROM)-Powered by MediaTek Dimensity 6080 Processor|120Hz FHD+ Display|50MP+8MP Ultrawide Camera|16MP FrontCamera|33W Fast Charging|Upto 16GB ExpandableRAM

11,999 14,999
  • Solid Performance with 2.4 Ghz Octa Core MediaTek Dimensity 6080 Processor, Camera - 50 MP + 8 MP Ultrawide Dual Rear Camera with LED Flash and 16 MP Selfie Camera.
  • Also supports 2K Video Recording with EIS Support
  • Premium Glass Back Design
Electronics
6,610OFF
Panasonic 280 L Double Door 3 Star AI Enabled Inverter Technology Frost Free Refrigerator (NR-TH292CPKN, Diamond Black, Jumbo Fresh Vegetable Basket,2023 Model, Net Capacity 256L)

Panasonic 280 L Double Door 3 Star AI Enabled Inverter Technology Frost Free Refrigerator (NR-TH292CPKN, Diamond Black, Jumbo Fresh Vegetable Basket,2023 Model, Net Capacity 256L)

30,590 37,200
  • Double door Frost Free Refrigerator: Stylish design, efficient operations and ample space, making it a perfect addition to a new age home.
  • Keep your food fresher and healthier for longer with this modern and chic refrigerators.
  • 280L Capacity: Suitable for families with 3 to 4 members
  • Net Capacity: 256L
  • 3 Star rating - The star rating changes are as per BEE guidelines on or before 1st Jan 2023.
Refrigerators

Related Articles

2,099OFF
London Hills Women's Casual Printed Round Neck, Oversized Longline Drop Shoulder Boho Style Regular Fit T-Shirt Pack Of 2

London Hills Women's Casual Printed Round Neck, Oversized Longline Drop Shoulder Boho Style Regular Fit T-Shirt Pack Of 2

499 2,598
  • Fit Type: Oversized Fit
  • Fabric: Cotton blend
  • Sleeve Type: Half Sleeve
  • Neck Style: Round Neck
  • Pattern : Printed
Clothing & Accessories

Recent Articles

Recent Quick Read

Recent Great People

641OFF
Amazon Brand - Solimo Stainless Steel Saucepan with Glass Lid, Induction Base (16 cm, 1500 milliliters)

Amazon Brand - Solimo Stainless Steel Saucepan with Glass Lid, Induction Base (16 cm, 1500 milliliters)

459 1,100
  • Stainless steel saucepan for safe and versatile cooking
  • Made using food safe stainless steel that is sturdy and durable
  • Being scratch and chip resistant and non-corrosive makes it an ideal choice for everyday use
  • Suitable for preparing sauces, boiling water, simmering, making soups, stews, tea and more
  • Cool-touch bakelite handle prevents the transfer of heat and provides a firm grip for holding
  • Easy to stack allows minimum space utilisation in the kitchen
  • Easy to clean and maintain as food doesn’t stick to the inner or outer surface
  • Comes with a toughened glass lid that allows you to see the food as it cooks
  • Compatible with gas-stove and induction cooktop
  • Product Dimensions:- Length: 335 mm (33.5 cm), Width: 176 mm (17.6 cm), Height: 125 mm (12.5 cm), Bottom Thickness: 3.5 mm (0.35 cm), Handle Position: 85 mm (8.5 cm), Depth: 80 mm (8 cm), Weight: 795 gm (0.795 kg), Capacity: 1500ml (1.5 litres), Glass Lid diameter: 160 mm (16 cm)
Home & Kitchen
3,000OFF
Redmi 10A (Charcoal Black, 4GB RAM, 64GB Storage) | 2 Ghz Octa Core Helio G25 | 5000 mAh Battery

Redmi 10A (Charcoal Black, 4GB RAM, 64GB Storage) | 2 Ghz Octa Core Helio G25 | 5000 mAh Battery

8,999 11,999
Redmi 10A (Charcoal Black, 4GB RAM, 64GB Storage) | 2 Ghz Octa Core Helio G25 | 5000 mAh Battery | Finger Print Sensor | Upto 5GB RAM with RAM Booster
Mobiles & Accessories
5,000OFF
OnePlus Nord 3 5G (Tempest Gray, 8GB RAM, 128GB Storage)

OnePlus Nord 3 5G (Tempest Gray, 8GB RAM, 128GB Storage)

28,999 33,999
Camera: 50MP Main Camera with Sony IMX890 (OIS supported), 8MP Ultrawide Camera with Sony IMX355 (FOV: 112 degree) and 2MP Macro lens f/2.4 and a FOV 89°; 16MP Front (Selfie) Camera with EIS supported. Camera Features: Photo, Video, Night, Portrait, PANO, TIME-LAPSE, and Dual-view video. Display: 6.74 Inches; 120 Hz AMOLED FHD+ Display with Corning Gorilla Glass Victus; Resolution: 2772 X 1240 pixels; HDR 10+, sRGB, 10-bit Color Depth, PWM + DC dimming. Operating System: OxygenOS based on Android 13.1. Processor: MediaTek Dimensity 9000 Chipset (Based 4nm Architecture). Storage: 8 GB + 128 GB | 16 GB + 256 GB; LPDDR5X RAM, UFS 3.1.
Mobiles & Accessories
6,000OFF
Lava Agni 2 5G (Glass Viridian, 8GB RAM, 256GB Storage) | Indias First Dimensity 7050 Processor | 120 Hz Curved Amoled Display | 13 5G Bands | Superfast 66W Charging | Clean Android

Lava Agni 2 5G (Glass Viridian, 8GB RAM, 256GB Storage) | Indias First Dimensity 7050 Processor | 120 Hz Curved Amoled Display | 13 5G Bands | Superfast 66W Charging | Clean Android

19,999 25,999
17.22cm (6.78 inch) 120Hz FHD+ Curved Amoled Display with Widevine L1 DRM Protection - Enjoy all your Content in High Resolution. Superior performance with Octa-core 2.6GHz MediaTek Dimensity 7050 6nm processor. Superfast 66W charger (50% Charging in less than 16mins). Big 1.0µm Pixel 50MP Quad Camera with 8MP ultrawide, 2MP Macro and 2MP Depth camera along with 1.0µm 16MP selfie camera. Clean Android 13 OS (No Bloatware, No Ads). In-Display Fingerprint Unlock | Supports 13 5G bands.
Mobiles & Accessories
We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies