If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Testing software development का एक important part है, और Laravel एक ऐसा powerful PHP framework है जो testing को आसान और efficient बनाता है।
Unit और Feature testing दोनो काफी important है, लेकिन इन दोनो का use case और focus अलग होता है।
तो, चलिए देखते हैं इन दोनो testing types के बारे में detail में, और समझते हैं कौनसे scenarios में कौनसा testing type use करना चाहिए।
●●●
Unit Testing का main purpose होता है एक छोटे unit, मतलब एक function या method को test करना। इसमें हम typically code के internal logic को test करते हैं और verify करते हैं कि वो expected behavior दिखाता है या नहीं।
Unit testing isolated होता है, इसका मतलब है कि हम external dependencies को mock करते हैं, ताकि test only एक method के behavior पर focused हो।
Isolated Testing : एक specific unit को test करते हैं जैसे एक method या function.
Faster Execution : Unit tests बहुत fast होते हैं, क्योंकि वो isolated tests होते हैं।
Focused on Logic : हम सिर्फ code कि logic को test करते हैं, न कि external interactions को।
Suppose , हमारे पास एक Calculator class है, जो numbers को add करता है, हम इस add() method का unit test लिखेंगे।
// Calculator.php
class Calculator {
public function add($a, $b) {
return $a + $b;
}
}अब हम इस add() method को unit test करेंगे -
// CalculatorTest.php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase {
public function testAddMethod() {
$calculator = new Calculator();
$result = $calculator->add(5, 3);
$this->assertEquals(8, $result); // Test if 5 + 3 equals 8
}
}Unit test को run करने के लिए, आपको Laravel के testing environment का use करते हुए artisan command से tests run करना होगा।
php artisan test
●●●
Feature Testing का goal होता है एक complete feature को test करना, जो multiple components या services को involve करता है। यहाँ हम real-world interactions को test करते हैं, जैसे user form submissions, database interactions, API responses, etc.
Feature testing end-to-end functionality को test करता है, जिसमे controllers, models, views, और database शामिल होते हैं।
End-To-End Testing : पूरे feature को test किया जाता है।
Real Interactions : Test main real database, API, या views कि interactions होती हैं।
More Comprehensive : Feature tests ज़्यादा broad और complex होते हैं unit tests के comparison में।
Now suppose , हमारे पास एक login functionality है जिसमे user email और password डालता है , हम feature test लिखेंगे जिसमे हम यह check करेंगे कि user सही credentials के साथ login कर पता है या नहीं।
// LoginTest.php
use App\Models\User;
class LoginTest extends TestCase {
public function testUserCanLogin() {
// Creating a user for testing
$user = User::factory()->create([
'email' => 'test@example.com',
'password' => bcrypt('password123')
]);
// Simulating login via POST request
$response = $this->post('/login', [
'email' => 'test@example.com',
'password' => 'password123'
]);
// Check if the login redirects to the dashboard
$response->assertRedirect('/dashboard');
// Check if the user is authenticated
$this->assertAuthenticatedAs($user);
}
}Run Test :
php artisan test
●●●
Isolated Functions : जब आपको individual functions या methods को test करना हो।
Core Logic : अगर आप code कि core logic को test करना चाहते हैं।
Faster Feedback : जब आपको जल्दी से feedback चाहिए हो, especially during development.
No Dependencies : जब आपके test को external dependencies कि need न हो (like database or external APIs).
Complete Workflows : जब आपको complete feature को end-तो-end test करना हो।
User Interactions : जब आपको user interactions को test करना हो, जैसे login, form submissions, etc.
Database Interactions : जब आपको database operations (create, update, delete) को test करना हो।
API Testing : जब आपको APIs को test करना हो और उनके responses को validate करना हो।
Unit Testing आपको छोटे parts (like functions) को isolated और fast way में test करने में help करता है। यह core logic के लिए ideal है।
Feature Testing आपको larger features या user workflows को end-तो-end test करने में help करता है। यह real-world scenarios को simulate करता है जिसमे multiple components involved होते हैं।
●●●
Loading ...