3

Having just kind of gotten my head around Simpletest functional testing in Drupal 8.x, I've now started trying to use PHPUnit testing since Simpletest has been deprecated in Drupal 9.1.x.

I started by looking at the instructions on Running PHPUnit tests. However, when I get to the step on running the tests I get this.

Fatal error: Uncaught Error: Class "Drupal\Tests\node\Functional\NodeTestBase" not found in /opt/drupal/web/core/modules/node/tests/src/Functional/PagePreviewTest.php:22

This looks a bit like an autoload issue to me. I ran the following command to verify it was effectively so.

php -r "require_once('autoload.php'); var_dump(class_exists('\Drupal\Tests\node\Functional\PagePreviewTest'));"

The command outputs bool(false).

My knowledge really ends here. The documentation seems to be moot on this, and it looks like I'm not the only one with this problem. (See How to run PHPUnit tests?).

How do I run PHPUnit tests?

This is the Docker file content that can be used to reproduce what I described in the question.

FROM drupal:9.1.0

RUN composer require -n \
  phpunit/phpunit:9.5.0 \
  phpspec/prophecy-phpunit:2.0.1 \
  symfony/phpunit-bridge:5.2.1

RUN cd web/core; phpunit -c phpunit.xml.dist modules/node/tests/src/Functional/NodeTestBase.php

Building this causes the error I described above.

4
  • You're using the core dev version 9.1.x-dev, right?
    – No Sssweat
    Dec 25 '20 at 21:15
  • @NoSssweat I'm using 9.1.0 docker image for testing. Should I be using the dev version?
    – quant
    Dec 26 '20 at 2:34
  • core/modules/node/tests/src/Functional/NodeTestBase.php doesn't exist?
    – anonymous
    Dec 26 '20 at 5:34
  • @anonymous yes the file exists. I've added a dockerfile to reproduce the problem.
    – quant
    Dec 26 '20 at 7:33
2

I believe the problem is that you are using the sample phpunit.xml.dist instead of copying it to phpunit.xml and then making a few teaks for your installation.

Part of the phpunit.xml file is setting up the autoloader to find test classes and modules (they are not in the normal autoloader). And you tend to get some really weird errors if you don't set up the DSN for the database (to run Kernel and Functional tests).

I would do the following (adjust paths as needed ; I'm looking at a core-dev project right now and not a live site).

  • In your install, do composer require --dev drupal/core-dev:~9.1.0. You may want to tweak the version number, depending on you you have core defined.
  • Make sure you are running the binary from vendor/bin in your install. I suggest looking into direnv.
  • Copy phpunit.xml.dist to phpunit.xml and edit
    • SIMPLETEST_BASE_URL
    • SIMPLETEST_DB
    • BROWSERTEST_OUTPUT_DIRECTORY to reflect your installation.

Then cd to core, and run

../vendor/bin/phpunit -c phpunit.xml --testsuite unit

This should start to loop through the Unit tests. Abort that if they look like they are working, and then do

../vendor/bin/phpunit -c phpunit.xml --testsuite kernel

Then, if that looks OK you can start to run your own tests.

All of that said, it is much easier to run individual tests through PhpStorm. You just need to got through the preferences to point to the right PHP interpreter, and set a few things for phpunit.

In your case, it may be better to just start the drupal:9.1.0 container, and then shell into it for initial testing before updating the Dockerfile.

2
  • Drupal.org has also documentation for running DrupalCI on a local computer. While it uses Vagrant instead of Docker, would it be a "easier" way of testing Drupal core and modules and be able to reproduce the output the tester runners on Drupal.org give?
    – apaderno
    Jan 1 at 10:18
  • @apaderno What I described is essentially what I use for core development. I haven't found an advantage for Docker/Vagrant or full DrupalCI locally. I have a bunch of shell scripts for running tests, and if need all that DrupalCI does it is because I need a full testbot run and don't want to do that locally (though I have been toying with setting up a Pi4 Branble to do it).
    – mpdonadio
    Jan 2 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.