Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 31

Use it for questions about automated Drupal tests.

12 votes
Accepted

Trying TDD with PHPUnit

My guess is that you're not including the phpunit.xml.dist file, which sits in the core folder. Either run tests from there (./vendor/bin/phpunit ../modules/your_module) or specify that file with -c. …
Berdir's user avatar
  • 83.5k
8 votes
Accepted

Why is my unit test not detected?

Unit tests must have the following namespace: Drupal\Tests\dev_module\Unit. For complicated reasons, they also don't show up in the right group but they are all in the PHPUnit group. To run your uni …
Berdir's user avatar
  • 83.5k
8 votes
Accepted

How to create a unit test for a class which uses a trait?

StringTranslationTrait also has a setStringTranslation() method so you can inject a string translation mock for exactly that purpose.
Berdir's user avatar
  • 83.5k
7 votes
Accepted

How to connect database in UnitTestCase subclass?

See https://www.drupal.org/phpunit for better documentation. There are 4 test base classes: UnitTestBase, pure unit tests where everything but your code has to be mocked and only supports classes K …
Berdir's user avatar
  • 83.5k
6 votes
Accepted

How to inspect outgoing email content during a SimpleTest?

Simpletest automatically captures sent mails during a test and allows you to fetch them using $this->drupalGetMails() in the test class. I did something like this recently in tests I wrote for the Si …
Berdir's user avatar
  • 83.5k
6 votes
Accepted

Writing unit tests for a method that calls static methods from another class

You can't mock static calls directly. A relatively easy way to fix this is what core uses in quite some places to mock functions that haven't been converted to objects/services yet. You create a meth …
Berdir's user avatar
  • 83.5k
6 votes
Accepted

Why am i getting PDO exception while running the following test from console?

You can't create and save entities in a unit test. That's what kernel tests are for. You will also need to install the necessary entity schema then, make sure the necessary modules are enabled. One w …
Berdir's user avatar
  • 83.5k
5 votes

How can I do Test Driven Development?

The only thing that I'm aware of is that for contributed modules, you can enable automated testing of commits and patches in the issue queue, see http://drupal.org/node/689990. …
Berdir's user avatar
  • 83.5k
5 votes
Accepted

SimpleTesting-PDOException: SQLSTATE[42S02]

This behavior is correct. SimpleTest sets up a new site environment for every test case, using a table name prefix. You probably forgot to install your module by overriding the setUp() method. Once y …
Berdir's user avatar
  • 83.5k
5 votes
Accepted

Strategy for unit testing classes that interact with the Entity API

In regards to the testing pyramid, that is the ideal scenario, but it's not always feasible. I also think that it is makes more sense in a scenario where you built and control the whole system. …
Berdir's user avatar
  • 83.5k
5 votes
Accepted

How to shadow a core function used in two different tests files?

Wrap it in a !function_exists() See core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php for example: <?php namespace Drupal\Core\Render\Element; if (!function_exists('t')) { functio …
Berdir's user avatar
  • 83.5k
4 votes
Accepted

Redirect SimpleTest queries to a different database

The most important thing is defining $profile = 'testing'; in your test class, that will only install the absolutely required modules for Drupal to run. Speeds up test runs by ~50%. …
Berdir's user avatar
  • 83.5k
4 votes
Accepted

Writing functional tests for a controller that makes call to external service?

It is sometimes more complicated to set up, but has the advantage that you are testing much more of your actual code and integration. … Testing all possible cases, error handling/conditions and so on. …
Berdir's user avatar
  • 83.5k
4 votes
Accepted

Functional tests performance

Use testing and then create only what you need in the test. That's the main thing to speed up your tests. They are still rather slow, but that should help. …
Berdir's user avatar
  • 83.5k
4 votes

How to unit test hook_form_alter implementation code in custom module

See https://www.drupal.org/docs/8/testing/types-of-tests-in-drupal-8 for documentation of different types of tests in Drupal 8 and when they should be used. …
Berdir's user avatar
  • 83.5k

15 30 50 per page