Debug School

rakesh kumar
rakesh kumar

Posted on

Assertions in Unit, Functional, UI, and Performance Testing

Unit testing assertion commands
Functinal testing assertion commands
UI testing assertion commands
Performance testing assertion commands

Unit testing assertion commands

assertSame() — exact value and type

Syntax: $this->assertSame($expected, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertSame(
    ['confirmed', 'cancelled'],
    $transitions['pending']
);
Enter fullscreen mode Exit fullscreen mode
  1. assertEquals() — equivalent value
Syntax: $this->assertEquals($expected, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertEquals(4500, $calculatedTripPrice);
Enter fullscreen mode Exit fullscreen mode

For prices, prefer assertSame() when you also need to enforce the result’s type.

  1. assertTrue() — condition must be true
Syntax: $this->assertTrue($condition);
Enter fullscreen mode Exit fullscreen mode
$this->assertTrue($booking->canBeCancelled());
Enter fullscreen mode Exit fullscreen mode
  1. assertFalse() — condition must be false
Syntax: $this->assertFalse($condition);
Enter fullscreen mode Exit fullscreen mode
$this->assertFalse($completedBooking->canBeCancelled());
Enter fullscreen mode Exit fullscreen mode
  1. assertNull() — value must be null
Syntax: $this->assertNull($actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertNull($booking->cancelled_at);
Enter fullscreen mode Exit fullscreen mode
  1. assertNotNull() — value must exist
Syntax: $this->assertNotNull($actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertNotNull($confirmedBooking->confirmed_at);
Enter fullscreen mode Exit fullscreen mode
  1. assertEmpty() — collection or value must be empty
Syntax: $this->assertEmpty($actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertEmpty($transitions['completed']);
Enter fullscreen mode Exit fullscreen mode
  1. assertNotEmpty() — collection or value must contain something
Syntax: $this->assertNotEmpty($actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertNotEmpty($availableTripDates);
Enter fullscreen mode Exit fullscreen mode
  1. assertCount() — exact number of items
Syntax: $this->assertCount($expectedCount, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertCount(3, $trip->itineraryDays);
Enter fullscreen mode Exit fullscreen mode
  1. assertContains() — item appears in a collection
Syntax: $this->assertContains($expectedItem, $actualCollection);
Enter fullscreen mode Exit fullscreen mode
$this->assertContains('confirmed', $transitions['pending']);
Enter fullscreen mode Exit fullscreen mode

11.** assertNotContains()** — item is absent from a collection

Syntax: $this->assertNotContains($unexpectedItem, $actualCollection);
Enter fullscreen mode Exit fullscreen mode
$this->assertNotContains('confirmed', $transitions['cancelled']);
Enter fullscreen mode Exit fullscreen mode
  1. assertArrayHasKey() — array contains a key
Syntax: $this->assertArrayHasKey($key, $array);
Enter fullscreen mode Exit fullscreen mode
$this->assertArrayHasKey('pending', $transitions);
Enter fullscreen mode Exit fullscreen mode
  1. assertArrayNotHasKey() — array does not contain a key
Syntax: $this->assertArrayNotHasKey($key, $array);
Enter fullscreen mode Exit fullscreen mode
$this->assertArrayNotHasKey('deleted', $transitions);
Enter fullscreen mode Exit fullscreen mode
  1. assertGreaterThan() — actual value is larger
Syntax: $this->assertGreaterThan($minimum, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertGreaterThan(0, $trip->availableSeats());
Enter fullscreen mode Exit fullscreen mode
  1. assertLessThanOrEqual() — actual value is at most the limit
Syntax: $this->assertLessThanOrEqual($maximum, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertLessThanOrEqual($trip->capacity, $requestedGuestCount);
Enter fullscreen mode Exit fullscreen mode
  1. assertInstanceOf() — object has the expected class
Syntax: $this->assertInstanceOf($expectedClass, $actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertInstanceOf(Booking::class, $booking);
Enter fullscreen mode Exit fullscreen mode
  1. assertIsArray() — value must be an array
Syntax: $this->assertIsArray($actual);
Enter fullscreen mode Exit fullscreen mode

$this->assertIsArray($trip->getItinerary());
Enter fullscreen mode Exit fullscreen mode
  1. assertIsInt() — value must be an integer
Syntax: $this->assertIsInt($actual);
Enter fullscreen mode Exit fullscreen mode
$this->assertIsInt($trip->availableSeats());
Enter fullscreen mode Exit fullscreen mode
  1. assertStringContainsString() — text contains a phrase
Syntax: $this->assertStringContainsString($expectedText, $actualText);
Enter fullscreen mode Exit fullscreen mode
$this->assertStringContainsString(
    'Royal Family Luxury Getaway',
    $trip->title
);
Enter fullscreen mode Exit fullscreen mode
  1. assertMatchesRegularExpression() — text matches a pattern
Syntax: $this->assertMatchesRegularExpression($pattern, $actualText);
Enter fullscreen mode Exit fullscreen mode
$this->assertMatchesRegularExpression(
    '/^HL-[0-9]{6}$/',
    $booking->reference
);

Enter fullscreen mode Exit fullscreen mode
For example, HL-123456 passes that
Enter fullscreen mode Exit fullscreen mode

Functinal testing assertion commands

Successful API response: assertStatus()

Syntax: $response->assertStatus($expectedStatus);

$response = $this->getJson('/api/trips/15');

$response->assertStatus(200);
Enter fullscreen mode Exit fullscreen mode
  1. JSON contains an exact field: assertJsonPath()
Syntax: $response->assertJsonPath($path, $expectedValue);
Enter fullscreen mode Exit fullscreen mode
$response = $this->getJson('/api/trips/15');

$response->assertJsonPath('data.id', 15);
Enter fullscreen mode Exit fullscreen mode
  1. Invalid booking is rejected: assertUnprocessable()
Syntax: $response->assertUnprocessable();
Enter fullscreen mode Exit fullscreen mode
$response = $this->postJson('/api/bookings', [
    'trip_id' => 15,
    'guests' => 0,
]);

$response->assertUnprocessable();
Enter fullscreen mode Exit fullscreen mode
  1. Validation identifies a field: assertJsonValidationErrors()
Syntax: $response->assertJsonValidationErrors($fields);
Enter fullscreen mode Exit fullscreen mode
$response = $this->postJson('/api/bookings', [
    'trip_id' => 15,
    'guests' => 0,
]);

$response->assertJsonValidationErrors(['guests']);
Enter fullscreen mode Exit fullscreen mode
  1. Booking exists in the database: assertDatabaseHas()
Syntax: $this->assertDatabaseHas($table, $matchingColumns);
Enter fullscreen mode Exit fullscreen mode
$this->postJson('/api/bookings', [
    'trip_id' => 15,
    'guests' => 2,
]);

$this->assertDatabaseHas('bookings', [
    'trip_id' => 15,
    'guests' => 2,
]);
Enter fullscreen mode Exit fullscreen mode

Use a dedicated test database for this example.

  1. Invalid booking was not saved: assertDatabaseMissing()
Syntax: $this->assertDatabaseMissing($table, $matchingColumns);
Enter fullscreen mode Exit fullscreen mode
$this->postJson('/api/bookings', [
    'trip_id' => 15,
    'guests' => 0,
]);

$this->assertDatabaseMissing('bookings', [
    'trip_id' => 15,
    'guests' => 0,
]);
Enter fullscreen mode Exit fullscreen mode
  1. Guest is sent to login: assertRedirect()
Syntax: $response->assertRedirect($expectedUrl);
Enter fullscreen mode Exit fullscreen mode

$response = $this->get('/bookings/create?trip_id=15');

$response->assertRedirect('/login');
Enter fullscreen mode Exit fullscreen mode

UI testing assertion commands

hese examples run inside a Dusk browser test, typically in a callback such as:

$this->browse(function (Browser $browser) {
    // UI checks go here.
});
Enter fullscreen mode Exit fullscreen mode
  1. Text appears on a page: assertSee()
Syntax: $browser->assertSee($text);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/trips/royal-family-getaway')
        ->assertSee('Royal Family Luxury Getaway');
Enter fullscreen mode Exit fullscreen mode
  1. Text does not appear: assertDontSee()
Syntax: $browser->assertDontSee($text);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/trips/royal-family-getaway/reviews')
        ->assertDontSee('Reviews for Desert Safari');
Enter fullscreen mode Exit fullscreen mode
  1. Button is visible: assertVisible()
Syntax: $browser->assertVisible($selector);
Enter fullscreen mode Exit fullscreen mode

$browser->visit('/trips/royal-family-getaway')
        ->assertVisible('@view-reviews');
Enter fullscreen mode Exit fullscreen mode

Here @view-reviews is a Dusk selector you add to the button.

11.** Click leads to the right page: assertPathIs()**

Syntax: $browser->assertPathIs($expectedPath);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/trips/royal-family-getaway')
        ->click('@view-reviews')
        ->assertPathIs('/trips/royal-family-getaway/reviews');
Enter fullscreen mode Exit fullscreen mode
  1. Form contains the expected value: assertInputValue()
Syntax: $browser->assertInputValue($field, $expectedValue);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/bookings/create?trip_id=15')
        ->assertInputValue('guests', '1');
Enter fullscreen mode Exit fullscreen mode

Browser input values are strings, hence '1'.

  1. Booking button is disabled: assertDisabled()
Syntax: $browser->assertDisabled($selector);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/bookings/create?trip_id=15')
        ->assertDisabled('@submit-booking');
Enter fullscreen mode Exit fullscreen mode

For example, this could check an unavailable trip date.

  1. Validation error appears after a click: assertSee()
Syntax: $browser->assertSee($expectedMessage);
Enter fullscreen mode Exit fullscreen mode
$browser->visit('/bookings/create?trip_id=15')
        ->type('guests', '0')
        ->click('@submit-booking')
        ->assertSee('The guests field must be at least 1.');
Enter fullscreen mode Exit fullscreen mode

Performance testing assertion commands

For these examples, $elapsedMs means a measured duration in milliseconds. Choose the limit from your own environment and requirements; the numbers below are illustrations. Very tight timing assertions can fail when a test server is busy.

  1. Calculation completes under a limit: assertLessThan()
Syntax: $this->assertLessThan($maximum, $actual);
Enter fullscreen mode Exit fullscreen mode
$start = hrtime(true);
$price = $calculator->totalForGuests(4500, 4);
$elapsedMs = (hrtime(true) - $start) / 1_000_000;

$this->assertSame(18000, $price);
$this->assertLessThan(50, $elapsedMs);
Enter fullscreen mode Exit fullscreen mode
  1. Search duration stays within a limit: assertLessThanOrEqual()
Syntax: $this->assertLessThanOrEqual($maximum, $actual);
Enter fullscreen mode Exit fullscreen mode
$start = hrtime(true);
$results = $searchService->search('Nainital');
$elapsedMs = (hrtime(true) - $start) / 1_000_000;

$this->assertNotEmpty($results);
$this->assertLessThanOrEqual(200, $elapsedMs);
Enter fullscreen mode Exit fullscreen mode
  1. Query count stays within a limit: assertLessThanOrEqual()
Syntax: $this->assertLessThanOrEqual($maximumQueries, $actualQueries);
Enter fullscreen mode Exit fullscreen mode
DB::enableQueryLog();

$response = $this->get('/trips/royal-family-getaway');
$queryCount = count(DB::getQueryLog());

$response->assertOk();
$this->assertLessThanOrEqual(20, $queryCount);

DB::disableQueryLog();
Enter fullscreen mode Exit fullscreen mode

Run this against a test database. Query count checks can reveal problems such as repeatedly loading reviews one at a time.

  1. Results are paginated: assertCount()
Syntax: $this->assertCount($expectedCount, $collection);
Enter fullscreen mode Exit fullscreen mode
$reviews = $reviewService->forTrip(tripId: 15, limit: 10);

$this->assertCount(10, $reviews);
Enter fullscreen mode Exit fullscreen mode

This checks that the service does not return more reviews than requested; it does not measure speed.

  1. Memory growth stays within a limit: assertLessThan()
Syntax: $this->assertLessThan($maximumBytes, $actualBytes);
Enter fullscreen mode Exit fullscreen mode
$before = memory_get_usage(true);
$reviews = $reviewService->forTrip(tripId: 15, limit: 10);
$growth = memory_get_usage(true) - $before;

$this->assertLessThan(10 * 1024 * 1024, $growth);
Enter fullscreen mode Exit fullscreen mode

PHP allocates memory in chunks, so use a sensible limit and repeat measurements before relying on this check.

  1. Response has a maximum payload size: assertLessThan()
Syntax: $this->assertLessThan($maximumBytes, $actualBytes);
Enter fullscreen mode Exit fullscreen mode
$response = $this->getJson('/api/trips/15/reviews?limit=10');

$response->assertOk();
$this->assertLessThan(
    100 * 1024,
    strlen($response->getContent())
);

Enter fullscreen mode Exit fullscreen mode

This checks that the reviews API does not return an unexpectedly large body.

Key distinction: Tests 15–17 measure execution or queries; tests 18–20 check factors that can affect performance. For reliable real traffic performance numbers, use a load test alongside these PHPUnit checks.

Commands to run them

cd /opt/lampp/htdocs/holiday-new/booking
php vendor/bin/phpunit --testdox tests/Feature
php artisan dusk
php vendor/bin/phpunit --testsuite Performance
Enter fullscreen mode Exit fullscreen mode

Top comments (0)