Running with coverage is very similar to a regular test run. Personally, I like using pcov as it’s much faster than Xdebug. This does require you to also install a coverage driver for the PHP version you are using. This is also something that’s possible with PHPStorm by using the Run > Run “X” with Coverage option. It can be useful to want to know the coverage of a certain test. The cool thing is that we can do the same for individual tests: What about code coverage? This will open up the test runner tool and you will see the output of your test run: Once that’s done I can continue with running the tests. So in this case I will first have to install it locally to avoid compatibility issues with the codebase. In this case the error occurs because the project is using PHP 8.1 which I do not have installed locally. Interpreters are actually just the different PHP versions that PHPStorm is aware of. Thankfully PHPStorm offers me a “Fix” button that will guide me to a solution: In the above screenshot you can see that there is an error that popped up because I don’t have an interpreter configured for the CLI. This will prompt you with a dialog to further configure your test runner. Once that’s done we can do a first run of all of our tests by going to Run > Run…. If you don’t see anything you can create a new entry by choosing PHPUnit Local and filling out the configuration. If you’re unsure, you can verify in your preferences under PHP > Test Frameworks. This should in most cases already be configured automatically. I’m going to assume that you already have PHPUnit configured and are able to run it in another way.The first thing that we’ll do is make PHPStorm aware of our PHPUnit installation. It turns out that it does and it’s actually pretty easy to set up! Getting started I thought I was dreaming and kept wondering if PHPStorm had a similar feature that I had just never realized. They would even get instant feedback visible within their editor. One day I was pairing with someone who used Visual Studio Code and I was amazed to see how they were able to run a single test method from within their editor by simply clicking a button. There are some ways to improve this experience such as using PHPUnit groups or the –filter option but even those require some setup and switching between windows (or having to google the syntax on how to run just one test method in a file). The best way to avoid this is to keep your feedback loops short. I’d get distracted with something else and lose my focus or come back and find out one failed in the end. On projects with very large test suites this would often lead to situations where I’d spend time waiting for all the tests to finish. When writing tests, I often find myself switching between the IDE and terminal windows to actually run the tests.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |