Snipe-IT uses PHPUnit as automated testing frameworks. We adhere to the Laravel way of doing things, which means our tests are broken down as described below. All tests are located in the tests directory of your local Snipe-IT installation.

❗️

If your tests break the CI build, or if you do not provide any tests with a new feature, your PR will not be accepted. Make sure you run the full test suite locally before submitting your PR to ensure your changes did not break existing functionality.

Running Tests

We use PHPUnit for testing. To run the full suite of tests:

php artisan test

To run just a particular test, use the filter option:

 php artisan test --filter=GroupStoreTest

Unit Tests

These are located in tests/Unit and are usually small tests that check the results of model methods. We typically do NOT check built-in Laravel validation or Eloquent relationships, and where possible, we avoid using factories or interacting with the database. (It's fine if your unit tests do need to interact with the database, it just slows things down a bit.)

Run these using phpunit or ./vendor/phpunit/bin from the command line in your local Snipe-IT project folder.

Feature Tests

These are located in tests/Feature and handle less-complex "real world" application testing, and API testing. Basically anything where we need to test for an HTTP response, but we don't need to actually run a headless browser that emulates an actual human being clicking around. (In some schools of testing thought, these might be called integration or functional tests.)

Run these using phpunit or ./vendor/phpunit/bin from the command line in your local Snipe-IT project folder.

Writing Unit Tests

You can reference the ExampleTest.php in the tests/Unit directory for an example of how tests in general should look.

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

to:

To generate a new Unit test, use:

php artisan make:test MyModelTest --unit

which will generate the scaffolding for your new Unit test in the tests directory.

Please update the generated PHP docblock in your tests from:

/**
* A basic test example.
*
* @return void
*/

to:

/**
* @test
*/

Using Factories

We lean on factories to generate realistic-looking data for testing. You can find those in database/factories, and they can be invoked in a Unit test.

🚧

The default scaffolding won't work out of the box if you're using factories to generate data within your Unit tests

If you wish to use factories in your Unit tests, you MUST change the use statement at the top of your test from:

use PHPUnit\Framework\TestCase;

to:

use Tests\TestCase;

in order to access factories within a Unit test.

If you don't make this change, you will likely see an error that looks like this:

InvalidArgumentException: Unable to locate factory with name [default] [App\Models\MyModel].

PHPInsights

To run phpinsights for checking code quality, run:

./vendor/bin/phpinsights