Search code examples
mockingphpunitcode-coveragephp-code-coverage

PHPUnit Mocked classes methods not shown as covered in code coverage reports?


I am working on a php project in magento2. I am using mock objects for writing unit test cases. I came to know that when i generate code coverage report, Mocked classes and methods do not show as covered in code coverage reports, Is there any way we can show these ?

I looked into official repo and it's mentioned as default behaviour.

Reference: https://github.com/sebastianbergmann/phpunit-mock-objects/issues/123

Any help much appreciated!

Thanks


Solution

  • Mocked methods are not shown as covered because their code was not executed (they were stubbed and only a predefined value was returned and the original code never called).

    There are few things you can do:

    • If Magento 2 has it's own test, execute them. That way the coverage of magento tests will be added to yours.
    • The above point actually doesn't make much sense, so better ignore magento paths from the code-coverage report.
    • Add integration tests that actually go trough the Magento code. This is the only real test you'll have. When you are mocking a magento method, you just verify that your code calls the method correctly or that it works with the expected return value correctly. You never verify if that method call actually does what you expect