Questions tagged [unit-tests]

Denotes questions related to unit testing on Magento.

Filter by
Sorted by
Tagged with
52
votes
3answers
7k views

Run tests for a specific module in Magento2

Running the tests for Magento to using php bin/magento dev:tests:run takes a lot of time. Is there a way to run the tests for a specific module only? I'm a noob in (unit) testing so have mercy.
23
votes
4answers
3k views

Tools for Unit Testing in Magento

What do you use for Unit Testing in Magento? Or what do you know about Unit Testing and Magento? Whether you do TDD or you just want to have unit test coverage for your project components, you need ...
23
votes
2answers
1k views

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

When I'm writing my modules, I'm trying to supply them with unit-tests for the most critical parts of the application. However, there are at the moment (Magento 2.1.3) several ways on how to write ...
18
votes
3answers
794 views

EcomDev_PHPUnit set up in Magento EE 1.13

I am trying to set up EComDev_PHPUnit in EE 1.13 as per the instructions in https://github.com/EcomDev/EcomDev_PHPUnit Well first hurdle was to get modman working but I gave up and copied over the ...
16
votes
3answers
1k views

Magento 1 Testing Frameworks

Wherever I can, I like to develop software following a Test Driven Development (TDD) approach. Usually, in PHP that means using PHPUnit. Ideally, I would like to write both fine and coarse-grained ...
14
votes
1answer
1k views

Unit Testing Observers in Magento

How do you approach unit testing Observers in Magento? (working with EcomDev_PHPUnit) What kind of assertions do include? check if the event is being dispatched (assertEventDispatched()) check the ...
11
votes
2answers
6k views

Best practice for unit tests in Magento 1.9

I maintain a Magento 1.9 site with several custom modules. Some of the functionality is business critical and in dire need of some unit tests. E.g. a unit price calculator. I usually develop in ...
10
votes
3answers
1k views

Unit testing source models

I have several models in my custom extension that only serve the purpose filling in some selects and/or multiselects in the add/edit form of my entities. So they are what magento calls "source models"....
10
votes
0answers
85 views

Magento2 Chrome Driver Service error in MTF

I am new to MTF. I have followed the magento2 tutorial based on the following link But When I am running the command vendor/bin/phpunit I am getting the following error - Fatal error: Uncaught ...
8
votes
2answers
4k views

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

I'm trying to read a row in my unit test from core_config_table in magento 2 database. I know that to accomplish this job as I have read this link. I have to use: \Magento\Framework\App\Config\...
8
votes
2answers
2k views

How to mock a Context object

I am trying to test a module which contains a context object as follows: public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\Product\...
7
votes
3answers
2k views

Generate factory or proxy in unit tests: “ReflectionException: Class …Factory does not exist”

As far as I understand, Factory and Proxy classes are generated on the fly by the autoloader if they do not exist yet in var/generation (see: What Triggers the Generation of a Factory in Magento 2) ...
7
votes
2answers
611 views

PHP Static tests for front-end devs

I'm trying to add PHP Static test at Frontools to simplify and improve testing process and if it's possible increase performance, b/c it takes so long to get any results. GH Issue - https://github....
7
votes
1answer
406 views

Magento enterprise edition 1.14 automation functional tests

Does Magento enterprise edition 1.14 have pre written functional automation tests? We use Magento 1.14 and I have to decide if I have to start building functional end-to-end user journey tests from ...
6
votes
2answers
1k views

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

So I recently stumbled upon a problem while writing a unit test in Magento 2. You can read more about the problem in question here. Vinai told me it is generally bad practice to use the object ...
6
votes
1answer
421 views

Unit test Issue on getting value

I am creating a unit test for my simple method, this method get the value from requestHeader here is my class which I want to create test class namespace Vendor\Module\Helper; use Magento\Framework\...
6
votes
1answer
355 views

Instantiating models for unit tests

This question came to mind after I've seen the edits made by Vinai to my answer here. Let's say we have a class to test with a constructor like this namespace Some\Namespace; class SomeClass { ...
6
votes
1answer
2k views

Magento and PHPUnit Mocking Mage::getStoreConfig()

I am writing unit tests using the EcomDev PHPUnit extension. When trying to provide values to the Mage::getStoreConfig() method using a fixture, I am not getting the value from my fixture, but the ...
6
votes
2answers
3k views

How to avoid redirection to “enable-cookies” page during controller unit test?

I'm doing a very simple controller test using Ecomdev_PHPUnit: class Foo_BarWishlist_Test_Controller_IndexController extends EcomDev_PHPUnit_Test_Case_Controller { /** * @test * @...
6
votes
2answers
155 views

What we can achieve by unit test in Magento 2 ? How it is useful?

I don't know how can we do unit test of our module in Magento 2? What will be benefits of doing a unit test on our Magento 2 module? what will be the outcome of the unit test in Magento 2? Which ...
5
votes
1answer
2k views

How to obtain the Object Manager in Unit-Tests?

I've created the second unit-test for my second class. The second class will take a dependency of a first class instance and in one test I'd like to use the object manager to create the second class. ...
5
votes
1answer
2k views

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

I have created a module in magento 2. And need to create the Unit tests for it. I can see that Each core module has its own Test folder containing tests for each component like Controller, Block,Model,...
5
votes
1answer
857 views

Test Driven Development with Magento

I am interested in unit testing in magento and have started looking at the ecomdev module. However, i dont think it is going to be possible to do full tdd with magento. For the following scenarios ...
5
votes
1answer
275 views

Magento 2 Functional Testing using Phantom JS (or other headless)

Curious as to if there is a way to configure the functional testing xml, in order to allow for other web-drivers such as Phatomjs? I already know of the way to change them to Chome and Firefox, but ...
4
votes
1answer
742 views

How to properly use getCollectionMock

TL;DR: I would love an example of how Magento\Framework\TestFramework\Unit\Helper\ObjectManager::getCollectionMock() can be used for unit tests that involve collections and/or collection factories and ...
4
votes
2answers
169 views

How to get base url in Unit Test?

This is my unit test in my custom module: namespace Star\Trek\Test\Unit\Model; class BeyondTest extends \PHPUnit\Framework\TestCase { } How can I get my Magento 2 base URL in there?
4
votes
2answers
524 views

Fatal error: Class 'Codeception\Platform\Extension' not found. Issue occurred after magento 2.3.0 upgrade to 2.3.1

Preconditions (*) 1.Magento 2.3.1 upgraded from Magento 2.3.0 Php 7.2.15 OS : Ubuntu 18.04 Bionic Phpstorm 2019.1.1 Evnironemnt : Magento development Steps to reproduce (*) Issue appears after ...
4
votes
1answer
144 views

Magento1 - tests - phpunit compatibility

I am currently working on a project (magento 1.9) where tests are developed using PHPUnit 4.8, ECOMDev and TechDivision/magento unit testing (https://packagist.org/packages/techdivision/...
4
votes
1answer
848 views

How to write tests when closures are involved

For learning purposes I was trying to write a unit test for one of my Magento 2 around plugins (interception). The method I'm testing receives as parameter a \Closure object that cannot be mocked ...
4
votes
1answer
121 views

Class 'Magento\TestFramework\TestCase\AbstractController' not found when running unit test for custom module

I tried to make a unit test for my custom module like this: app/code/MyCompany/UnitTest/Test/Unit/Controller/SpinTest.php namespace MyCompany\UnitTest\Controller; class SpinTest extends \Magento\...
4
votes
0answers
103 views

Unit testing ina Magento 2 - Mocks and Assertions

Open file \Magento\Cms\Test\Unit\Model\PageRepositoryTest and take a look at the setUp() method /** * Initialize repository */ protected function setUp() { $this->pageResource = $this->...
4
votes
1answer
485 views

How to use collection factory model in Unit Test magento 2

I have a custom extension with model with structure like this in database: +----------------------------------+ |entity_id | customer_id | balance | +----------------------------------+ | 1 | ...
4
votes
0answers
72 views

Writing Magento 2 Unit Tests [duplicate]

I have written unit tests for a custom module which i have created. I have put the test in the Module directory like the core modules. Now the tricky part is that,dev\tests\functional\tests\app ...
4
votes
3answers
505 views

setting up ecomdev install error 'host is not set for base url'

I am trying to install ecomdevs' unit testing with my copy of magento. I have added the folders manually to the relevant directories and added the below in app/etc/local.xml.phpunit <default> ...
3
votes
2answers
582 views

Unit Test or Integration Test for this kind of functions?

I have the following resolve function in one of my classes: public function __construct( \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, \Magento\Framework\Webapi\...
3
votes
1answer
2k views

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

I'm trying to write unit tests for my code, but for some reason I can't get it to work. To simplify it, I have the following: namespace Custom\Shipping\Model\Config\Source; use Custom\Foundation\...
3
votes
1answer
612 views

Simulating Order Data For a Unit Test

I am currently writing my first unit test. The basis of the test is I get all of the order information along with the items and create a JSON object from it. Is there a way to create dummy items and ...
3
votes
1answer
384 views

Unit tests custom module not executed - Magento 2

I have created a file under \Vendor\Module\Test\Unit\Block\Catalog\Product\ProductList\Toolbar.php namespace Vendor\Module\Test\Unit\Block\Catalog\Product\ProductList; class Toolbar extends \...
3
votes
1answer
598 views

Magento 2 Tests in Magento

We are looking to start using testing for our builds and are looking at the best tools to use. I noticed there is a port of the Magento 2 tests https://github.com/techdivision/...
3
votes
1answer
2k views

What is the correct way to create mock object in unit test

Now days I am focusing on TDD development, and I am following the Magento test class for reference. So firstly I look into Contact module (as it is the small module) and I found that in DataTest.php ...
3
votes
1answer
373 views

Magento2 - Why would Magento Test Framework - Integration tests fail on a clean composer install

Firstly as I'm using Magento Enterprise I don't think I can pull against a git repository where I know the tests are passing. I presume that these are being run in travis-ci.com rather than .org ...
3
votes
1answer
392 views

How to mock a class property in Magento 2 Unit Test

How can I mock a class attribute such as a particular object (Ex: \Magento\Catalog\Model\Product)
3
votes
1answer
420 views

Magento Unit Test Error

Following is the Unit Test error that I get Expectation failed for method name is equal to when invoked zero or more times Parameter 0 for invocation Magento\Framework\App\Config\...
3
votes
2answers
926 views

Why is this test script for eav visibility attribute failing?

During the course of work, I ran into some issues with the visibility attribute in Magento 1.8 and decided to write a test script. Unfortunately, the results have only made me MORE confused. Basically,...
3
votes
1answer
660 views

Problem creating mock method for “core/store” with ecomdev phpunit

I'm trying to mock this method: getCurrentCurrency() from "core/store" (Mage_Core_Model_Store) in my unit test. Just a simple stub, nothing more. private function _stubGetCurrentCurrency() { $...
3
votes
0answers
85 views

Cart observer unit testing

I created a module that has some logic to modify the price of certain items depending on a few conditions pertaining to the other items in the cart. I'm trying to write a unit test to test that the ...
2
votes
2answers
3k views

How run unittest using EcomDev_PHPUnit module?

Here is described old version: http://www.ecomdev.org/2011/02/01/phpunit-and-magento-yes-you-can.html And they wrote that I need call phpunit UnitTests.php But I have magento v.1.7. I downloaded ...
2
votes
2answers
1k views

Magento 2 - run phpunit to test

I tried to run a unit but failed with the following error. My test class <?php namespace Vendor\Module\Test\Unit\Observer; class CheckoutOnepageControllerSuccessActionObserverTest extends \...
2
votes
3answers
2k views

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

Env: PHP 7.0.27-1+ubuntu16.04.1+Clear Magento 2.2.2 with sample data (downloaded in archive) I have created new test module that very simple with unit test for one class. When I run php bin/magento ...
2
votes
1answer
335 views

Magento 2: how to use the CommandTester

So I've developed a small module that creates a new CLI to be able to change the customer password. Here's my DigitalPianism\ChangePassword\Console\Command\ChangePassword.php class: <?php ...