Questions tagged [testing]
Use it for questions about automated Drupal tests.
434
questions
2
votes
2
answers
102
views
Where can I find the Annotation(s) for test classes? [closed]
I've seen lots of examples of tests like:
/**
* My test description.
*
* @group mymodule
*/
class MyModuleTest extends KernelTestBase { }
I know what @group mymodule is and how that annotation ...
0
votes
0
answers
13
views
Disable javascript in selenium test? (WebDriverTestBase)
Background
A website I work on has a progress bar that gets an initial value from Drupal, and is then updated to the "correct" value with javascript.
I am writing a test with ...
0
votes
1
answer
43
views
I can't debug a functional test for a custom module
I am trying to debug the functional tests for my custom module.
When I run the debugger, it doesn't hit the breakpoint I've set on the first line of the test method. However, when I debug a functional ...
1
vote
1
answer
34
views
Functional Testing - Schema Incomplete Exception missing schema
I am working on my first functional test for a custom module. Each time I run the functional test I get errors dealing with configuration schema issues. However, I'm confused by the error message.
...
0
votes
1
answer
36
views
Running tests for a module without requiring a full site installation
I'm building a custom module (for use on multiple projects) with code that relies on stuff from Drupal core and third party modules (e.g. extending classes, implementing interfaces, annotating as ...
0
votes
0
answers
27
views
Mock user with fields to use in a functional test
How is it possible to mock a user with some simple text fields (for example, First name and Last name) in a functional test (which extends BrowserTestBase) without creating the configuration for this ...
1
vote
0
answers
40
views
How to create functional tests for testing REST requests that the site has? [closed]
I'm trying to understand how to create tests for REST Endpoint that a custom module creates.
I managed to create a Unit Test for the Get method of the Rest-resource and now I want to create a ...
1
vote
0
answers
23
views
How can I identify if my code needs a Unit test a Kernel test or a browser test? [closed]
In my project we extend base classes. BlockBase, ControllerBase, FormBase, FormatterBase and so on to build blocks, Fieldformatters, forms ...
We already have automated tests (functional) using a ...
0
votes
0
answers
31
views
Functional test for emails (subject and title)
Question: How can a Drupal Functional Test (extenting BrowserTestBase) check for a certain Mail-Subject and Mail-Body defined in hook_mail in a custom module ?
Context:
I have a custom module which ...
1
vote
1
answer
31
views
Autoload helper classes in tests/src/ for phpunit in contrib module
I am working on a contrib module with phpunit tests.
Currently there is no composer.json or phpunit.xml or installation routine in the module itself, instead one has to run the functional and browser ...
0
votes
0
answers
38
views
CKEditor hides from my testing tool
I am building an automated testing tool for the drupal site that I maintain. The site has migrated to D9 and functions normally for a human user. When my python scripts pull up selenium/Chrome ...
0
votes
1
answer
19
views
Debugging functional tests with phpunit just hang unless I toggle "stop/start listenting for php debug connections"
Trying to get tests up and running by following https://www.previousnext.com.au/blog/running-and-debugging-phpunit-tests-phpstorm-ddev-and-xdebug. Seems to work, except having an issue with functional ...
1
vote
1
answer
42
views
Can you install exported site config as part of a kernel test?
I am using the Configuration Sync module to export config specific to my site out into a config directory that lives outside my main Drupal directory.
When writing kernel tests I know that you can use ...
1
vote
1
answer
101
views
How do I mock \Drupal::httpClient()?
I'm unit testing a utility library (of our own making) that makes calls to an external REST API with \Drupal::httpClient()
So, I have a library class with static functions:
class myUtils {
public ...
0
votes
1
answer
30
views
PHPUnit throws error: \Drupal\Tests\DocumentElement class not found
I have added some phpunit tests to my custom module. When trying to run them in I get this error: Class '\Drupal\Tests\DocumentElement' not found I have copied core/phpunit.xml.dist to core/phpunit....
1
vote
2
answers
25
views
How to build core test database?
Fifteen years writing Drupal code and yet I have never written unit tests. I have recently posted a couple core patches which require tests to be written for them - so good time to learn how to write ...
0
votes
0
answers
28
views
Testing an internal Drupal API that requires POST
I want to add a test around an internal API in Drupal 8 that only accepts a POST payload and fires off some services. I am getting the following:
"curl error 3"
From Guzzle when trying:
$...
-1
votes
1
answer
255
views
PHPUNIT is running successfully on local but giving error on acquia-pipleine
When I am running the .vendor/bin/phpunit it is not giving any error.
o/p:
Testing
. 1 / 1 (100%)
Time: 00:00.014, Memory: 14.00 MB
OK ...
0
votes
0
answers
19
views
Mocking the response of XmlResponse::getResultCode not working
Following is the code that I'm using to mock the response of XmlResponse::getResultCode as Ok, but it isn't working, I'm not sure what exactly is going wrong here:
$response = $this->...
1
vote
1
answer
53
views
Missing module error while running unit test
I am trying to run a unit test for one of my contributed modules in my local development environment. But it keeps throwing an error:
$:/path/to/web$ sudo -u www-data -E phpunit -c core/phpunit.xml ...
0
votes
0
answers
33
views
How can I test code that uses cron for scheduling?
I have some custom code on my site that schedules emails to be sent. When an email is set, a user account field is updated. So I want to write a test that:
Jumps forward in time into the future
...
0
votes
0
answers
28
views
PHPUnit throws error on FieldStorageConfigInterface
In a custom module, I created a class that extends the CommerceKernelTestBase class implemented from the Drupal Commerce module.
This class implements setUp() using the following code.
/**
* @...
1
vote
0
answers
56
views
How to kernel test a custom formatter for an entity reference field type?
We have a custom field formatter for a string and an entity reference field type. While this works for testing the string field:
$node = $this->createNode(['type' => 'page', 'field_id' => '...
2
votes
1
answer
52
views
How can I capture Watchdog output during a functional test?
I have a functional test that is failing, and I think that there could be some useful information in the database log, but unfortunately the test suite only runs in containers on GitLab CI so I don't ...
1
vote
0
answers
63
views
In testing, how do I get an attribute using XPath?
In Drupal BrowserTestBase, I am trying to access an attribute value using XPath:
$query = $this->xpath('//a[text() = "View"]/@data-drupal-link-system-path');
I can't figure out any way ...
0
votes
1
answer
14
views
Using attachFileToField in a Functional test doesn't allow entering the alt text for an image
In a Functional test, I am doing:
$this->drupalGet('node/add/article');
$page->fillField('title[0][value]', 'Page 1 EN');
$page->pressButton('Add image');
// Using ...
1
vote
0
answers
48
views
Code coverage with Functional JS Tests
So I am trying to get the code coverage report to work with my functional JS tests. I have been looking everywhere and trying everything, but I just can not seem to get the report to work, even ...
2
votes
2
answers
152
views
Basic kernel test fails with Type of Drupal\KernelTests\Core\Entity\EntityKernelTestBase::$modules must be array
Trying to port a Drupal 7 module to 9 with accompanying test and getting this error immediately when running it:
PHP Fatal error: Type of Drupal\KernelTests\Core\Entity\EntityKernelTestBase::$modules ...
1
vote
0
answers
82
views
How do I access $this in a dataProvider method?
Is it possible to access $this in a PHPUnit BrowserTestBase data provider method to get access to properties defined in the testBase class? It seems that the sequence of processing during startup of ...
3
votes
1
answer
560
views
How do I run PHPUnit tests?
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 ...
1
vote
0
answers
257
views
Kernel test fails with 'Error: Call to undefined function drupal_valid_test_ua()'
My kernel tests are failing with:
Error: Call to undefined function drupal_valid_test_ua()
/Users/joachim/Sites/drupal-core-composer/repos/drupal/core/tests/Drupal/KernelTests/KernelTestBase.php:273
...
3
votes
1
answer
78
views
Why is the test runner not picking up tests for a contrib module?
I recently added tests to Cache Register, but the test runner on drupal.org doesn't seem to be picking them up. The tests are all located in tests/src/Kernel (repo) but aren't queuing up in the issue ...
1
vote
1
answer
190
views
How can I set up an a SQLite memory database to improve PHPUnit test speeds?
I'm trying to speed up a functional Drupal test.
Given a test...
...
public function the_front_page_loads_for_anonymous_users() {
$this->config('system.site')
->set('page.front', '/...
4
votes
1
answer
115
views
How can I have Behat create content with an address field properly filled?
I'm writting a Behat test, where I'm triying to create a content that uses a field address, generated by Address Module. I'm using Drupal 9.0.7, Address Module 1.9.0 and behat 3.8.1. My goal is to ...
2
votes
1
answer
256
views
How do I test a CKEditor text format field with Behat/Mink?
I am using an @javascript test in Behat on CircleCI to check my Drupal 8 site.
When I use the standard I fill in "my field" with "value" step, it works for fields with the plain ...
3
votes
1
answer
113
views
Mocking Settings (settings.php) within Unit tests
Drupal's Settings class is marked final and has no interface, meaning traditional PHPUnit mocking techniques will not work out of the box. Is it necessary to install third-party tools to be able to ...
0
votes
1
answer
114
views
How do I run a BrowserTestBase functional test with blocks?
I have a module that defines a custom block and places it using a block.block.block_name.yml file that I generated by exporting the placement of the block on a dev site. I've unit-tested the block ...
7
votes
1
answer
229
views
How can I write a unit test for a custom field formatter?
I'm working on a project where we need to have code coverage using phpunit.
I have a public method that includes this code:
/**
* {@inheritdoc}
*/
public function viewElements(...
1
vote
1
answer
590
views
"attempt to write a readonly database" with BrowserTestBase Functional tests
I have a functional test that is currently empty. I run it with the following command;
php core/scripts/run-tests.sh --sqlite /db/drupal.sqlite --php /usr/local/bin/php --verbose --directory modules/...
1
vote
1
answer
97
views
How do I write a behat test to go to user/UID/somewhere when I only know the username?
I'm trying to write Behat tests to verify my Drupal Commerce store.
My test creates some users, so I know the usernames but not the UIDs (the UIDs are assigned by Drupal in the test, so they will be ...
2
votes
1
answer
120
views
Why do functional tests require a running website?
I'm trying to set up functional tests in Drupal 9 using the BrowserTestBase class. The Type of Tests page on drupal.com and Browser test tutorial are quite clear that browser tests will build their ...
0
votes
1
answer
137
views
run-tests.sh error: Test class not found
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 ...
0
votes
0
answers
61
views
Problem with SchemaIncompleteException when running BrowserTestBase
I am trying to run a test, which extends BrowserTestBase, on my module. I got the following error:
Drupal\Tests\my_module\Functional\BasicTestCase::testMyModule
Drupal\Core\Config\Schema\...
0
votes
1
answer
34
views
What is the quickest way to dump a node to insert into a test case?
Is there a script, console, drush command etc that can dump a node in the format needed to create a test case?
How can I rapidly fill out
$this->drupalCreateContentType(['type' => 'bond',
'...
1
vote
1
answer
175
views
PHPUnit testing a method that changes an entity field
Let's say I have an entity with certain fields and a method that does the following
public function changeEntityField(Entity $entity) {
$entity->set('field_xyz', 20);
$entity->save();
...
0
votes
0
answers
27
views
Entity Query returns incorrect results while running functional javascript test
Im writing a functional javascript test for my project where im facing a problem with my feature. I have created a group entity just above it and referencing a parent group.
This doesn't work:
$...
1
vote
2
answers
105
views
How to Write Browser Tests on Custom Theme?
I'm writing front end tests for a custom webform. These tests work fine when testing on the webform page of the form.
$this->drupalGet('/webform/custom_form');
$this->assertResponse(200);
...
1
vote
2
answers
391
views
How do I set the value of a date and time field with Behat?
I am trying to test my site with Behat.
On my entity edit form, I have a Datetime field that uses the Date and time widget. I want to use the Date and time widget because I need to set the time in ...
0
votes
1
answer
306
views
Custom PHP Unit test trait for a custom module cannot be found
I have created a new Trait for testing my custom module and placed it in my_module/tests/src/Traits with the namespace Drupal\Test\my_module\Traits, the same as core modules do.
When I try to use this ...
2
votes
1
answer
741
views
What is the recommended way for using PHPUnit?
Installing Drupal 9 with the drupal/recommended-project Composer package ships without PHPUnit. Why? (In Drupal 8 it is included. The documentation only tells about Drupal 8.)
Which version of PHPUnit ...