Questions tagged [laravel-testing]

Use this tag for question about Laravel testing

Laravel is built with unit testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a phpunit.xml file is already setup for your application. An example test file is provided in the tests directory.

Defining & Running Tests

To create a test case, simply create a new test file in the tests/Unit or tests/Feature directory. The test class should extend TestCase. You may then define test methods as you normally would when using PHPUnit.

An Example Test Class

class FooTest extends TestCase {

    public function testSomethingIsTrue()
    {
        $this->assertTrue(true);
    }

}

You may run all of the tests for your application by executing the php artisan test (or phpunit) command from your terminal.

Note: If you define your own setUp method, be sure to call parent::setUp.

Reference

177 questions
4
votes
2 answers

Laravel 5.6. PHPUnit. Order-insensitive assertJson() method

In my business logic I don't care about the order of elements. Here is my example of a test $this->getJson('/api/order-attempts') ->assertJson([ 'data' => [ ['status' => 'error'], ['status' => 'error'], …
D.R.
  • 2,540
  • 3
  • 24
  • 49
4
votes
3 answers

How to test an email's subject with laravel's Mailable

I'm wondering, if there is an easy and direct way to test the email's subject using the new Mailable function of Laravel I have a class which can send 5 different emails, but all of them to the same person, so that testing if an email was sent to a…
Fran Roa Prieto
  • 385
  • 1
  • 3
  • 12
3
votes
3 answers

Laravel testing - 429 Too Many Requests

As I move forward in my Laravel project I have several tests for controllers and now I'm facing with this issue. Some of my tests are failing with this message: Expected response status code [200] but received 429. Failed asserting that 200 is…
netdjw
  • 5,419
  • 21
  • 88
  • 162
3
votes
1 answer

How are parallel test databases configured in Laravel?

From the Laravel Testing Documentation: # Parallel Testing & Databases Laravel automatically handles creating and migrating a test database for each parallel process that is running your tests. The test databases will be suffixed with a process…
Joel Mellon
  • 3,672
  • 1
  • 26
  • 25
3
votes
0 answers

Failed asserting that 200 is identical to 302. in laravel feature testing

I am running test in laravel . But it is failing test. I am not getting the reason may because of middleware but not sure. Here is test function public function test_example() { $staff = factory(Staff::class)->create(); $response…
user13134426
3
votes
1 answer

Laravel - Testing - assertJsonMissingExact

I have to admit that it's the first time I use this method Before the description, here is the Laravel and PHP version in this case. Laravel: 8.15 PHP: 7.4.13 My Route: Route::get('/', function () { return ['a' => 1, 'b' => 2, 'c' =>…
Ray
  • 333
  • 4
  • 11
3
votes
0 answers

Laravel Sanctum testing

I'm trying to test my API auth methods, so I created ConfirmPasswordControllerTest with: /** @test */ public function confirm_password_confirms() { $user = factory(User::class)->create(); $data = ['password' => 'password']; $response =…
user12003867
3
votes
1 answer

Lumen 5.2 make:test command is missing

I have installed Lumen 5.2.8. I am trying to unit test the app using phpunit. But when I tried to run the command php artisan make:test UserTest it throws an exception Command "make:test" is not defined. How can I solve this issue?
Kiren S
  • 3,037
  • 7
  • 41
  • 69
3
votes
1 answer

Laravel Unit Testing Migration fails

I am trying to run PHPUnit test. I have setup SQLite in :memory for the testing environment. In my setup, I call Artisan::call('migrate') but then I get the following error: General error: 1 Cannot add a NOT NULL column with default value NULL …
Kousha
  • 32,871
  • 51
  • 172
  • 296
2
votes
2 answers

How to run tests in Laravel in parallel, while excluding some tests?

Both php artisan test --parallel and php artisan test --exclude someGroup (this excludes tests which are annotated with someGroup) work, but php artisan test --parallel --exclude someGroup doesn't I saw However I can't think of a reason why these 2…
online Thomas
  • 8,864
  • 6
  • 44
  • 85
2
votes
0 answers

Laravel Feature test: The user is not authenticated Failed asserting that false is true

I am trying to test the user registration, the test file came with laravel jetstream, I just added some additional fields, and I kept getting this error The user is not authenticated Failed asserting that false is true., please anyone with any idea…
Adam
  • 85
  • 2
  • 12
2
votes
2 answers

Laravel remembers original response during http tests

Given the following pest test: it('allows admins to create courses', function () { $admin = User::factory()->admin()->create(); actingAs($admin); $this->get('/courses')->assertDontSee('WebTechnologies'); …
Jazerix
  • 4,729
  • 10
  • 39
  • 71
2
votes
1 answer

How to define custom message on testing JSON in Laravel if assertation fails?

I have this test in my Laravel project: // here are some test function configurations // found nothing if not match $this->post(action([TagController::class, 'search'], '!! Not foundable name !!')) ->assertOk() ->assertJsonCount(0,…
netdjw
  • 5,419
  • 21
  • 88
  • 162
2
votes
2 answers

How to test array in AssertableJson in Laravel 8?

In my Laravel project I want to test a JSON response what is acually a paginator result with data key. It's an array and it contains some elements. We don't know the amount exactly, because it's coming from a function. My problem is if the…
netdjw
  • 5,419
  • 21
  • 88
  • 162
2
votes
2 answers

Call to undefined method Tests\Unit\TopPageTest::get() in Unit test in Laravel 8

I am writing a code to make Unit test for each pages in my project and started with the Main page. The main page consists of shop lists, genre lists and area lists. tests\Unit\ToppageTest.php :
1
2
3
11 12