AsyncUnit vs PHPUnit

Now that you clicked on the provocative title let's talk about PHPUnit! And why you should be thinking of AsyncUnit as an addition to your PHPUnit tests and not a replacement.

Why not just use PHPUnit and the Amp wrapper?

If you're familiar with unit testing and Amp you've probably come across the amphp/phpunit-util wrapper. This is a great utility that really helps improve writing unit tests with PHPUnit and Amp. I highly recommend you check it out and use it! However, where the wrapper starts to have problems is with more complete integration testing that requires more exact control of the Loop... particularly those tests with database interactions.

The current architecture of PHPUnit and the Amp wrapper is one where each test gets its own Loop. For unit tests that are focused on ensuring a clean, known state this is likely the correct approach. However, if you need a resource with a heavy initialization cost, such as a connection to a database, that has to be run for every single test problems can quickly crop up. It would not take much to imagine a test suite attempting so many connections to the database in such a short time frame that the database becomes non-responsive! That's precisely the scenario that led to the creation of this framework and its design has been guided by the need to have a single Loop running all tests.

Use both frameworks

In sufficiently complex applications you should probably be making use of both frameworks. Continue to use PHPUnit and the wrapper for your simple unit tests. It is mature and has a ton of support. When you start performing integration tests and you experience the pain of having your app server or database connection reset on every test start to incorporate AsyncUnit!

That being said, nothing is stopping you from using AsyncUnit as your testing framework for both unit and integration tests! However, that's not the goal of AsyncUnit and the framework was designed to solve a specific need instead of replacing your primary testing framework.

Last updated