I know how to test methods that return some values - that is basic unit testing, but what if I want to test methods that enter values to database. I have found this post, that says integration test are right for that?

Also I have found this blog post. Where is example how to write integration tests, to test category and product creation. It uses $this->dispatch() method to get page content from url.

My question is: I have not fount any examples how to write integration tests, which would test database entries that are not displayed in front-end or back-end. Can anyone please post any example codes (I am beginner at Test Driven Development)?

Background: I am trying to write module, that uses previously stored data from database (functions ran by cron are using the data).

up vote 1 down vote accepted

In integration tests you may use the object manager to work with Magento objects, like repositories.

For example:

use Magento\Framework\App\ObjectManager;
use Magento\Sales\Model\OrderRepository;

class YourTest extends \PHPUnit_Framework_TestCase
     * @var OrderRepository
    private $orderRepository;
     * @var ObjectManager
    private $objectManager;

    protected function setUp()
        $this->objectManager = ObjectManager::getInstance();
        $this->orderRepository = $this->objectManager->create(OrderRepository::class);

    public function testSomething()
        // do stuff

        $orders = $this->orderRepository->getList( ... );

        // make assertions

Assuming you are using PHPUnit to write integration tests, the first step is to make sure your environment is correctly configured.

For that you will need to have an empty database in place and use the correct PHPUnit configuration file.

Integration Test Environment Setup

To specify the credentials for the test database use this file:


And the phpunit configuration file can be found in the following path:


Running Integration Tests

Once you have your test environment configured you are ready to create your first test.

I think nothing stops you from put them anywhere but usually your tests go into: {magento_root}/app/code/{vendor}/{extension_name}/Test/Integration/...

Integration test examples

For integration test examples I tend to look at: {magento_root}/dev/tests/integration/testsuite/Magento

There are many examples of integration test for blocks, controllers, models, etc.

A good and simple example is this {magento_root}/dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php

namespace Magento\Contact\Controller;

 * Contact index controller test
class IndexTest extends \Magento\TestFramework\TestCase\AbstractController

    public function testPostAction()
        $params = [
            'name'    => 'customer name',
            'comment' => 'comment',
            'email'   => 'user@example.com',
            'hideit'  => '',

                "Thanks for contacting us with your comments and questions. We'll respond to you very soon."

For more information take a look at this link: http://devdocs.magento.com/guides/v2.0/test/integration/integration_test_execution.html

Hope it helps.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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