3

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?

closed as unclear what you're asking by mradcliffe, Pierre.Vriens, Yusef Mohamadi, sanzante, DRUPWAY Mar 24 '17 at 6:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 Mar 23 '17 at 14:03
  • Generally you provide those tests with just enough data, like config files and in the setUp() perform other tasks. – Kevin Mar 23 '17 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 Mar 23 '17 at 15:12
  • @Kevin Ok, sounds like an idea. is there a tutorial or example where this is being shown or explained? – Martin Mar 23 '17 at 15:14
1

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!

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

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