NULL pointer
NULL pointer

Reputation: 1377

How can I share code between Laravel Dusk Browser tests, unit tests, and functional tests

Unit tests created with php artisan make:test -- unit UnitTestClassName extends \Tests\TestCase

Functional Tests create with php artisan make:test FunctionalTestClassName also extends \Tests\TestCase

\Tests\TestCase extends Illuminate\Foundation\Testing\TestCase

However, Laravel Dusk browser test case create with php artisan dusk:make DuskTestClassName extend \Tests\DuskTestCase, which in turn extends Laravel\Dusk\TestCase

There appears to be no common class to which I can add things like which user to test with (at least outside of the vendor folder).

To allow shared code, I have created a trait in app\Traits\TestData.php:

<?php
namespace App\Traits;
trait TestData {
  static $testUserId = 40;
  public static function checkTestData() {
    // returns an array of strings describing any problems with the test data setup.
    ...
  }
  ...
}

and I use that trait in my tests. Is there any better way? Specifically, is there a common testing class I am missing?

The specific issue I am having is that my trait does not extend any TestCase, so I can't call functions like

$this->assertNotNull($testUser,"Test user not found");

in my trait, so I need to duplicate that code in the TestCase and DuskTestCase.

Upvotes: 0

Views: 516

Answers (1)

NULL pointer
NULL pointer

Reputation: 1377

Just call the assert methods statically in the trait:

self::assertNotNull($testUser,"Test user not found");

Upvotes: 0

Related Questions