53 votes
Accepted

Run tests for a specific module in Magento2

Magento 2 use phpunit framework for running test. You can run test per type and module with command: {phpunit_executable} -c {Magentoroot}/dev/test/{type}/phpunit.xml[.dist] path/to/module/test/dir ...
user avatar
  • 20.3k
17 votes
Accepted

What is the proper way for unit testing PHP7 code with PHPUnit 4.1 in Magento 2?

Using the bundled PHPUnit version, even if it's ancient, is probably the best way to go since that will allow running the tests for all modules together during CI. I think writing tests in a manner ...
user avatar
  • 14.4k
16 votes
Accepted

Unit testing source models

This is a great thread, and I really like both Answers from @KAndy and @fschmengler. I would like to add some additional thoughts that I find valuable when asking a question such as "Should I test X?" ...
user avatar
  • 14.4k
15 votes

Run tests for a specific module in Magento2

kandy's answer seams like the way to go, but in the mean time I found a quick and dirty way to do it. Copy dev/tests/unit/phpunit.xml.dist to dev/tests/unit/phpunit.xml and replace <directory ...
user avatar
  • 196k
14 votes

Best practice for unit tests in Magento 1.9

Installation Since Magento 1 doesn't use composer out of the box, I don't think it makes a big difference if you install phpunit using composer or just download the phar version. If you already use ...
user avatar
  • 14.4k
12 votes
Accepted

How to get ScopeConfigInterface through the object manager of unit tests in magento 2?

I may be wrong here, but I think for unit tests you don't have to retrieve values from the data base. You can assume that the implementations of \Magento\Framework\App\Config\ScopeConfigInterface are ...
user avatar
  • 196k
12 votes
Accepted

Magento 2 : When to use Object Manager in Unit tests?

The short answer is, yes, I would mock the entire Context class. $mockScopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); $mockContext = $this->getMock(\...
user avatar
  • 14.4k
11 votes
Accepted

How to properly use getCollectionMock

TL;DR You are using the getCollectionMock() method correctly. The problem is that it only mocks the getIterator() method. Your code however calls toOptionArray() on the collection, not getIterator(). ...
user avatar
  • 14.4k
11 votes
Accepted

Best practice for unit tests in Magento 1.9

I faced the same problem a while back. I considered using the Ecomdev PHPUnit module but I find it hard to use and poorly documented (but I still love what Ivan does and his great contribution to the ...
user avatar
10 votes
Accepted

How to obtain the Object Manager in Unit-Tests?

You can use in your unit test an instance of \Magento\Framework\TestFramework\Unit\Helper\ObjectManager. /** * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ protected $...
user avatar
  • 196k
9 votes
Accepted

Unit test Issue on getting value

The problem lies in this line in setUp: $this->requestMock->method('getHeader')->willReturn($this->httpHeader); Here you instruct the request mock to return your HttpHeaderInterface ...
user avatar
7 votes
Accepted

How to mock a Context object

You can test your class like this: public function testSomething() { $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class) ->...
user avatar
  • 196k
7 votes
Accepted

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

The easiest way to deal with that is to run compilation before running tests: bin/magento setup:di:compile The other way is to explicitly define methods for the factory mock eg. instead of doing ...
user avatar
7 votes

Simulating Order Data For a Unit Test

You should distinguish between unit and integration tests. Unit Test In a unit test the class under test should get the order data from a collaborator that you can inject as a mock, for example ...
user avatar
  • 14.4k
7 votes
Accepted

Magento 2 : Unit test with mock data don't work. Why?

Given the information you posted, I suspect that the class \Custom\Shipping\Helper\Config implements (or inherits) public function __call() from a parent class and that the method getProductTypes() is ...
user avatar
  • 14.4k
7 votes

Magento2 unit tests doesn't run using vendor/bin/phpunit or vendor/phpunit/phpunit/phpunit

./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/VendorName/ModuleName/Test/Unit Try Below Command instead of yours ./vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml....
user avatar
6 votes

How we can create unit tests to everything we have created for our custom module in Magento 2?

The best I can suggest for now is reading up on PHP Unit and looking at other test scripts provided with the code base. I do not recall more unit testing examples being high on the doc teams list at ...
user avatar
  • 3,458
6 votes

Unit testing source models

In my opinion, there is no general answer to "write unit tests for source models, yes or no" I've written unit tests for source models, but those were dynamic source models that fetched external data ...
user avatar
5 votes

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

Also you may use something like this private function getMockupFactory($instanceName) { /** Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ $objectManager = $this->...
user avatar
5 votes

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

The problem originates from PHPUnit mocking library, as it cannot autoload the needed class. If you take a look into Magento dev repo, it setups Autoloader catcher, that generates a class when it is ...
user avatar
5 votes

Unit testing source models

How should I unit test those? I think you should not. Add code to system increase support and maintenance cost but testing process should be LEAN. More over this code should not exists. I believe ...
user avatar
  • 20.3k
5 votes
Accepted

Instantiating models for unit tests

Instantiating the class under test Unit tests should be a simple as possible because that helps with understanding the code being tested as well as debugging. Lets have a look at the purpose of the \...
user avatar
  • 14.4k
5 votes
Accepted

Unit Test or Integration Test for this kind of functions?

In addition to @raphael-at-digital-pianism's great response. I find the resolve method rather complex, so I would want test coverage. $inputParams = $this->serviceInputProcessor->process( \...
user avatar
  • 14.4k
5 votes
Accepted

Magento 2 - run phpunit to test

The error probably is caused by some other test initializing an object with the object manager during construction, which is trying to mock the arguments. This shouldn't happen unless the tests ...
user avatar
  • 14.4k
5 votes
Accepted

How to write tests when closures are involved

To mock callables, I usually mock __invoke: $callbackMock = $this->getMockBuilder(\stdClass::class) ->setMethods(['__invoke']) ->getMock(); $callbackMock->expects($this->once()...
user avatar
4 votes
Accepted

EcomDev PHPUnit Translation not Working with Mocked Helper

The original translation method __() indeed gets called, but it involves some "magic". This is the method in Mage_Core_Helper_Abstract: /** * Translate * * @return string */ public function __() {...
user avatar
4 votes
Accepted

Unit tests custom module not executed - Magento 2

Well your phpunit.xml file specifies a suffix: <directory suffix="Test.php"> So every tests you run must have this suffix and it doesn't seem like your test does. To fix that, rename Tagalys\...
user avatar
4 votes
Accepted

How to write Unit Test for a function which uses a variable defined in Constructor

In unit tests you test components isolated. That means that you should construct the objects under test yourself and provide input by the test, not by some external method. Example: /** * @...
user avatar
4 votes

Magento 2 : When to use Object Manager in Unit tests?

To answer your question I will describe why ObjectManagerhelper class was developed by Magento team: When M2 development started all classes use Mage::* methods and we start increase coverage with ...
user avatar
  • 20.3k
4 votes

Magento 2: how to use the CommandTester

You are not getting the real behavior because the \Magento\Framework\TestFramework\Unit\Helper\ObjectManager creates a mock for each constructor argument dependency, not the real thing. The ...
user avatar
  • 14.4k

Only top scored, non community-wiki answers of a minimum length are eligible