Neden test yapmalıyız?
// Basit test örneği public function test_api_returns_success_response() { $response = $this->get('/api/users'); $response->assertStatus(200); }
php artisan make:test UserApiTest
// GET isteği $response = $this->get('/api/users/1'); // POST isteği $response = $this->post('/api/users', [ 'name' => 'Test User', 'email' => 'test@example.com' ]); // JSON isteği $response = $this->json('PUT', '/api/users/1', [ 'name' => 'Updated Name' ]);
$response->assertStatus(200); $response->assertJson(['success' => true]); $response->assertJsonStructure([ 'data' => [ 'id', 'name', 'email' ] ]);
$response->assertHeader('Content-Type', 'application/json'); $response->assertJsonValidationErrors(['email']); $response->assertUnauthorized();
// Postman test örneği pm.test("Status code is 200", function() { pm.response.to.have.status(200); }); pm.test("Response has user data", function() { var jsonData = pm.response.json(); pm.expect(jsonData.data).to.have.property('email'); });
newman run MyCollection.json --environment MyEnv.json
use Illuminate\Foundation\Testing\DatabaseTransactions; class UserApiTest extends TestCase { use DatabaseTransactions; public function test_user_creation() { $response = $this->post('/api/users', [ 'name' => 'Test', 'email' => 'test@example.com' ]); $this->assertDatabaseHas('users', [ 'email' => 'test@example.com' ]); } }
public function test_authenticated_access() { $user = User::factory()->create(); $token = $user->createToken('test')->plainTextToken; $response = $this->withHeaders([ 'Authorization' => 'Bearer ' . $token ])->get('/api/profile'); $response->assertOk(); }
public function test_api_response_time() { $start = microtime(true); $this->get('/api/users'); $this->assertLessThan(0.5, microtime(true) - $start); }
php artisan test --filter=ApiLoadTest --repeat=100
Test TürüKapsamAraçlarKarmaşıklıkUnitTekil metodlarPHPUnit⭐FeatureAPI Endpoint'lerPHPUnit/Postman⭐⭐IntegrationSistem bileşenleriPostman/Newman⭐⭐⭐LoadPerformansArtillery/Loader⭐⭐⭐⭐
Kapsamlı testlerle:
API testlerinizde hangi araçları kullanıyorsunuz? Yorumlarda paylaşın! 💬
Bir sonraki yazımız: 🚀 [Laravel'de API Security: SQL Injection ve XSS'den Korunma] - API'nizi saldırılardan nasıl korursunuz?
#Laravel #APITesting #PHPUnit #Postman #QualityAssurance 🧪