Warning: mkdir(): No space left on device in /var/www/tgoop/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/backendmaster/--): Failed to open stream: No such file or directory in /var/www/tgoop/post.php on line 50
Backend-Мастер@backendmaster P.189
BACKENDMASTER Telegram 189
Go и тестирование

GO имеет встроенную поддержку тестирования, которая позволяет легко написать и запустить тесты для вашего кода.

1. Пример написания теста для функции сравнения чисел:

package main

import "testing"

func TestCompare(t *testing.T) {
result := Compare(5, 5)
if result != 0 {
t.Errorf("Compare(5, 5) = %d; expected 0", result)
}
result = Compare(5, 10)
if result != -1 {
t.Errorf("Compare(5, 10) = %d; expected -1", result)
}
result = Compare(10, 5)
if result != 1 {
t.Errorf("Compare(10, 5) = %d; expected 1", result)
}
}

2. Пример написания теста для функции сортировки:

package main

import "testing"

func TestSort(t *testing.T) {
arr := []int{5, 3, 2, 8, 1, 9}
result := Sort(arr)
expected := []int{1, 2, 3, 5, 8, 9}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Sort(%v) = %v; expected %v", arr, result, expected)
}
}

3. Пример написания теста для метода структуры:

package main

import "testing"

func TestPerson_FullName(t *testing.T) {
person := Person{FirstName: "John", LastName: "Doe"}
result := person.FullName()
expected := "John Doe"
if result != expected {
t.Errorf("FullName() = %s; expected %s", result, expected)
}
}



tgoop.com/backendmaster/189
Create:
Last Update:

Go и тестирование

GO имеет встроенную поддержку тестирования, которая позволяет легко написать и запустить тесты для вашего кода.

1. Пример написания теста для функции сравнения чисел:

package main

import "testing"

func TestCompare(t *testing.T) {
result := Compare(5, 5)
if result != 0 {
t.Errorf("Compare(5, 5) = %d; expected 0", result)
}
result = Compare(5, 10)
if result != -1 {
t.Errorf("Compare(5, 10) = %d; expected -1", result)
}
result = Compare(10, 5)
if result != 1 {
t.Errorf("Compare(10, 5) = %d; expected 1", result)
}
}

2. Пример написания теста для функции сортировки:

package main

import "testing"

func TestSort(t *testing.T) {
arr := []int{5, 3, 2, 8, 1, 9}
result := Sort(arr)
expected := []int{1, 2, 3, 5, 8, 9}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Sort(%v) = %v; expected %v", arr, result, expected)
}
}

3. Пример написания теста для метода структуры:

package main

import "testing"

func TestPerson_FullName(t *testing.T) {
person := Person{FirstName: "John", LastName: "Doe"}
result := person.FullName()
expected := "John Doe"
if result != expected {
t.Errorf("FullName() = %s; expected %s", result, expected)
}
}

BY Backend-Мастер




Share with your friend now:
tgoop.com/backendmaster/189

View MORE
Open in Telegram


Telegram News

Date: |

Click “Save” ; Hui said the messages, which included urging the disruption of airport operations, were attempts to incite followers to make use of poisonous, corrosive or flammable substances to vandalize police vehicles, and also called on others to make weapons to harm police. To edit your name or bio, click the Menu icon and select “Manage Channel.” The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: Telegram iOS app: In the “Chats” tab, click the new message icon in the right upper corner. Select “New Channel.”
from us


Telegram Backend-Мастер
FROM American