Skip to main content
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 ...
Vinai's user avatar
  • 14k
16 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 ...
Vinai's user avatar
  • 14k
13 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 ...
Raphael at Digital Pianism's user avatar
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(\...
Vinai's user avatar
  • 14k
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 ...
Marius's user avatar
  • 198k
11 votes
Accepted

Magento 2: Fixing Call to Undefined Method Mock_BlockFactory_4b440480::create() Test Failures

The \Magento\Framework\TestFramework\Unit\Helper\ObjectManager isn't able to automatically create a factory mock. (On a side note, I never use the \Magento\Framework\TestFramework\Unit\Helper\...
Vinai's user avatar
  • 14k
10 votes
Accepted

Magento 2.4.3 and Composer error messages for allure phpunit and ramsey uuid - Can't continue with update

Magento 2.4.3 composer.json Taking a look at the 2.4.3 codebase, it looks like the composer.json has newer require-dev dependencies than what you have posted. Typically when doing upgrades, among ...
sdaugherty's user avatar
  • 1,231
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 ...
Fabian Schmengler's user avatar
8 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 ...
Vinai's user avatar
  • 14k
8 votes
Accepted

Error: Class "Yandex\Allure\Adapter\AllureAdapter" does not exist

Install following package as follows since 2.3.0 didn't came with allure-phpunit. This issue is related with following issue Fatal error: Class 'Codeception\Platform\Extension' not found. ...
PhantomS's user avatar
  • 503
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 ...
Vinai's user avatar
  • 14k
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) ->...
Marius's user avatar
  • 198k
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....
Gaurav Manwani's user avatar
6 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( \...
Vinai's user avatar
  • 14k
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 ...
Vinai's user avatar
  • 14k
5 votes
Accepted

PHPUnit code coverage

I got it working. I copied dev/tests/unit/phpunit.xml.dist to dev/tests/unit/phpunit.xml and used the following configuration: <?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="...
jzahedieh's user avatar
  • 828
5 votes
Accepted

ObjectManager is unusable for me in Magento 2 Unit Tests

For unit tests you should use Magento\Framework\TestFramework\Unit\Helper\ObjectManager which allows to create any object with needed arguments (if they aren't passed, mocks will be created instead). ...
joni jones's user avatar
  • 2,549
5 votes

Running an integration test doesn't create a new Magento Installation

And as usual, after hours of trial and error and finally posting my question on Stack Exchange I find the answer myself in the next 10 minutes... Turns out it was my own custom module. It all bubbles ...
Giel Berkers's user avatar
  • 12.3k
5 votes
Accepted

Magento 2 - PHPUnit using mocking or without mocking?

It looks to me that you're mixing up 2 types of testing here: unit testing and integration testing. Unit Testing With unit testing, you're testing a single unit. Now what the definition of a "Unit" ...
Giel Berkers's user avatar
  • 12.3k
5 votes

Magento upgrade 2.4.4, phpunit issue

you need to find the "require-dev" in your magento root composer.json file "require-dev": { "allure-framework/allure-phpunit": "~1.5.0", "...
pankaj parmar's 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: /** * @...
Fabian Schmengler's 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 ...
KAndy's user avatar
  • 20.9k
4 votes
Accepted

Magento 2 : mocking `getParam()` in unit test not working

The getParam method accepts 2 parameters, the second one defaulting to null public function getParam($key, $defaultValue = null); returnValueMap works like this. the first N-1 values in the array ...
Marius's user avatar
  • 198k
4 votes
Accepted

how to call a controller in phpunit test?

Alan Storm got the response here: https://magento.stackexchange.com/a/8751/49865 Basically add the file and instantiate the class. In my case look like this: <?php require_once 'vendor/module/...
Adrià Garrido's user avatar
3 votes
Accepted

Mock products and setting values on unit testing

Use $product->method('getSku')->willReturn('test'); Mocks can be configured in many ways! It is worth reading the documentation here: https://phpunit.de/manual/current/en/test-doubles.html
Vinai's user avatar
  • 14k
3 votes

ObjectManager is unusable for me in Magento 2 Unit Tests

Remember to use the integration phpunit.xml.dist as the alternative configuration file for your Run/Debug configuration as following: Probably you are using the unit phpunit.xml.dist configuration ...
Fernando Miguel's user avatar
3 votes
Accepted

How to mock a class property in Magento 2 Unit Test

You would need to address this one level up. So if for example <?php namespace Fooman/Namespace; class Example { protected $product; public function __construct( Magento\...
Kristof at Fooman's user avatar
3 votes

How to mock a Context object

The following code will create a mock of the context in your test. $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class) ->disableOriginalConstructor() ...
jzahedieh's user avatar
  • 828
3 votes

Unit Test or Integration Test for this kind of functions?

Well its up to you to decide whether or not you should do integration or unit tests. Here my suggestion: why not do both? Thanks to what Vinai taught me here's how I see things: Try to do unit ...
Raphael at Digital Pianism's user avatar
3 votes
Accepted

Magento Unit Test Error

$this->scopeConfig->method('getValue') ->with([ 'module/general/base_url', 'module/general/user', 'module/general/api_user', 'module/...
Fabian Schmengler's user avatar

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