Laravel 5: Testing – General: test types, running tests, environment etc.

Testing – test types, running tests, environment etc.

Every developer test his app.
But large organizations, where people come and go, felt they need some more formalized testing.
Testing, where code tests some other code.
But the bottom line is like in this old saying: “Who polices the Police!”.
In other words, test are only as good as a coder, who writes them.
Of course in OOP environment, is makes sense to have some formalized, software driven tests, to see if something done in one place does not wreck something else, somewhere else.

Full PHPUnit documentation – see external docs here.
There is internal PDF version for PHPUnit v.6 – see here.
Testing types in Laravel 5.4

In pre 5.4 there were no folders inside of /tests, now we have some.

∴ \tests main folder

 \Browser sub-folder

Easy-to-use browser output testing API.
In other words, check if what should appear in browser, really appears.

This is a Laravel 5.4 Dusk only feature.

What is Dusk.
It is enriched replacement for ‘Integrated Package’.
It requires extra setup.
More: read here.

Here is how to create Dusk test file:

 \Feature sub-folder

Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint.
In other words, you make HTTP requests to your application and examine the output.

Here is how to make scaffolding for this kind of test:

 \Unit sub-folder

These are standard phpunit-like tests.
Unit tests focus on a very small, isolated portion of your code (e.g. on a single method).

Here is how to make scaffolding for this kind of test:

From docs:
When running tests via phpunit, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file.
Laravel also automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

It is good for to make testing database (sometimes) and put this below QUEUE_DRIVER in phpunit.xml:

Then db_local should be defined in /config/database.php in ‘connections‘ array.

Running tests

Running tests via command in console:

Command ‘phpunit’ returns ‘No tests executed!’

If you see  No tests executed!, when you run phpunit, you may want to install phpunit as ‘global’ and add it’s path to Windows ‘environment variable path’ – for more, see this: link.

Otherwise run: vendor\bin\phpunit.
If you use out-of-the-box phpunit, which comes with Laravel, this is the only command that would work, as phpunir is not ‘global’.

Also, this may happen, when your tests were made using phpunite lower than v.6
You are probably extending class PHPUnit_Framework_TestCase. As of v.6 that class is called: TestCase. This will also get you: No tests executed! response. See this link.

For Dusk use this command:

Both commands accepts any argument that is also accepted by the phpunit command.
  • –filter <pattern> : Filter which tests to run.
  • –testsuite <pattern> : Filter which testsuite to run.
  • –group … : Only runs tests from the specified group(s).
  • –exclude-group … : Exclude tests from the specified group(s).
  • –list-groups : List available test groups.
  • –list-suites : List available test suites.
  • –test-suffix … : Only search for test in files with specified suffix(es)

More PHPUnit CLI commands – here.