Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Erhan Yakut committed Apr 2, 2020
1 parent 30060c8 commit 3422257
Show file tree
Hide file tree
Showing 28 changed files with 292 additions and 256 deletions.
6 changes: 6 additions & 0 deletions 01-normal/normal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package normal

func Sum(x, y int) int {
z := x + y
return z
}
14 changes: 14 additions & 0 deletions 01-normal/normal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package normal

import "testing"

func TestSum(t *testing.T) {

want := 8
got := Sum(3, 5)

if got != want {
t.Errorf("Test fail! want: '%d', got: '%d'", want, got)
}

}
6 changes: 6 additions & 0 deletions 02-table/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package table

func Sum(x, y int) int {
z := x + y
return z
}
40 changes: 40 additions & 0 deletions 02-table/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package table

import (
"fmt"
"testing"
)

var table = []struct {
x int
y int
want int
}{
{2, 2, 4},
{5, 3, 8},
{8, 4, 12},
{12, 5, 17},
}

//Table Test (Normal)
func TestSum(t *testing.T) {
for _, row := range table {
got := Sum(row.x, row.y)
if got != row.want {
t.Errorf("Test fail! want: '%d', got: '%d'", row.want, got)
}
}
}

//Table Test (With Subtest)
func TestSumSubtest(t *testing.T) {
for _, row := range table {
testName := fmt.Sprintf("Test %d+%d", row.x, row.y)
t.Run(testName, func(t *testing.T) {
got := Sum(row.x, row.y)
if got != row.want {
t.Errorf("Test fail! want: '%d', got: '%d'", row.want, got)
}
})
}
}
6 changes: 6 additions & 0 deletions 03-assertion/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package assertion

func Sum(x, y int) (int, error) {
z := x + y
return z, nil
}
75 changes: 75 additions & 0 deletions 03-assertion/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package assertion

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

// Assertion example with testify
func TestSum(t *testing.T) {

// Use this for more then one assert
// assert := assert.New(t)

got, err := Sum(2, 5)

assert.Equal(t, 7, got, "they should be equal")

assert.NotEqual(t, 6, got, "they should not be equal")

assert.Nil(t, err)

err = errors.New("different then nil")
if assert.NotNil(t, err) {
// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal(t, "different then nil", err.Error())
}
}

// Assertion with table test
func TestSumTable(t *testing.T) {
assert := assert.New(t)

var table = []struct {
x int
y int
want int
}{
{2, 2, 4},
{5, 3, 8},
{8, 4, 12},
{12, 5, 17},
}

for _, test := range table {
got, _ := Sum(test.x, test.y)
assert.Equal(test.want, got)
}
}

// Custom assertion function
func TestSumCustomAssertion(t *testing.T) {

customAssert := func(t *testing.T, got, want int) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}

t.Run("Test 1+3", func(t *testing.T) {
got, _ := Sum(1, 3)
want := 4
customAssert(t, got, want)
})

t.Run("Test 4+7", func(t *testing.T) {
got, _ := Sum(4, 7)
want := 11
customAssert(t, got, want)
})

}
23 changes: 23 additions & 0 deletions 04-testmain/testMain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testMain

import (
"os"
"testing"
)

func TestA(t *testing.T) {
t.Log("Test A run")
}

func TestB(t *testing.T) {
t.Log("Test B run")
}

func TestMain(m *testing.M) {
// setup()
exitVal := m.Run()
if exitVal == 0 {
// teardown()
}
os.Exit(exitVal)
}
6 changes: 6 additions & 0 deletions 05-parallel/paralel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package parallel

func Sum(x, y int) (int, error) {
z := x + y
return z, nil
}
31 changes: 31 additions & 0 deletions 05-parallel/paralel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package parallel

import (
"fmt"
"testing"
)

var table = []struct {
x int
y int
want int
}{
{2, 2, 4},
{5, 3, 8},
{8, 4, 12},
{12, 5, 17},
}

func TestSumParalel(t *testing.T) {
t.Parallel()
for _, row := range table {
testName := fmt.Sprintf("Test %d+%d", row.x, row.y)
t.Run(testName, func(t *testing.T) {
t.Parallel()
got, _ := Sum(row.x, row.y)
if got != row.want {
t.Errorf("Test fail! want: '%d', got: '%d'", row.want, got)
}
})
}
}
13 changes: 7 additions & 6 deletions tests/subTests_test.go → 06-subtest/subtest_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package tests
package subtest

import (
"testing"
)

// Go ile Subtest mantığıyla test yazma
func TestSubtests(t *testing.T) {
// <setup code>
t.Run("A", func(t *testing.T) {
t.Log("Test A1 tamamlandı")
t.Log("Test A1 completed")
})

t.Run("B", func(t *testing.T) {
t.Log("Test B tamamlandı")
t.Log("Test B completed")
})

t.Run("C", func(t *testing.T) {
t.Log("Test C tamamlandı")
t.Log("Test C completed")
})
// <tear-down code>
// <teardown code>
}
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/nethttptest/main_test.go → 07-nethttp/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestHttp(t *testing.T) {
handler(w, req)

// Status code test
if w.Code != 200 {
if w.Code != 404 {
t.Error("Http test isteği başarısız")
}

// Return value test
if w.Body.String() != "pong" {
if w.Body.String() != "pongd" {
t.Error("Dönen cevap farklı, test başarısız")
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/ginhttptest/main_test.go → 08-ginhttp/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestPingRoute(t *testing.T) {
router.ServeHTTP(w, req)

// Status code test
if w.Code != 200 {
if w.Code != 202 {
t.Error("Http test isteği başarısız")
}

// Return value test
if w.Body.String() != "pong" {
if w.Body.String() != "pongs" {
t.Error("Dönen cevap farklı, test başarısız")
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions 10-mocking/mocking_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tests

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

/* Gerçek Fonksiyonlar ********** */
type Matematik interface {
MockTopla([]int) (int, error)
}

type islem struct{}

func (*islem) MockTopla(sayilar []int) (int, error) {
toplam := 0
for i := range sayilar {
toplam = toplam + sayilar[i]
}
return toplam, nil
}

/* Test işlemleri ********** */
type MockRepository struct {
mock.Mock
}

func (mock *MockRepository) MockTopla(sayilar []int) (int, error) {
// Called, mock objesine bir methodun çağırıldığını haver verir ve döndürmek üzere bir dizi (array) değer alır
args := mock.Called(sayilar)
result := args.Get(0)

return result.(int), args.Error(1)
// Veya
// return args.Int(0), args.Error(1)
// Diğer seçenekler: args.Int(0) args.Bool(1) args.String(2)
}

func TestMockTopla(t *testing.T) {

// Test objemizin bir kopyasını oluşturalım
mockRepo := new(MockRepository)

// setup expectations
mockRepo.On("MockTopla", []int{2, 3}).Return(5, nil)

// Testini yaptığımız kodu çağıralım
testMatematik := Matematik(mockRepo)

sonuc, err := testMatematik.MockTopla([]int{2, 3})

// Beklentilerin karşılandığını iddia et (assert)
mockRepo.AssertExpectations(t)

assert.Equal(t, 5, sonuc)
assert.Nil(t, err)
}
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# go-test-Turkce
# go-test-examples
This repo conains different kinds of golang test methods. I hope that will be useful.

## Subjects:
1. Normal test
2. Table design test
3. Assertion logic
4. TestMain function
5. Parallel testing
6. Subtest logic
7. Testing API endpoints with httptest
8. Testing gin framework API endpoints with httptest
9. Database test
10. Mocking
11. Code coverage
42 changes: 0 additions & 42 deletions tests/assertTest_test.go

This file was deleted.

Loading

0 comments on commit 3422257

Please sign in to comment.