Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Unit Test Implementation in Magento 2 custom module

    As in my previous blog we have learned few commands to perform unit testing . Today we will create a small custom module in magento 2 and will write test cases for it.
    Let’s start, now I am assuming that you are aware with the Magento Directory structure and know how to create custom module.
    If you are new to Magento 2, so first please check our wonderful blog here , before moving ahead.

    Let me brief I am going to create a model for calculator where we can perform basic operations like addition, substraction etc.
    Step 1 : Create your custom module and register it, in may case I have create module Webkul_HelloUnitTest.

    Step 2 : Create model  calculator here :

    Above We have a class calculator and inside it there is a function addition which will recieve 2 values and return the addition as result.

    Now lets create unit test model for the above.

    Step 3 :  Create test model here :

    Step 4 : Run any of the commands of my previous blog.
    Here I have a command to run test for the particular file.
    here it is :

    This will return below output.
    On Success :

    On Failure : when I changed the logic to substract.

    Few types of Assertions are as below  :

    Note : If you want to go deep in the topic so you can check this link .
    Please try out once and If stuck any where so please feel free to ask in comments section.

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again