Automating test generation for PHPUnit

At my company I want my developers to write tests with a coverage of at least 70%.This is nothing unusual when you work with data that can affect your business.

In our case all the applications that we build are based on microservices that we put together with an inhouse tool called SPT (Single Point Of Truth).

In order to assure that our services are playing well together I want everyone in my team to write tests. But wrapping up the tests can be frustrating since you have to repeat the code a couple of times (Especially when you try to test API-Controllers).

That's where PhpUnitGen comes into place!

What is PhpUnitGen?

Well.. PhpUnitGen is a tool that you can use online or directly on your machine.

PhpUnitGen is a simple, powerful and modular tool to generate your PHP unit test templates.

So in short: It scaffolds tests for you. Automatically.

How to install PhpUnitGen?

In order to install it on your machine you simply need to run the following composer command:

composer global require --dev phpunitgen/console

How to use PhpUnitGen?

So now that we have installed the tool on our machine, we can use it in our project.

To to this we need to navigate into our project and run phpunitgen inside:

phpunitgen app/Http

This will give us some new files within our tests folder:

Our HTTP-Tests should now look like this:

Of course this test will fail since you don't have any assertions.Modify the test according to your logic and run the test.


PhpUnitGen is not perfect. But it will safe you a lot of time when you need to generate base-methods for your tests.

Take a look on the documentation for more about setting up and running phpunitgen.

© 2020 Florian Wartner. All rights reserved.