Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Explained how to run tests in multiple kernel apps #8124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 37 configuration/multiple_kernels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,43 @@ In order to solve this issue, add the following configuration to your kernel:
# allows to use app/Resources/views/ templates in the ApiKernel
"%kernel.root_dir%/../app/Resources/views": ~

Running Tests Using a Different Kernel
--------------------------------------

In Symfony applications, functional tests extend by default from the
:class:`Symfony\\Bundle\\FrameworkBundle\\Test\\WebTestCase` class. Inside that
class, a method called ``getKernelClass()`` tries to find the class of the kernel
to use to run the application during tests. The logic of this method does not
support multiple kernel applications, so your tests won't use the right kernel.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful here. The approach described here does not solve the issue when you have more than one kernel. The problem then still is that the KernelTestCase class keeps a reference to the previously created kernel in its static $kernel property. Thus, if your functional tests do not run in isolated processes a later run test for a different kernel will reuse the previously created instance which points to a different kernel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! You are right and this is critical. What would be the best (and simplest) solution to this problem?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be sufficient to always reset the static $class property inside the tearDown() method:

protected function tearDown()
{
    parent::tearDown();

    static::$class = null;
}

The parent::tearDown() call is important to make sure that the kernel is shut down after the test was run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!! I've fixed the code and reused your original explanation because it's great.


The solution is to create a custom base class for functional tests extending
from ``WebTestCase`` class and overriding the ``getKernelClass()`` method to
return the fully qualified class name of the kernel to use::

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

// tests needing the ApiKernel to work, now must extend this
// ApiTestCase class instead of the default WebTestCase class
class ApiTestCase extends WebTestCase
{
protected static function getKernelClass()
{
return 'ApiKernel';
}

// this is needed because the KernelTestCase class keeps a reference to
// the previously created kernel in its static $kernel property. Thus,
// if your functional tests do not run in isolated processes, a later run
// test for a different kernel will reuse the previously created instance,
// which points to a different kernel
protected function tearDown()
{
parent::tearDown();

static::$class = null;
}
}

Adding more Kernels to the Application
--------------------------------------

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.