I'd like to unit test my Magento blocks using PHPUnit, how can I properly instantiate my models using the Magento framework to be used as a System Under Test? I include the path to my class, but it will need a link to the rest of the framework to run.. What do I do to avoid a long line on include statements?

include C:\repo\magento\cwdl\app\code\core\Mage\Adminhtml\Block\Sales\Order\Shipment\Create\Tracking.php';

class TrackingPageTests extends PHPUnit_Framework_TestCase{

    public function testGetCarriers(){
        $this->SUT = new Mage_Adminhtml_Block_Sales_Order_Shipment_Create_Tracking();

error I get: PHP Fatal error: Class 'Mage_Adminhtml_Block_Template' not found in C:\repo\magento\cwdl\app\code\core\Mage\Adminhtml\Block\Sales\Order\Shipment


You have to include at least the Magento autoloader (Varien_Autoload) and register it. I'd recommend doing this in your test bootstrap file instead of in each test case.

But if you are not testing your classes completely isolated, which is rare for blocks, you will also need to instantiate the Magento application (Mage::app()), basically turning your tests into integration tests.

There is lots of global state in Magento, so you should call Mage::reset() in the setUp() methods to reset all registry values and singletons.

Here's a working example: https://github.com/digitalpianism/testframework

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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