6

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 test method is standard in Magento 2 for custom modules?

Please explain briefly.

Thank in advance.

1

Unit tests confirm that our code is handling all of the possible scenarios. It works as EXPECTED. Even exceptions are EXPECTED. Let's say we are writing a public function that is used by other code. This public function has lot's of logic in it (No point in testing methods like simple getters and setters.). Unit tests confirm that "all logical paths" have been handled.

Take this magento controller as an example that I'm working on right now

```

public function execute()
{
    $params = $this->getRequest()->getParams();

    if($this->isValidRequest($params)){
        try{
           $user = $this->userFactory->create();
           $user->setData($params);
           $user->save();
           $data=['success'=>1];
        }catch (\Exception $e){
            $data=['success'=>0,'error'=>$e->getMessage()];
        }

    }else{
        $data = ['success'=>0,'error'=>'invalid request'];
    }

    $result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
    return $result->setData($data);
}

```

I'm trying to save some user data in my custom database tables. This controller will be called using AJAX post call from the frontend.

I start writing unit tests by thinking about different scenarios like so

  1. What if request is not AJAX.
  2. What if a post parameter is missing
  3. What if a post parameter is invalid. (may be has invalid value)
  4. What if for some reason my database save call fails
  5. What response should go back if everything goes as expected

and so on and so forth...

You need Unit tests when there is logic in your code. Well written Unit tests also serve as documentation to original code. They help you refactor your code. If you break something, you will know as soon as you run tests.

Above all, they force you to think about the design of your program. They help you keep the code decoupled.

In the end I'm going to leave a rather debatable comment.

"You should only test public API's. Test output against input. Your unit tests should not care about the implementation." But that's just my opinion.

If you are new to magento2 unit testing, getting comfortable with mocks and stubs really helps.

0

Please check the below link unit test is available in magento you need to run the command to test the entire site.

Comment Usage:

magento dev:tests:run --help

To list the available test types:

magento dev:tests:run --help

This gives you a list similar to the following:

all, unit, integration, integration-all, static, static-all, integrity, legacy, default

For example, to run integration tests:

magento dev:tests:run integration

Reference link : http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-test.html

  • Thanks for reply @Jeeva But I wanted to know actual purpose of it. what will be outcome of it ? – Dhaval Solanki Nov 7 '17 at 11:27
  • I know it is available with many functions but my question is what is purpose of it ? – Dhaval Solanki Nov 7 '17 at 11:28
  • It's for Unit test only we don't need to write automated script for testing. In magento 1.9 this option is not available in magento 2 this option is implemented.Find the below link, devdocs.magento.com/guides/v2.0/test/testing.html – Jeeva Chezhiyan Nov 7 '17 at 11:34

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.