Automated Testing in Drupal 8: Get More Efficient, More Confident to... Deploy

Feeling your hands getting sweaty every time you need to hit the deploy button? What if you ship compromised code to production and you break a few months' work? Who knows how many bugs will just start swarming around? Now, instead of having panic attacks over and over again, why don't you leverage automated testing in Drupal 8?

“But how precisely can I implement it within Drupal core, within my custom Drupal website or my contributed project? What are my testing options at hand?”

“What type of tests can I write and what automated testing tools can I easily integrate with my Drupal 8 project?”

Time for some answers, right? 

1. What's the Difference Between Automated and Manual Testing?

Pre-scripted tests that run... automatically.

This must be the most concise, yet complete definition of automated testing.

As opposed to manual testing, where you'd have your QA team test the application against all the conditions clearly defined in a test case, automated testing:

Sets the current results against the expected output.

This way, you'll instantly get a clear picture of whether your Drupal 8 website/application works as planned.

Furthermore, automated testing withstands both iterative and regression tests. You get to determine, on the spot, how well your project copes with newly implemented features.

2. What Are the Benefits of AutomatedTesting?

Or “What are the limitations of manual testing that automated tests... exploit?” if you prefer.

Here are just a few of the clear advantages:

  • you get to re-use your automatically run tests
  • you can now automate all those lengthy, discouragingly complex tests that used to... intimidate you
  • you get to detect bugs and compromised functionality at an early stage of the development lifecycle 
  • increased coverage
  • you get to test your app/website while being trafficked by thousands of virtual visitors
  • you get to test your piece of software on multiple devices, simultaneously
  • you can reiterate the same tasks, over and over again, and still get the same precision of the delivered results

3. What Precisely Can You Test... Automatically?

Pretty much anything that you'd rather not test... manually.

But, to be more specific, here's a short list of functionalities and different parts of your software where automated testing in Drupal 8 makes the best testing approach:

  • workflows
  • API integrations (you can automate mock API)
  • access control
  • bug fixes: you get to replicate the detected bug, ensure the test fails, then address the issue
  • email notifications
  • form submissions (you can automate filling out long form submissions)

4. Automated Testing in Drupal 8: Test Types Available

For yes, Drupal 8, unlike Drupal 7, provides you with an entire spectrum of automated testing options.

From unit testing for Drupal to functionality tests to Behat tests, to... kernel tests, you're free to test your Drupal website in a multi-layered way. 

Basically, with every type of test that you write and run, you're testing specific features and capabilities of your Drupal 8 app/website.

And here are some of the most popular automated tests that you can “juggle with” in Drupal 8:

4.1. Unit Tests

Think of unit tests in the context of automated testing in Drupal 8 as:

Tools to help you test those components of your code that don't depend on Drupal: methods, individual classes, functions.

Or as “tools” to use for testing the smallest units of your Drupal 8 app/software project.

Main features:

  • PHPUnit is the underlying framework
  • they use mocked class dependencies
  • no database

4.2. Behat Tests

You can also use a framework like Behat to test your Drupal 8 project as a whole by leveraging:

  • gherkin syntax scenarios
  • theme-dependent scenarios
  • configuration-dependent scenarios

Its key benefit? It's fast.

It'll just “turbocharge” your whole Drupal 8 automated testing process.

Furthermore, with the Drupal extension “merging” Drupal, Behat, and Mink Extension via an integration layer, you're even more motivated to integrate Behat into your testing process.

And here are just some ways that you can “exploit” this extension:

  • to clear cache
  • to define test data using the Drupal API or Drush 
  • to define theme regions and test the data they include
  • to identify steps provided by contributed themes and modules

4.3. Nightwatch.js Tests

They make the latest “addition” to the collection of automated tests available in Drupal 8. 

Their key characteristic? They run on a 100% JS testing framework...

When/why would you run Nightwatch.js tests? Whenever you need to test a part of your app's UI that's JavaScript dependent.

4.4. Functional Tests

Run automated functional tests to evaluate your Drupal 8 website's performance.

To determine to what degree it meets all the pre-defined functional requirements...

Key benefits of functional testing:

  • it installs all schemas, configurations and module dependencies
  • it comes with full “testing” install profile
  • functional tests are written in PHP
  • it makes the best testing approach to evaluate suitability, accuracy, interoperability, compliance

When precisely should you run functional tests? Whenever you need to walk into the user's shoes and test your app's UI, except for those cases when you need JavaScript (they don't support JavaScript).

4.5. Kernel Tests

Take them as more powerful versions of the unit tests.

And yet, they're not fine-grained like unit tests and they're not full integration tests either. Where do you add that they don't provide a working UI...

So, why should you use kernel tests during your automated testing in Drupal 8?

To test parts of your module interacting with Drupal, but that aren't dependent of a working UI: module APIs, hooks, plugins, services...

Main features:

  • specified entity types available only
  • only specified modules, schemas and configuration installed
  • written in PHP

The END!

This is how using automated testing in Drupal 8 helps you get:

  • more efficient
  • more productive
  • more confident

… on your “deployment day”.

And also your most powerful options at hand, to refine and to automate your entire testing process...


Photo by Helloquence on Unsplash