Write tests

Ready for review -- Last peer review: 27 Sep 2014
Learn to write an automated test for a Drupal core bug.

Automated testing is important to create a easy method to verify patches. In this lesson, you will use the Drupal API to create unit tests to reproduce the issue and verify bug fixes. You will create a test that will reproduce the issue and fail when the bug is present, and pass when the bug is fixed.
Students may have a look at Automated Tests and Testing/SimpleTest for more information on automated tests.

  • Basic PHP knowledge
  • Git and Drupal 8 installed (see previous lessons)
  1. Find an issue in the issue queue that needs tests (head to the Advanced Search and search for one tagged with "needs tests"). You can find one directly in Issue Queue (tagged as "needed tests")
  2. Follow the instructions on the issue page to reproduce the bug on your site. If you can not reproduce the bug, post a comment and choose another issue.
  3. Find the file which should contain the automated test. Head to core/modules/my_module/src/Tests/.
  4. In the directory, add a new class or extend a preexisting class(in case there exists some class which checks for similar bugs). You just need to write a simple test to reproduce the bug, and henceforth assert whether the expected result is obtained after the application of patch or not.
  5. Reproduction of bugs can be done using methods like drupalGet() and drupalPost(), and then check for expected results with methods like assertText() and assertTrue().
  6. Create a patch containing the test code. Upload the patch to the issue site.
  7. ***Its a good practice to run the test both pre- and post-application of patch***