Docker के साथ Go इंटीग्रेशन टेस्टिंग: पूर्ण गाइड
\n\nइंटीग्रेशन टेस्टिंग डेवलपमेंट का एक महत्वपूर्ण चरण है, खासकर माइक्रोसर्विसेज और डिस्ट्रीब्यूटेड सिस्टम की दुनिया में। Go (Golang) Docker के साथ मिलकर विश्वसनीय इंटीग्रेशन टेस्ट बनाने के लिए शक्तिशाली और सुरुचिपूर्ण टूल प्रदान करता है। इस लेख में, हम सीखेंगे कि एनवायरनमेंट कैसे सेट करें, डेटाबेस और बाहरी सेवाओं के साथ इंटरैक्शन के लिए टेस्ट कैसे लिखें, Docker, testcontainers-go और स्टैंडर्ड testing लाइब्रेरी का उपयोग करके।
1. इंटीग्रेशन टेस्टिंग के लिए Docker क्यों?
\n\nपारंपरिक यूनिट टेस्ट लॉजिक को अलग करते हैं, लेकिन वास्तविक डिपेंडेंसीज: डेटाबेस, क्यू, API के साथ इंटरैक्शन की जांच नहीं करते। Docker इस समस्या को हल करता है, जिससे आप यह कर सकते हैं:
\n- \n
- एनवायरनमेंट को अलग करना: प्रत्येक टेस्ट एक साफ कंटेनर चलाता है, स्टेट विरोध को समाप्त करता है। \n
- पुनरावृत्ति: लोकल मशीन और CI/CD में समान एनवायरनमेंट। \n
- त्वरित डिप्लॉयमेंट: कंटेनर सेकंडों में शुरू होते हैं। \n
- सफाई में आसानी: टेस्ट के बाद कंटेनर नष्ट हो जाता है। \n
हम testcontainers-go लाइब्रेरी का उपयोग करेंगे — यह टेस्ट कोड से सीधे कंटेनरों के जीवनचक्र को प्रबंधित करने के लिए एक उच्च-स्तरीय API प्रदान करती है।
2. प्रोजेक्ट सेटअप और डिपेंडेंसीज इंस्टॉल करना
\n\nहम Go में एक सरल HTTP सर्वर बनाएंगे जो PostgreSQL में डेटा स्टोर करता है। फिर हम एक इंटीग्रेशन टेस्ट लिखेंगे जो एंडपॉइंट की जांच करता है।
\n\nप्रोजेक्ट संरचना:
\nmyapp/\n├── main.go\n├── go.mod\n├── go.sum\n├── handlers/\n│ └── user.go\n└── integration_test.go\n\nचरण 1: मॉड्यूल इनिशियलाइज़ करें और testcontainers इंस्टॉल करें:
\ngo mod init myapp\ngo get github.com/testcontainers/testcontainers-go\ngo get github.com/testcontainers/testcontainers-go/modules/postgres\ngo get github.com/lib/pq\ngo get github.com/stretchr/testify/assert\n\nचरण 2: handlers/user.go में एक सरल हैंडलर लिखें:
package handlers\n\nimport (\n "database/sql"\n "encoding/json"\n "net/http"\n)\n\ntype User struct {\n Name string `json:"name"`\n Age int `json:"age"`\n}\n\nfunc CreateUser(db *sql.DB) http.HandlerFunc {\n return func(w http.ResponseWriter, r *http.Request) {\n var user User\n if err := json.NewDecoder(r.Body).Decode(&user); err != nil {\n http.Error(w, err.Error(), http.StatusBadRequest)\n return\n }\n _, err := db.Exec("INSERT INTO users (name, age) VALUES ($1, $2)", user.Name, user.Age)\n if err != nil {\n http.Error(w, err.Error(), http.StatusInternalServerError)\n return\n }\n w.WriteHeader(http.StatusCreated)\n json.NewEncoder(w).Encode(user)\n }\n}\n\n3. Docker के साथ इंटीग्रेशन टेस्ट लिखना
\n\nअब सबसे दिलचस्प हिस्सा — एक टेस्ट जो PostgreSQL को Docker में चलाता है, एक टेबल बनाता है, हमारा सर्वर शुरू करता है और इसके काम की जांच करता है।
\n\nफ़ाइल integration_test.go:
package myapp\n\nimport (\n "bytes"\n "context"\n "encoding/json"\n "net/http"\n "net/http/httptest"\n "testing"\n\n "myapp/handlers"\n\n "github.com/lib/pq"\n "github.com/stretchr/testify/assert"\n "github.com/testcontainers/testcontainers-go"\n "github.com/testcontainers/testcontainers-go/modules/postgres"\n "github.com/testcontainers/testcontainers-go/wait"\n)\n\nfunc TestCreateUserIntegration(t *testing.T) {\n ctx := context.Background()\n\n // 1. PostgreSQL कंटेनर शुरू करें\n pgContainer, err := postgres.RunContainer(ctx,\n testcontainers.WithImage("postgres:15-alpine"),\n postgres.WithDatabase("testdb"),\n postgres.WithUsername("test"),\n postgres.