6

the last few weeks and days I have been reading into Drupal 8 testing with phpunit.

For a project I want to use Unit testing to test out functionality and services and the cooperation between different components.

Run the tests against an existing database / installation and not against a fresh Drupal installation, since we have configured a lot already and I need to make sure the new code does work with the whole configuration and data.

I can Unit tests to test out simple functions or classes but what I cannot use is the BrowserTests or KernelTest bases since they all create a new, fresh installation.

Maybe I have misunderstood something or I am on the wrong path.

Can anybody help me out or has an example on how to do that?

4
  • You should search SE or post a new question regarding your final question. It is better to show an example of what you have tried and where specifically you are stuck on testing services or requiring the container.
    – mradcliffe
    Commented Mar 23, 2017 at 14:03
  • Generally you provide those tests with just enough data, like config files and in the setUp() perform other tasks.
    – Kevin
    Commented Mar 23, 2017 at 15:01
  • @mradcliffe That is the problem, I have absolutely no idea how to go about it, which is why I posted this question in the first place.
    – Martin
    Commented Mar 23, 2017 at 15:12
  • @Kevin Ok, sounds like an idea. is there a tutorial or example where this is being shown or explained?
    – Martin
    Commented Mar 23, 2017 at 15:14

1 Answer 1

2

I have once answered this exact same question for Drupal 7. Though some things have changed, the concept remains the same; extend the test class.

The normal setup of a BrowserTest would look something like this:

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Create an article content type that we will use for testing.
    $type = $this->container->get('entity_type.manager')->getStorage('node_type')
      ->create([
        'type' => 'article',
        'name' => 'Article',
      ]);
    $type->save();
    $this->container->get('router.builder')->rebuild();
  }

All you need to is to remove the database definitions from parent::setUp();

So copy setUp() from core/tests/Drupal/Tests/BrowserTestBase.php and remove the database definitions. That should make the test run on the current database.

Though this is very simplistic and you might run into more problems along the way. All is in the BrowserTestBase class. Make sure to read my other answer, make sure to implement the teardown properly. And most of all, make proper backups!

2
  • 1
    I tried it a few times but did not succeed. Do you have a working example, where you implemented this?
    – Martin
    Commented Apr 3, 2017 at 15:54
  • 1
    Unfortunately not for Drupal 8 and only very briefly for 7. Commented Apr 3, 2017 at 16:22

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