Go (Golang) में परीक्षण: फ्रेमवर्क और उपकरण

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

Go (Golang) में परीक्षण: फ्रेमवर्क और उपकरण



Go (Golang) को शुरू से ही आधुनिक विकास की आवश्यकताओं को ध्यान में रखकर डिज़ाइन किया गया था, इसलिए मानक लाइब्रेरी में पहले से ही एक शक्तिशाली पैकेज testing मौजूद है। हालांकि, जटिल प्रोजेक्ट्स के लिए अक्सर अतिरिक्त उपकरणों की आवश्यकता होती है: मॉक, एसर्शन (assertions), BDD-शैली और गोरूटीन लीक की जाँच। इस लेख में हम Go में परीक्षण के लिए मुख्य फ्रेमवर्क और लाइब्रेरीज़ पर चर्चा करेंगे, कोड उदाहरण दिखाएंगे और समझाएंगे कि कब क्या उपयोग करना चाहिए।



1. अंतर्निहित पैकेज testing



मानक पैकेज testing सब कुछ का आधार है। टेस्ट _test.go प्रत्यय वाली फ़ाइलों में लिखे जाते हैं, और फ़ंक्शन का सिग्नेचर func TestXxx(t *testing.T) होना चाहिए। उदाहरण:



package main

import ( "testing")

func Sum(a, b int) int { return a + b}

func TestSum(t *testing.T) { result := Sum(2, 3) expected := 5 if result != expected { t.Errorf("Sum(2,3) = %d; want %d", result, expected) }}


टेस्ट चलाना: go test ./...। बेंचमार्क के लिए testing.B का उपयोग किया जाता है, उदाहरणों के लिए ExampleXxx का।



2. लाइब्रेरी testify



Testify Go में परीक्षण के लिए सबसे लोकप्रिय तृतीय-पक्ष लाइब्रेरी है। यह सुविधाजनक एसर्ट्स (अभिकथन), मॉक और सहायक फ़ंक्शन प्रदान करती है।



स्थापना:

go get github.com/stretchr/testify


एसर्ट्स के साथ उदाहरण:

package main

import ( "testing" "github.com/stretchr/testify/assert")

func TestSumWithTestify(t *testing.T) { result := Sum(2, 3) assert.Equal(t, 5, result, "Sum should be 5") assert.NotEqual(t, 0, result) assert.True(t, result > 0)}


Testify में मॉक बनाने के लिए पैकेज mock भी शामिल है। उदाहरण:

package main

import ( "testing" "github.com/stretchr/testify/mock")

// DataService — इंटरफ़ेस जिसे हम मॉक करेंगेtype DataService interface { GetData(id int) string}

// MockDataService — मॉक संरचनाtype MockDataService struct { mock.Mock}

func (m *MockDataService) GetData(id int) string { args := m.Called(id) return args.String(0)}

func TestGetData(t *testing.T) { mockService := new(MockDataService) mockService.On("GetData", 1).Return("result1")

result := mockService.GetData(1) assert.Equal(t, "result1", result) mockService.AssertExpectations(t)}


3. gomock — मॉक जनरेशन



Gomock Google द्वारा — इंटरफ़ेस के आधार पर मॉक जनरेट करने का फ्रेमवर्क है। यह mockgen उपयोगिता के माध्यम से काम करता है।



स्थापना:

go install github.com/golang/mock/mockgen@v1.6.0


उपयोग का उदाहरण:

//go:generate mockgen -source=service.go -destination=mock_service.go -package=main

type Service interface { Process(data string) error}

// परीक्षण में:func TestProcess(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()

mockService := NewMockService(ctrl) mockService.EXPECT().Process("test").Return(nil)

err := mockService.Process("test") assert.NoError(t, err)}


Gomock बड़े प्रोजेक्ट्स के लिए अच्छा है जहाँ कई इंटरफ़ेस होते हैं — कोड जनरेशन समय बचाता है।



4. ginkgo — BDD-शैली



Ginkgo Behaviour-Driven Development (BDD) के लिए फ्रेमवर्क है। यह "विवरण" (Describe), "संदर्भ" (Context) और "विशिष्टता" (It) शैली में टेस्ट लिखने की अनुमति देता है।



स्थापना:

go get github.com/onsi/ginkgo/v2go get github.com/onsi/gomega


उदाहरण:

package main

import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "testing")

func TestCalculator(

अनुशंसाएँ