Write tests. Not too many. Mostly integration
At the assert(js) 2018 conference, Kent C. Dodds dissects this tweet by Guillermo Rauch:
Write tests. Not too many. Mostly integration.
— Guillermo Rauch (@rauchg) December 10, 2016
At the assert(js) 2018 conference, Kent C. Dodds dissects this tweet by Guillermo Rauch:
Write tests. Not too many. Mostly integration.
— Guillermo Rauch (@rauchg) December 10, 2016
Frank de Jonge, creator and maintainer of Flysystem, recently started using Docker to test the FTP driver. On his blog he explains the why and how.
For Flysystem, an open source PHP package to deal with filesystems, I needed a way to test FTP (barf) interactions. FTP servers are notoriously bad at abiding by the spec. ... For Flysystem's FTP(d) adapter an integration test, using an actual FTP server, brought back the level of confidence it needed.
https://blog.frankdejonge.nl/regaining-trust-in-your-tests-with-docker/
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"Freek’s newsletter is one of the best ways to stay updated with the Laravel and PHP ecosystem. It consistently highlights useful packages, tools, and ideas from the community, especially the amazing work coming from Spatie. As a Laravel developer building SaaS and web platforms, I find it extremely helpful to discover practical tools and insights that improve my development workflow."
Monica Dinculescu, an engineer at Google, shares on her blog how you can use Puppeteer, Mocha and Pixelmatch to create automated visual tests of a webpage.
I did a little song-and-dance that sets up Puppeteer* , takes screenshots of your app (like, all the routes you care about), and then compares them to the “golden” ones. If they match, your test passes! Yes, it only works on Chrome. No, it’s not actually unit testing. Yes, it’s literally just counting pixels but you know what? It counts them in both a wide and a narrow viewport size and any testing is better than no testing at all; fight me.
https://meowni.ca/posts/2017-puppeteer-tests/
I'm currently building a webapp named Oh Dear: an easy to use and beautiful website monitor. It has recently gone into it's beta phase. At the moment of writing everybody can register a new account. After you've registered you'll start your trial period of 10 days. Oh Dear is built on top Laravel…
Beautiful, organized results in PHPUnit, where have you been all my life?
— Keith Damiani (@keithdamiani) December 21, 2017
Composer-install this bad boy, add one line to phpunit.xml, and enjoy the view. ????https://t.co/JJi4tEhWWo pic.twitter.com/4pndct3iZk
Read more [twitter.com]
Laravel rockstar TJ Miller posted a short and sweet post how how he tested a middleware that forces requests to respond with JSON.
So what I’ve done here is define custom testing routes and applied the middleware as I would use it in the application routes, in this case global middleware and as middleware for the api group. This allows me to assert that the middleware is configured and functioning correctly.
https://medium.com/@sixlive/an-approach-to-testing-middleware-c547fc942848
In his excellent talk given at Laracon Online 2017, Adam Wathan argues that testing in isolation leads to brittle test and that integration tests are much more valuable.
In a fantastic post on his blog, Kent C. Dodds makes the case for focusing on writing integration tests, and stop going for 100% code coverage. Amen to that!
You may also find yourself testing implementation details just so you can make sure you get that one line of code that’s hard to reproduce in a test environment. You really want to avoid testing implementation details because it doesn’t give you very much confidence that your application is working and it slows you down when refactoring. You should very rarely have to change tests when you refactor code.
https://blog.kentcdodds.com/write-tests-not-too-many-mostly-integration-5e8c7fff591c
Need to mock a specific method in a facade, leaving the remainder free to respond to calls normally? It's possible with "Partial Mocks". pic.twitter.com/VgVMdnBDid
— Raul (@rcubitto) October 4, 2017
Read more [twitter.com]
The HTTP specification says that status codes should be three digits integers, but what happens if they are not? April King, head of website security at Mozilla, did some fun experiments to find out.
While it is easy to create test cases for conditions that don't satisfy this requirement, it is somewhat more difficult to determine how third-party libraries will handle HTTP requests that fall outside this constraint. I looked around the internet for websites to help me test weird status codes, but most of them only let me test with the known status codes. As such, I decided to add arbitrary HTTP status codes to my naughty httpbin fork, called misbehaving.site.What I discovered is that the various browser manufacturers have wildly different behavior with how they handle unknown HTTP status codes.
An official toolset for testing Vue components will be released soon. In a new series Jover Morales tells you all about it.
vue-test-utils, the official VueJS testing library and based on avoriaz, is just around the corner. @EddYerburgh is indeed doing a very good job creating it. It provides all necessary tooling for making easy to write unit test in a VueJS application.Jest, on the other side, is the testing framework developed at Facebook, which makes testing a breeze, with awesome features such as:
- Almost no config by default
- Very cool interactive mode
- Run tests in parallel
- Spies, stubs and mocks out of the box
- Built in code coverage
- Snapshot testing
- Module mocking utilities
In the JavaScript world Jest, built by Facebook, is an excellent tool to run tests. Not only can it automatically rerun your tests when source code changes, but there's also an interactive mode where you can set a filter on which tests to run while the tool is running. Would it be great if we could…
Christoper Pitt published another excellent piece over at Sitepoint. This time he describes how he built a watcher to automatically recompile his preprocessed code and rerun the tests.
In order to reduce the burden of invoking the transformation scripts, boilerplate projects have started to include scripts to automatically watch for file changes; and thereafter invoke these scripts.These projects I’ve worked on have used a similar approach to re-run unit tests. When I change the JavaScript files, these files are transformed and the unit tests are re-run. This way, I can immediately see if I’ve broken anything.
https://www.sitepoint.com/write-javascript-style-test-watchers-php/
Let's have a little fun! Yitzchok Willroth, better know as Coderabbi, created a new package that can improve the output of PHPUnit.
Here's what the output of a testrun looks like with his package installed.
Testing @spatie_be 's media library with the new #laravel upload fakes ? Trust, but verify ? #testing pic.twitter.com/Ss98rGloRf
— Christoph Rumpel (@christophrumpel) July 3, 2017
Read more [twitter.com]
Curl is not your only tool when creating or testing out APIs. On her blog Lorna Jane Mitchell made a nice list of alternatives.
At a conference a few days ago, I put up a slide with a few of my favourite tools on it. I got some brilliant additional recommendations in return from twitter so I thought I'd collect them all in one place in case anyone is interested - all these tools are excellent for anyone working APIs (so that's everyone!).
Mattieu Napoli shows how you can use non breaking spaces to make long test function names more readable.
Yes. This article is about using non-breakable spaces to name tests. And the fact that it’s awesome. And why you should use them too.public function test a user can add a product to a wishlist() { // ... }
The code above is valid PHP code and works. Non-breaking spaces (aka in HTML) look like spaces in editors but are actually interpreted like any other character by PHP.
http://mnapoli.fr/using-non-breakable-spaces-in-test-method-names/
It's cool that it works, but I'm not really a fan of this. I very much prefer how test runners like jest go about this by passing the name of the test to a function so you can use spaces.
Cool little Laravel 5.5 Eloquent Collection macro to assert two model collections are the same ?? pic.twitter.com/zzbkr1fL9T
— Adam Wathan (@adamwathan) June 14, 2017
Read more [twitter.com]
In a new blogpost at Sitepoint Christopher Pitt talks about snapshot testing. He first explains how snapshot testing can help test React components. Then he demonstrates how you can use our home grown package to use snapshot testing in PHP. Finally he shows pretty cool use cases for snapshot tests.
Ah-ha moments are beautiful and rare in programming. Every so often, we’re fortunate enough to discover some trick or facet of a system that forever changes how we think of it.For me, that’s what snapshot testing is.
You probably write a lot of PHP code, but today I want to talk about something I learned in JavaScript. We’ll learn about what snapshot testing is and then see how it can help us write better PHP applications.
https://www.sitepoint.com/snapshot-testing-viable-php/
If you want know more about snapshot testing, check out this blogpost by my colleague Sebastian, or watch this video where I refactor some tests to use snapshots.
Welcome to part 5 of a series on how to test a Vue component. Here's the table of contents of the series: Starting out with tests The first test and snapshot testing Creating an instance of a component More tests and faking time Jest awesomeness and a skeleton to get started (you're here) Jest watch…