3

Running tests takes time, sometimes I just want to know the coverage. Is it possible to run something like phpunit --coverage-text so it only outputs test coverage without actually running all tests?

2
  • 2
    Problem is how is it supposed to know which code is actually tested without running the tests? Commented Aug 9, 2019 at 18:31
  • @NigelRen Right but I was thinking maybe it can do something like PHPStan where it analyzes the code without running it Commented Aug 9, 2019 at 18:35

1 Answer 1

6

You can't do it because PHPUnit needs to run the tests in order to know which lines of code were touched by the tests. But there's a workaround: merging code coverage reports.

Example: You run your full test suite, and the report says you have 72% of you code covered. Then you add a few more tests. You run only these new tests, ending up with, let's say, 3% of code coverage. After that you can merge the two reports in order to have 75% coverage.

One way to do it is using PHPUnit parameter --coverage-php and phpcov:

You run your full test suite once:

phpunit --coverage-php coverage/fulltest.cov

After a while you write a new test and run only that one

phpunit tests/SomeClassTest --filter testNewMethod --coverage-php coverage/testNewMethod.cov

You merge the reports into a coverage.html file in order to get full code coverage

phpcov merge --html coverage.html /coverage

If your tests really take a long time to run, this can be an option, as now you will need to run only the new tests to update the code coverage. Of course, doing this process manually every time is a pain, but you can create a script to automatize it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.