0

Hi and thanks in advance for your patience - I really appreciate the patience this community has shown me despite my silly questions. I'm using Drupal 9 and have just read this tutorial on unit testing in Drupal 7, but it doesn't seem to be working for me. It's unclear to me whether I'm doing something wrong or the instructions simply aren't valid anymore:

www-data@fa5bab330dc3:/opt/drupal/web$ php core/scripts/run-tests.sh --verbose --url http://example.com/ --class "Drupal\Tests\rsvplist\Unit\RSVPListServiceTest"
  ERROR: Test class not found: Drupal\Tests\rsvplist\Unit\RSVPListServiceTest

I have tried pointing run-tests.php to a specific directory but that doesn't seem to work either:

www-data@fa5bab330dc3:/opt/drupal/web$ php core/scripts/run-tests.sh --verbose --url http://example.com/ --directory modules/custom/
  ERROR: No valid tests were specified.

I tried the above with variations on the directory, including modules/custom/rsvplist and /opt/drupal/web/modules/custom/rsvplist/tests/src/unit/ (the location of the test file itself), but always got the same error.

When running unit tests via phpunit directly I seem to be able to get it to work:

www-data@fa5bab330dc3:/opt/drupal/web/core$ phpunit ../modules/custom/rsvplist
PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

Testing ../modules/custom/rsvplist
R                                                                   1 / 1 (100%)

Time: 123 ms, Memory: 6.00 MB

There was 1 risky test:

1) Drupal\Tests\rsvplist\Unit\RSVPListServiceTest::testSomething
This test did not perform any assertions

/opt/drupal/web/modules/custom/rsvplist/tests/src/unit/EnablerServiceTest.php:14

/opt/drupal/vendor/phpunit/phpunit/src/Framework/TestResult.php:860
/opt/drupal/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
/opt/drupal/vendor/phpunit/phpunit/src/Framework/TestSuite.php:761
/opt/drupal/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:632
/opt/drupal/vendor/phpunit/phpunit/src/TextUI/Command.php:208
/opt/drupal/vendor/phpunit/phpunit/src/TextUI/Command.php:164

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.

I'd like to run my tests via run-tests.sh as I'd like to use the Drupal\KernelTests\KernelTestBase class so that I can mock out database connections, and as I understand it this should be done via run-tests.sh and not phpunit. Obviously I'd like to resolve the errors above but I'm also happy to be shown a different / better way of achieving what I'm trying to do.

Here is my test class, located in modules/custom/rsvplist/tests/src/unit/EnablerServiceTest.php:

<?php

namespace Drupal\Tests\rsvplist\Unit;

use Drupal\Tests\UnitTestCase;

class RSVPListServiceTest extends UnitTestCase {
  public function testSomething() {
  }
}
3
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Sep 13, 2020 at 8:25
  • @clive I think in this instance the comments were relevant dialogue as they explained the steps needed to diagnose the issue. They might have been helpful to others.
    – quant
    Sep 13, 2020 at 23:05
  • @quant Anything posted in comments that is relevant should be added to the question (if it's not part of the answer) or added to an answer. Comments are ephemeral by their own nature: They can be removed at any time, when they don't have a purpose anymore.
    – apaderno
    Sep 20, 2020 at 12:00

1 Answer 1

1

The solution ended up being:

  1. Make sure the test file is named as the test class, in this case RSVPListServiceTest.php.
  2. Make sure my unit tests are placed in the correct directory structure, in my case my RSVPListServiceTest.php file had to be in modules/custom/rsvplist/tests/src/Unit/EnablerServiceTest.php (as opposed to unit with a lowercase u)

The ERROR: No valid tests were specified. message ended up being a bit of a red herring, and I've submitted a patch to drupal to make this message clearer: https://www.drupal.org/project/drupal/issues/3170651

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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