Automated Testing of Drupal 8 Modules

article
#1

Originally published at: http://www.sitepoint.com/automated-testing-drupal-8-modules/

In this article we are going to look at automated testing in Drupal 8. More specifically, we are going to write a few integration tests for some of the business logic we wrote in the previous Sitepoint articles on Drupal 8 module development. You can find the latest version of that code in this repository along with the tests we write today.

But before doing that, we will talk a bit about what kinds of tests we can write in Drupal 8 and how they actually work.

Simpletest (Testing)

Simpletest is the Drupal specific testing framework. For Drupal 6 it was a contributed module but since Drupal 7 it has been part of the core package. Simpletest is now an integral part of Drupal core development, allowing for safe API modifications due to an extensive codebase test coverage.

Right off the bat I will mention the authoritative documentation page for Drupal testing with Simpletest. There you can find a hub of information related to how Simpletest works, how you can write tests for it, what API methods you can use, etc.

By default, the Simpletest module that comes with Drupal core is not enabled so we will have to do that ourselves if we want to run tests. It can be found on the Extend page named as Testing.

Once that is done, we can head to admin/config/development/testing and see all the tests currently available for the site. These include both core and contrib module tests. At the very bottom, there is also the Clean environment button that we can use if any of our tests quit unexpectedly and there are some remaining test tables in your database.

Continue reading this article on SitePoint

0 Likes

#2

One major oversight in this article

Avoid using Simpletest when possible

The plan is to phase out Simpletest by Drupal 9 in favor of PHPUnit. Whenever possible build functionality such that it can be tested from PHPUnit, not simpletest. The only thing Simpletest is used for these days are full system tests, but ideally a module should not be so interlaced with the rest of the system as to require a full system test on it.

1 Like

#3

Interesting. Reference?

0 Likes

#4

https://www.drupal.org/phpunit
The testing framework PHPUnit was added to Drupal 8 in June of 2013. Simpletest is still supported but should only be used for web tests and DrupalUnitTest's that require a complete or partial Drupal environment.

The plans for Drupal 9 were mentioned to me in IRC. It will be several years before that point is reached and certain types of tests cannot be done in PHPUnit yet so knowing how to use Simpletest isn't invalid. Also, Drupal itself needs to be transitioned to a more component based model to be able to completely phase out Simpletest.

1 Like

#5

Well.. you can join my Behat initiative for Drupal 8 - http://www.gizra.com/content/simpletest-behat-drupal-8/

0 Likes

#6

I honestly would rather see just 1 unit test framework in place. Less code to maintain that way. In any event, my main focus at this time is introducing assert to the code base.

0 Likes

#7

Hey there,

Thanks for the feedback. Indeed, whenever possible, it's best to use unit testing. Though as you pointed out as well, there are many aspects in D8 that cannot yet be tested like that and require Simpletest.

0 Likes

closed #8

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.

0 Likes