When your unit test suite gets larger, it can take quite a long time. One of the many ways to speed this up is to hunt down and fix slow tests. Let’s use PHPUnit’s test listeners to do just that. (more…)
Entries with the tag "testing"
PHPUnit Code Coverage Can Help While Writing Tests
It’s great to run code coverage at the very end before you push your changes. This gives you some idea what’s tested and what’s not. But you don’t have to wait till the end; code coverage can help you all throughout writing your test suite, too. (more…)
Host PHPUnit Code Coverage Image in Your Repo
I’m a sucker for those little badges at the top of the README files in Github repos. I know you can get them from external services, but could I host my own? Let’s find out. (more…)
Stop Using Assert Database Has in Laravel
Please stop using assertDatabaseHas in Laravel. There are many reasons why this is bad, and there are better options available. Let’s find out why. (more…)
Stop Using Sqlite in Laravel Unit Tests
tldr; Using Sqlite in Laravel (or most PHP apps) for unit testing causes false positives in unit tests. Things that work will not work when you move to production and use a different db like MySQL. Instead, spin up a test database that is the same tech and engine as your application will be. (more…)
Mark WIP Features for Better UX/Customer Acceptance
When you’re trying to demo your work in progress to a client, it can be difficult to know where to draw the line between working and non-working features. On one hand, you want to show them some context so they can understand the current feature. But, then what happens when they invariably try to explore the context (which may not be done)? Let me put that in a more concrete example. (more…)
Test Coverage is Not as Good of a Metric as You Think
There are a lot of debates about trying to achieve test coverage of 100%. Some people swear you need to do this. Others say, get 80% or more… the rest doesn’t matter. Each side has strong arguments. (more…)
PHPUnit Runs Data Provider Before Setup
I started noticing a disturbing trend on one of my projects: developers were doing too much logic in the setup and data provider methods of their PHPUnit tests. However, before we could address this, a “limitation” popped up which helped them kick this habit. (more…)
Better failing tests with Mockery::on()
In an earlier post titled Use $this->fail() with Mockery::on(), I explained the challenges of debugging a failing test with the closure passed to Mockery::on(). Instead of returning false, I opted to use $this->fail() - which seemed like a good idea at the time. After all, I was doing my test, then failing with a useful bit of information. (Previous to this, it would just say that you don’t have a matching handler for this assertion,... (more…)
Use Dependency Injection in Laravel Console Commands
It’s important to unit test your application code - even your console commands. So many times, I’ve seen people using the Artisan facade inside of console commands to either queue up new commands or call a different command. This makes it more difficult to unit test the application - you have to rely more on fakery (requiring you to reset your application each time then) and/or integration tests. (more…)
AssertSame vs AssertEqual in PHPUnit
When you’re testing inside of your PHPUnit test methods, you will use many assert-based methods. Two that are seemingly very similar are assertSame() and assertEqual() (more…)
Use the $this->fail() method with Mockery::on()
When you have a more complex assertion you need to make on the parameters of a mocked object, you might use the Mockery::on() method. It can be hard to tell how this fails, though, because if the assertion fails somewhere, the message is confusing - it basically says that there was no matching call to that method, which is technically correct. (more…)
Use Anonymous Classes to Test Traits
I’m guilty of creating stub-like classes in my tests to unit test traits, sometimes. So, you end up with a special class inside your unit test file, perhaps at the bottom, that is empty but only extends the trait or something like that. This is not a good idea, but it was my only way that I could figure out how to unit-test traits separately - especially if they were made of protected methods. (more…)
Fixing Laravel 5.4's Dependency on PHPUnit 5
Normally, when I write unit tests, I don’t use Laravel’s facade and fakery methods. I do a lot of injection of services, but in one particular case, when working with the Queue system, I had to use the facade for faking the queue and asserting some jobs were pushed. (more…)
Using Developer Tools in Chrome for Testing
In a perfect world, when testing your website you’d have a giant device library, a number of different internet connection speeds, and a bunch of users using the system simultaneously. A perfect world? Pfffft. But, I can help you at least get close to this set of requirements for your test. Let’s see how we can use Google Chrome to simulate different devices and change our internet connection speeds. (more…)
Should you unit test your dependency container?
I’ve been working with Pimple Dependency Injector lately, and I’ve come up with an interesting question. (more…)
Testing Error 500 Pages in Zend Framework
For the most part, ZF can capture any of your hard errors. It will generate an error 500. You’ve seen them, don’t lie… I’ve seen them way too many times. However, in my production application, I capture these with a specific controller. And because I love Unit Testing, I want to make sure that I test my implementation of my omg-this-is-broken setup. (more…)
PHPUnit Mock Objects: Only Mock What You Need
I was looking at mock objects in PHPUnit the other day and started to get confused. When I asked for a Mock object, the entire object was a mock. In all actuality, it wasn’t so much a Mock of the object but a complete shell of it. Each method needed to be defined. So, if I was testing an object that required three methods to execute, I would have to mock each method. (Yeah, some... (more…)
Extend Zend_Mail to use Views
I love working in the Zend Framework view system. One thing that bothers me, however, is that I must create a complex set of models when trying to send email. I decided that I’d like to move all of this output for the mail class into my own view system as well. (more…)
When writing unit tests, test against absolutes
So, while chatting with one of the developers on my team, we started talking about testing an XML document creation process he had been working on. He wrote a unit test and said “see, here is my class which generates the XML, and then here I use DomDocument to add the nodes I know it will have and test against that.” (more…)
Add PHPUnit Listeners to Watch for Long Running Tests
One of the under-utilized features of PHPUnit probably is the listeners interface. You can see the configuration options here: php.net/appendixes.configuration.html. So, I decided that I want to use this to know if a Unit Test takes longer than 2 seconds to run. That’s super over-kill in my opinion, but that’s my hard limit. If it takes longer than 2 seconds to run, something is wrong! So, I added the following to my configuration: (more…)
Testing protected and private attributes and methods using PHPUnit
First, I just want to say up front that this is not a discussion of “is 100% test coverage necessary” or a discussion about testing private methods. This is simply how you may do it if you wanted to. (more…)
Testing Outgoing Email with Zend Framework
I was creating a new application with Zend Framework at work the other day and I started using my technique that I described here by adding the original email in my email address using the + sign. However, the current organization I’m at has a mailserver (Exchange?) that is either configured not to allow this or just doesn’t have this functionality built in. So, this won’t work. I solved this with a new implementation of... (more…)
Eclipse Testing with TPTP - help me?
I recently came across this tutorial here about Testing with TPTP- and I’m confused. Whats the benefit of this type of testing (um… creating JAVA code for a JUnit test… right?) compared to running some PHPUnit, Selenium and AB (from apache, or something…)? What am I missing - does anyone have any other good hands-on tutorials? (more…)
APD post processing wrapper
A while ago, I discovered the ‘joys’ of APD… and then more so, the ‘joys’ of not being able to make heads or tails out of the output script. After digging deeper, I saw that the original directory already had some PHP scripts to parse the output. I ran those and wasn’t very impressed. Even more important, my boss wouldn’t be impressed. I needed to be able to make something that could be useful to... (more…)
Friendly reminders about testing your PHP code
I was reading on a forum the other day about some benchmarks for PHP. The guy had posted some results in ms measurements, and was getting upset about his erratic results. There were some things that he was forgetting, however. (more…)
Test Driven Development Book Review
(“the triangle”) just recently purchased a book for the library at my request, Test-Driven Development by example by Kent Beck. The current Amazon price for this book is $35. The book took me about 2 weeks of sporadic reading to finish. The first section took the first week, the remaining 2 sections and appendix flew by. (more…)