-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Erhan Yakut
committed
Apr 2, 2020
1 parent
30060c8
commit 3422257
Showing
28 changed files
with
292 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.